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?
13 Answers
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 11You 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 , and loads of other resources out there on the web.
2@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.
2To 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.
to update your branch from the master:
git checkout master git pull git checkout your_branch git merge master - git checkout master
- git pull
- git checkout feature_branch
- git rebase master
- git push -f
You need to do a forceful push after rebasing against master
There are two approaches
You want to merge the master branch into your branch
- git checkout master - git pull - git checkout your-feature-branch - git merge master //resolve conflicts if any and commit - git push
2: If you want to rebase your changes on top of main.
git checkout master #Switch to main branch git pull #Take latest git checkout your-feature-branch #Switch to story branch git pull --ff-only # Ensure branch is up to date git rebase -i origin master #Interactively rebase your commits on top of master. So your changes are on top of latest commits in main. git rebase --continue #Resolve conflicts and rebase --continue to continue with next commits git push -f origin your-feature-branch # As you have rewritten the commit history, you have to **force push** the commits 6IN CASE IF YOU WANT TO REVERT TO A LAST COMMIT AND REMOVE THE LOG HISTORY AS WELL
Use below command lets say you want to go to previous commit which has commitID SHA - 71e2e57458bde883a37b332035f784c6653ec509 the you can point to this commit it will not display any log message after this commit and all history will be erased after that.
git push origin +71e2e57458bde883a37b332035f784c6653ec509^:master For everyone who finds this thread searching for an easy-to-use and consistent solution to merge your current branch with the latest changes on master:
You can add this to your shell configuration:
alias merge='currentBranch=$(git rev-parse --abbrev-ref HEAD) && git checkout master && git pull && git checkout $currentBranch && git merge master' This alias works with 5 commands:
currentBranch=$(git rev-parse --abbrev-ref HEAD) # gets your current branch(needed for point 4) git checkout master # checks out master git pull # gets latest changes from master git checkout $currentBranch # checks out the in point 1 saved branch git merge master # merges your current branch with master After adding the alias, you can simply use the command “merge” to “update” the branch you are currently working on.
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