Update Git branches from master

Viewed 1107277

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

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

14 Answers

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..,)

to update your branch from the master:

  git checkout master
  git pull
  git checkout your_branch
  git merge master
  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

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

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

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.

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

Let's say main 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 main, you can simply pull the code using

git pull origin main

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.

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

Related