I'm new to Git, and now I'm in this situation:
- I have four branches (master, b1, b2, and b3).
- After I worked on b1-b3, I realized I have something to change on branch master that should be in all other branches.
- I changed what I needed in
masterand... here is my problem:
How do I update all other branches with master branch code?
You have two options:
The first is a merge, but this creates an extra commit for the merge.
Checkout each branch:
git checkout b1
Then merge:
git merge origin/master
Then push:
git push origin b1
Alternatively, you can do a rebase:
git fetch
git rebase origin/master
You have basically two options:
You merge. That is actually quite simple, and a perfectly local operation:
git checkout b1 git merge master # repeat for b2 and b3This leaves the history exactly as it happened: You forked from master, you made changes to all branches, and finally you incorporated the changes from master into all three branches.
gitcan handle this situation really well, it is designed for merges happening in all directions, at the same time. You can trust it be able to get all threads together correctly. It simply does not care whether branchb1mergesmaster, ormastermergesb1, the merge commit looks all the same to git. The only difference is, which branch ends up pointing to this merge commit.You rebase. People with an SVN, or similar background find this more intuitive. The commands are analogue to the merge case:
git checkout b1 git rebase master # repeat for b2 and b3People like this approach because it retains a linear history in all branches. However, this linear history is a lie, and you should be aware that it is. Consider this commit graph:
A --- B --- C --- D <-- master \ \-- E --- F --- G <-- b1The merge results in the true history:
A --- B --- C --- D <-- master \ \ \-- E --- F --- G +-- H <-- b1The rebase, however, gives you this history:
A --- B --- C --- D <-- master \ \-- E' --- F' --- G' <-- b1The point is, that the commits
E',F', andG'never truly existed, and have likely never been tested. They may not even compile. It is actually quite easy to create nonsensical commits via a rebase, especially when the changes inmasterare important to the development inb1.The consequence of this may be, that you can't distinguish which of the three commits
E,F, andGactually introduced a regression, diminishing the value ofgit bisect.I am not saying that you shouldn't use
git rebase. It has its uses. But whenever you do use it, you need to be aware of the fact that you are lying about history. And you should at least compile test the new commits.
git rebase master is the proper way to do this. Merging would mean a commit would be created for the merge, while rebasing would not.
If you've been working on a branch on-and-off, or lots has happened in other branches while you've been working on something, it's best to rebase your branch onto master. This keeps the history tidy and makes things a lot easier to follow.
git checkout master
git pull
git checkout local_branch_name
git rebase master
git push --force # force required if you've already pushed
Notes:
- Don't rebase branches that you've collaborated with others on.
- You should rebase on the branch to which you will be merging which may not always be master.
There's a chapter on rebasing at http://git-scm.com/book/ch3-6.html, and loads of other resources out there on the web.
@cmaster made the best elaborated answer. In brief:
git checkout master #
git pull # update local master from remote master
git checkout <your_branch>
git merge master # solve merge conflicts if you have`
You should not rewrite branch history instead keep them in actual state for future references. While merging to master, it creates one extra commit but that is cheap. Commits does not cost.
To update other branches like (backup) with your master branch copy. You can do follow either way (rebase or merge)...
- Do rebase (there won't be any extra commit made to the backup branch).
Merge branches (there will be an extra commit automatically to the backup branch).
Note : Rebase is nothing but establishing a new base (a new copy)
git checkout backup git merge master git push
(Repeat for other branches if any like backup2 & etc..,)
git checkout backup git rebase master git push
(Repeat for other branches if any like backup2 & etc..,)
You can merge, or you can apply individual commits across branches by using git cherry-pick.
来源:https://stackoverflow.com/questions/3876977/update-git-branches-from-master