Git merge folder by folder?
I have 2 branches “master” and “slave”, both branches did alot of changes. While merging, some folder need to be overwrite, some need to solve conflicts and some can just merge normally. What I wish is merge them part by part, folder by folder.
Merging “master” to “slave”, so I checkout branch “slave”…
- How to add Git's branch name to the commit message?
- How to get git to correctly merge moved content (not only files)
- Git HEAD referring to branch vs to commit
- Why doesn't Git remove some files when I switch a branch?
- GIT - How to get all newly created branches
- Merging commits from branch to master -> odd-looking tree
git checkout slave
FolderA, FolderB, FolderC need to be overwrite with the folders in “master”, so what I did is…
git checkout master FolderA git checkout master FolderB git checkout master FolderC git commit -am "Overwrite with master" git push origin slave
FolderD, FolderE, FolderF are folders that will have conflicts, I wish I could do merge them one by one. Then use git mergetool to solve conflicts, something like…
(git merge master FolderD)? git mergetool
But that will give me a error “fatal: ‘FolderD’ does not point to a commit”.
FolderG to FolderZ have changes that doesn’t have conflicts, so just merge normally.
git merge master
The conflicts will be complicate and need times to solve, thats why I wish to do it one by one, how do I achieve step 3 and be able to proceed to step 4?
One Solution collect form web for “Git merge folder by folder?”
Here’s one way I would do it. Give it a try and see if it does what you want.
git checkout slave git merge master git checkout --theirs FolderA FolderB FolderC git add FolderA FolderB FolderC
Now you’ll be in a state, given your example, where conflicts will be left in FolderD through FolderZ which you can resolve and
git add. This should give you what you are asking for.
The only direct problem I see is your statement that you would like to resolve the conflicts one folder at a time. The problem here is that Git’s first layer is the commit which can span multiple folders at once.
One nice thing about this approach is that Git’s history is maintained, whereas your proposed approach would destroy the history as you would be making a new, non-merge, commit for FolderA through C.
Another approach you might want to explore is using
git filter-branch to semi-automatically rewrite each commit in a branch. This will give you the ability to selectively accept and reject changes from each commit, allowing you to programmatically filter FolderA through Z as you see fit. Take a look at: