Git Rebase from Remote Master

11,443

There are a series of methods you could apply here to accomplish what you want. And that may depend on your repository policy. For instance, if you rebase your branch you'll not be able to push it back to the server, you would have to either push it using -f or deleting the remote branch and push a new one. Some could say rebase is not a good approach once you have already pushed you branch to the server, if that's the policy of your repo so you would have to merge the master into your branch and than push it back to the remote.

Let's talk about both approaches:

Rebase:

I personally like doing rebase more, but only if I'm sure no one else is using the branch which I'm pushing to on the remote. I like rebase because this ends up leaving a cleaner history, it does that because it removes my changes from the branch, applies the remote changes and then apply my changes back on top, but, doing that, it changes the commit hashes and it does not match with the ones you had previously for the same commit, for that reason you have to either push it using -f or delete the remote branch and creating it back again.

I would do like this, on your local repository I would go to the master branch:

git checkout master

then update it with the remote changes:

git pull

It will run a git fetch with merge behind the scenes on your git master branch.

Then go back to you branch:

git checkout my_branch

and then rebase it with the master:

git rebase master

If everything runs without conflict you're good to go.

you can either use:

git push -f origin my_branch

(BE CAREFUL! this command will override your remote branch, it's very dangerous make sure everything went fine on your rebase before using this command.)

or you could delete your remote branch end push it normally back to the server:

git push origin my_branch

Merge:

Using this approach you'll end up with a merge commit on your history, and the log can get confusing over time, but there are some advantages on this as well, if you do have a conflict, you need to resolve it only once, when you're merging, if you have a conflict when you're rebasing, you may have to resolve it multiple times, since it will try to apply your commits over the new remote commits one by one.

To do this you can just go to your master branch and update it:

git checkout master

and:

git pull

will do the same as we saw earlier

now go back to your branch:

git checkout my_branch

and run:

git merge master

if you do not have any conflict you're good to go and you can push it to the server, if you do you only have to resolve it and you can push it normally to the server as well since now you resolved the conflicts on a new commit, there is nothing diverging between your local branch and your remote one.

git push origin my_branch

And that's it, I hope you find this helpful, you can probably do this in fewer steps, but these are just the steps I like to make, they may not be the shorter ones but I feel safer this way. And of course there are lots of options on merging and rebasing I would advise you to look more into it they are very useful.

Share:
11,443
Chris Moretti
Author by

Chris Moretti

Dabbles in automation and efficiency projects at work. Also a fan of tech-based hobbies and activities around the house including FreeNAS-based custom built Plex Media Servers and home theater automation.

Updated on June 13, 2022

Comments

  • Chris Moretti
    Chris Moretti almost 2 years

    This may have been asked before but I am a little confused on the terminology, so I am not sure what command(s) are the correct ones to accomplish what I need to accomplish. Right now I have a GitHub repository I am working from with my team. On there, I made a branch for a task. I cloned the repository to my local machine and then checked out that branch locally. I started making changes that I wish to continue working on... While making these changes, my team has been updating their code and pushing changes back into the master branch on GitHub. How do I pull down their changes from the master branch on GitHub so that it updates my branch locally AND on GitHub as well (Right now on GitHub, it says I am 1 commit ahead and 19 commits behind master). Thanks for the advice here...this will be something I plan to do regularly so learning the right process would be instrumental for me...

  • Chris Moretti
    Chris Moretti about 7 years
    Ok once I add that configuration, what would be the command to accomplish what I need? Can i just do a git rebase then?
  • Yang Yu
    Yang Yu about 7 years
    just git pull. the commit in remote will merge to local.
  • Smit
    Smit about 7 years
    You must add more information to the answer. Just series of command is not useful for future purpose.
  • Chris Moretti
    Chris Moretti about 7 years
    Do i need to do anything special when I go to push back into my GitHub repo from my local branch?
  • Yang Yu
    Yang Yu about 7 years
    @user1943674 just push and check the diff. verity it.
  • Chris Moretti
    Chris Moretti about 7 years
    It is complaining about my pom.xml. Is there an easy way to just ignore my changes locally on that file, pull down the updated one from the rebase and then re-make those changes locally on that file? They were small changes so I feel that would be easier.
  • Chris Moretti
    Chris Moretti about 7 years
    I was able to avoid the pom.xml my correcting the conflict and then doing an add on it and a commit. But now my branch locally says "Your branch and 'origin/<mybranch>' have diverged, and have 22 and 1 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean" Any idea?
  • Marcone
    Marcone about 7 years
    Not sure if I understood, did you commit your changes and then you tried rebase? didn't you have any commit before that? because like I said: "...if you rebase your branch you'll not be able to push it back to the server, you would have to either push it using -f or deleting the remote branch and push a new one"
  • Chris Moretti
    Chris Moretti about 7 years
    Nevermind. I switched to the merge and it worked perfectly. I did have to add in the conflicting files. It is not a clean commit history with this, but for this particular instance, it is fine. As long as it works, which it did. Thanks!
  • MikNiller
    MikNiller over 4 years
    I think Chris is asking on how to push his changes to Github as well after the merge/rebase
  • Thomas
    Thomas over 4 years
    I think answers to (rather beginner-level) git questions that are using rebase should always come with a warning/disclaimer about potential negative consequences when rewriting history that has already been shared with others (pushed to remote).