Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: merging public and private branches while while keeping certain files intact in both branches

Tags:

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

like image 611
Evgeny Avatar asked Nov 27 '09 06:11

Evgeny


People also ask

Does merging affect both branches?

So merging one branch into another has a secondary effect: it moves the merge base of those two branches. In particular, the new merge base of the two branches is now the second parent of the merge commit. In many situations, that fact will not matter to you.

Do branches get deleted after merge?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.

What git strategies are used to merge two branches?

Recursive is the default merge strategy when pulling or merging one branch. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies.

What happens to a branch when you merge?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.


1 Answers

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.

like image 158
VonC Avatar answered May 11 '23 01:05

VonC