Search This Blog

Thursday, August 9, 2012

HowTo: Git Branch Merge with Rebasing

Rebasing Merge Commits in Git | Envato Notes

Thousands of files in a git merge...Hundreds of merge conflicts...Someone will commit before you are just assume...

You will have to rebase the merge before you push it to the remote.

The obvious way, git pull --rebase, not so lose your merge and start over.

This describes a Branch Merge, (merging two branches in a single git clone). Android Repo doesn't do multiple branches - I tried to find an easier way...

1) Install one of the git supported merge tools.

If the merge tool is on the path, git mergetool will automatically call it for each of your merge conflicts - very fast.

merge tool candidates: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 emerge vimdiff

(I like meld on Linux, it works with cvs, svn, and git.)

2) Branch Merge: Merge all changes from two branches so that they match in content and point in time.

# Clone the repo containing the branches you want to merge.

git clone xxx
# Get the branch_to_merge
git checkout branch_to_merge
# Bring changes from Master to branch_to_merge.
git merge master
# Resolve any conflicts
git mergetool
# Switch back to branch master
git checkout master
# Bring changes from branch_to_merge to master
git merge branch_to_merge
# Resolve conflicts
git mergetool
# See if there are any differences between the merged branches.
git diff branch_to_merge..master
# Done, start testing...

3) Get any changes since you started merging.

# Before push, get any changes.
git checkout master
# Get new commits from the remote.
git fetch
# Move your merge "aHEAD" of the new commits.

git rebase --preserve-merges origin/master
# You may have a set of conflicts to resolve.
git mergetool
# If there are conflicts, you will be on a detached head. Must continue.
git rebase --continue
# Save the commit message when it comes up.
# At the end of rebase you will be back on master branch.

# Next get these changes to the other branch being merged.
git checkout branch_to_merge
# Get any new commits from the remote branch_to_merge.
git fetch
git rebase --preserve-merges origin/branch_to_merge
git mergetool
git rebase --continue # Upon resolving the conflicts, you will see the commit message again.
# Save it, and you are (almost) done.

# If there were commits on the branch_to_merge,
# switch back to master and merge them in (checkout, merge, mergetool).

# You are at a stable point for pushing your merge again...

# Both local branches should match, no differences.
git diff branch_to_merge..master

# Both branches should match in time.
# You should see your final merge first, at the HEAD of the tree.
git checkout master
git log --oneline --graph
git checkout branch_to_merge
git log --oneline --graph

1 comment: