I'm new to Git, and now I'm in this situation:
master
and... here is my problem:How do I update all other branches with master
branch code?
This question is related to
git
git-branch
To update other branches like (backup) with your master branch copy. You can do follow either way (rebase or merge)...
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..,)
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.
You can merge, or you can apply individual commits across branches by using git cherry-pick.
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 b3
This 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.
git
can 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 branch b1
merges master
, or master
merges b1
, 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 b3
People 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 <-- b1
The merge results in the true history:
A --- B --- C --- D <-- master
\ \
\-- E --- F --- G +-- H <-- b1
The rebase, however, gives you this history:
A --- B --- C --- D <-- master
\
\-- E' --- F' --- G' <-- b1
The point is, that the commits E'
, F'
, and G'
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 in master
are important to the development in b1
.
The consequence of this may be, that you can't distinguish which of the three commits E
, F
, and G
actually introduced a regression, diminishing the value of git 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.
to update your branch from the master:
git checkout master
git pull
git checkout your_branch
git merge master
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:
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.
You need to do a forceful push after rebasing against master
There are two options for this problem.
1) git rebase
2) git merge
Only diff with above both in case of merge, will have extra commit in history
1) git checkout branch(b1,b2,b3)
2) git rebase origin/master (In case of conflicts resolve locally by doing git rebase --continue)
3) git push
Alternatively, git merge option is similar fashion
1) git checkout "your_branch"(b1,b2,b3)
2) git merge master
3) git push
@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.
Source: Stackoverflow.com