English 中文(简体)
Git: merging public and private branches while while keeping certain files intact in both branches
原标题:

I ve read a few git questions here, but could not find an answer to this one:

I have a public and a private branches where I want to allow certain files to diverge.

Those are configuration files with passwords and my local customizations.

I do want to be able to merge the branches both ways: from private to public and back, but I do not want to ever have those specific files merged automatically.

Is there a way to set up git this way? I d love to find an automated solution :) - so that merging could be done as usual.


EDIT: here s the solution that worked for me (Thanks to VonC for the advice on gitattribute)

the only unexpected thing for me was that "merge protection" starts working only after files have diverged in the two branches, not immediately after the following configuration was applied

.gitattributes (track with git if you want to share this) or .git/info/attributes:

file1      merge=keepmine
path/file2     merge=keepmine

keepmine is the named custom merge manager that is just a do-nothing command called instead of the internal merge driver on selected files, which is set up below

When merging from private to public branch I usually do git merge --squash private. That way private edits won t get into the git history on the public branch.

.git/config:

#public repository
[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = <public repo git url> 

#private repository
#has to set up with git init and populated with the initial commit to branch mybranch
[remote "private"]
    push = +:
    url = /path/to/local/private/repo 
[merge "keepmine"]
    name = dont_merge_selected_files
    driver = echo %O %A %B 
[branch "master"]
    remote = origin
    merge = refs/heads/master 

#private branch settings
[branch "mybranch"]
    remote = private
    merge = refs/heads/mybranch

if there s a way to improve this please comment

最佳回答

To be on the safe side, you can add a git attribute (see here for an example) for those private files.

That way, you can define a script (a "merge manager") which will ensure the file including private informations will remain empty (or with a public content) if merged on the public branch, while keeping its local content if merged to the private branch.
It means you can merge/rebase without thinking about that file.

问题回答

One way to do this is with git rebase. By keeping your private changes as a few commits off the end of your master, you can commit public stuff to the master branch (or whatever you choose your working branch to be), and then rebase your private branch against master whenever you want to update.

Another way to handle this is to keep template configuration files in Git, such as frobozz.config.template. In your working directory, copy frobozz.config.template to the (unversioned) frobozz.config and modify. Just be sure to back up your working directory too, if you need your local changes to be backed up.

This only appears to work if there are merge conflicts detected. Merging back and forth between branches the file does get overwritten. Unless I set something up wrong. Of course this in on windows msysgit git version 1.6.5.1.1367.

Keep passwords under version control is the worst idea ever. You need CVS, not git, to work with separate files. Git as many other modern DVCS working with the entire tree, not with separate files.





相关问题
bzr init-repo and multiple projects

I m having difficulties understanding bzr init-repo. I have 3 projects that I want to have in their own isolated repository, in subversion I would use svnadmin create three times to create them. Like ...

How suitable is a DVCS for the corporate environment?

I ve been using SVN for some time now, and am pretty happy with how it works (but I can t say I m an expert, and I haven t really done much with branches and merging). However an opportunity has ...

Renaming directories with the Fossil DVCS

Is it possible to rename directories with Fossil? I ve tried the obvious command: fossil mv oldname newname Fossil then informs me that it has done something: RENAME oldname newname However, ...

Distributed source control for VisualWorks Smalltalk

One of the annoying things about Smalltalk is that it (usually) requires its own VCS, due to the way that it manages its source code. Squeak and Gemstone (at least in its GLASS version) have a DVCS ...

Can I clone part of a Mercurial repository?

Is it possible to clone part of a Mercurial repository? Let s say the repository is quite large, or contains multiple projects, or multiple branches. Can I clone only part of the repository? E.g. in ...

How does git track source code moved between files?

Apparently, when you move a function from one source code file to another, the git revision log (for the new file) can show you where that code fragment was originally coming from (see for example the ...

热门标签