1119

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 master and... here is my problem:

How do I update all other branches with master branch code?

3
  • 5
    I found my answer here: How do you merge selective files with git-merge? Commented Oct 18, 2017 at 15:45
  • 176
    Yet another simple task made difficult by Git. The Git devs should use Stack Overflow as feedback in their SDLC loop. 300,000 people should indicate something is seriously wrong with Git's workflow. They need to hire a UX expert because they clearly cannot git it right on their own. Commented Feb 8, 2018 at 20:15
  • 3
    @jww what is sdlc loop?system development life cycle? Commented Mar 2, 2022 at 21:57

14 Answers 14

1052

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 
Sign up to request clarification or add additional context in comments.

13 Comments

I have a concern about this approach. When I run git log --graph, the graph show the master is actually merged to the topic branch. Will this cause any problem in the long run? I thought the best practice is always merge the topic branch back to master. Please comment.
Look out for this issue if you're going with the merge workflow: randyfay.com/node/89
You are merging master into b1. Why do you got push origin master... doesn't make sense. You're not changing master branch. I think it's a mistake with 119 upvote :/
Do not use the merge method, using git rebase master is the correct answer
For those of us reading later - @Kursion 's concern about the typo was addressed by the author's edit. Also, the second highest upvoted answer below says basically the same thing as this answer but with a diagram of the branch structure and a warning as to why you wouldn't want to rebase.
|
809

You have basically two options:

  1. 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.

  2. 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.

8 Comments

I was merging another source branch (not master) and additional steps to add to this nice answer was to update it on my local repo before merging (to have the latest code locally): git checkout <source branch> git pull. Then continuing with above: git checkout b1 ...
As a long-time SVN user, I prefer the merge option to the rebase: using any version control it is very, very important to keep accurate records of the changes you made and why. I can see the appeal of the rebase for simplifying the apparent history, but you should then go back and add to the commit comments of E', F', G' - and preferably have the rebase automatically added to those comments. Otherwise if the build/test/test-deploy process breaks on G' you have to work out why the changes were made without full information.
The history is a lie
Thanks i use "git merge any-branch-name" to merge one branch code to another branch. Locally i can test code of branch 1 while i am on branch 2
This is the best answer IMO. Anyone merging/rebasing should know exactly what they're doing. The accepted answer is correct and concise but it does nothing to explain the fundamental differences.
|
279

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.

8 Comments

What about when you have already pushed to origin, if you rebase you will be rewriting the commit history and this will conflict with your remote branch. I think rebase should only be used on a pull or when you haven't pushed to a remote branch.
If you are the only one working on the remote branch you can do git push --force origin feature to update your remote branch with rebased local branch. stackoverflow.com/questions/8939977/…
rebase and merge both works, rebase is best for private branches, because it gives a cleaner history graph. this answer is the best
Need to be clearer about the trade-off between clarity (great for single-user or small-team) or messy truth (for multi-contributor code branches - required for maintainability (in my experience - YMMV)).
|
88

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.

1 Comment

Tip: git checkout local_branch_name could be git checkout - if the branch you were previouly on was local_branch_name
34

@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.

2 Comments

Is refreshing a feature branch from master, something that should be done on a regular basis ? Say when the feature branch takes a while to complete, and master has evolved in that time.
Why do we use git merge at the end? Why not git pull, is it redundant?
21

Surprisingly the most common method I use isn't mentioned. It's quite common when working Trunk Based Development style where master constantly gets updated and one is working from a branch from it.

Let's say master already has the updated code and that you are in branch b1. If that's not the case, you'll need git fetch.

So, to update b1 with the changes made in master, you can simply pull the code using

git pull origin master 

The same will have to be done in the other branches when you, or someone else, gets to them and wants to update as well.

1 Comment

Would you mind elaborating how this is different from using rebase or a merge mentioned in other answers?
19

To update other branches like (backup) with your master branch copy. You can do follow either way (rebase or merge)...

  1. Do rebase (there won't be any extra commit made to the backup branch).
  2. 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..,)

Comments

13

to update your branch from the master:

 git checkout master git pull git checkout your_branch git merge master 

1 Comment

I'd say you should do git pull --ff-only to avoid unwanted side effects.
12

You can merge, or you can apply individual commits across branches by using git cherry-pick.

Comments

11
  1. git checkout master
  2. git pull
  3. git checkout feature_branch
  4. git rebase master
  5. git push -f

You need to do a forceful push after rebasing against master

Comments

10

There are two approaches

  1. 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 

6 Comments

@Anku I have added inline comments to make it more explanatory.
Hi @Sanjay.. I tried 2nd option but did not work. After running git push..I go to feature-branch using git checkout feature_branch. AND I do not see any changes in feature branch that comes from master branch.
Is there a way without resolving conflict as there are lot of changes has been made in Master and I do not want to manully resolve them.
If there are conflicts they have to be resolved. IF your branch is way out of date from master (not ideal), then I will suggest create another branch from main (so you have latest) and do your changes on top of it. (Some manual efforts)
Thanks @Sanjay, This is what I did: 1. I have cloned Master branch in different folder. 2. Copied (CTL + C) every content from this folder and Paste (CTL + V) to my feature branch folder. Now I have everthing that Master branch had. I hope this is something can be done by git command but anyways. Thanks for your help!
|
2

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.

Comments

1

IN 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 

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.