How do I synchronize two branches in the same Git repository? How do I synchronize two branches in the same Git repository? git git

How do I synchronize two branches in the same Git repository?


If you don't need the branch around:

If you've merged foo back to master, "git branch -d foo" to kill the topic branch, and then "checkout -b foo" in the future when you need to hack on it again.

If you do need the branch around:

You can rebase your topic branch against the master branch:

git checkout foogit rebase master

Or:

git rebase master foo


Rebasing is the process of moving or combining a sequence of commits to a new base commit. Rebasing is most useful and easily visualized in the context of a feature branching workflow. The general process can be visualized as the following:

Git Rebase visual explanation

The example below combines git rebase with git merge to maintain a linear project history. This is a quick and easy way to ensure that your merges will be fast-forwarded.

# Start a new featuregit checkout -b new-feature master# Edit filesgit commit -a -m "Start developing a feature"

In the middle of our feature, we realize there’s a security hole in our project

# Create a hotfix branch based off of mastergit checkout -b hotfix master# Edit filesgit commit -a -m "Fix security hole"# Merge back into mastergit checkout mastergit merge hotfixgit branch -d hotfix

After merging the hotfix into master, we have a forked project history. Instead of a plain git merge, we’ll integrate the feature branch with a rebase to maintain a linear history:

git checkout new-featuregit rebase master

This moves new-feature to the tip of master, which lets us do a standard fast-forward merge from master:

git checkout mastergit merge new-feature

Taken from Atlassian Git Rebase Tutorial


I use the following to combine changes from two branches (mine and yours) and to synchronize both branches for continued work. This seems to be working. Does anyone see a problem with it?

git checkout mine # make sure I'm on my branchgit commit -a     # commit changesgit push origin mine  git checkout yours # switch to your branchgit pull origin yours # get changes you've committed & pushedgit checkout mine git merge yours # merge your changes into minegit push origin mine git checkout yours git rebase mine # set your branch to the merged resultgit push origin yours # push the merged result up to your branch on origingit checkout mine # get back to my branch