202

Okay. If I'm on a branch (say working), and I want to merge in the changes from another branch (say master), then I run the command git-merge master while on the working branch, and the changes get merged in without rebasing the history at all. If I run git-rebase master, then the changes in master are rebased to be put on the top of my working branch. But what if I want to merge in the changes from master but rebase my changes in working to be on top? How do I do that? Can it be done?

I could run git-rebase working on my master branch to put my changes on top in the master branch, but I'd like to be able to do that in my working branch, and I have no idea how. The closest that I can think of doing is creating a new branch from master and then rebase working's changes on top of that, but then I'd have a new branch instead of altering the working branch.

4 Answers 4

320

You've got what rebase does backwards. git rebase master does what you're asking for — takes the changes on the current branch (since its divergence from master) and replays them on top of master, then sets the head of the current branch to be the head of that new history. It doesn't replay the changes from master on top of the current branch.

1
  • 5
    @Jonathan it's cool. This is a little bit of a tricky topic. By the way, git rebase working would move master's changes (after the point that working branched off) to be on top of the working branch — but that's not a very sensible thing to do to master :)
    – hobbs
    Sep 4, 2011 at 5:00
85

Another way to look at it is to consider git rebase master as:

Rebase the current branch on top of master

Here , 'master' is the upstream branch, and that explain why, during a rebase, ours and theirs are reversed.

7
  • That also explains why LOCAL and REMOTE are reversed. Thanks. Jul 23, 2015 at 15:04
  • @AVIDeveloper on LOCAL and REMOTE, you can also read stackoverflow.com/a/3052118/6309
    – VonC
    Jul 23, 2015 at 15:06
  • 5
    @@VonC: Thanks. Yep, after spending an afternoon mumbling to myself "REMOTE is my branch.. LOCAL is not mine", it all sunk in. Honestly, I would've preferred seeing the branch names (or abbrev. SHA) instead of REMOTE/LOCAL/ours/theirs/mine. My thoughts are the same regarding the git difftool's horrible left/right. Kinda off topic, but for difftool I stick to git-meld and enjoy names like 'working-dir', 'stash@{0}', as so on. Jul 23, 2015 at 19:33
  • 1
    @VonC: Can you explain how can it be that after we do: git checkout branch_to_update git rebase master I get in git log the commits of master on top of local branch, instead the opposite?
    – JavaSa
    Apr 25, 2018 at 14:51
  • 1
    @JavaSa That is strange, unless the rebase was not properly completed? You might need to ask a separate question with more details in it.
    – VonC
    Apr 25, 2018 at 14:55
4

when rebasing current changes on top of master, you can:

  1. pull down latest master: git pull <remote_name> master
  2. checkout the branch you want to rebase changes into: git checkout <branch_name>
  3. perform rebase: git rebase master

an alternative path that lets you rebase changes from a remote branch (origin/master, for example)into a local branch without updating the remote branch locally is: git rebase origin/master

0

If a maintainer asks you to rebase and re-push your PR you may follow this procedure on the CLI:

git switch blead # making sure I'm in "main"
git pull # update local with remote
git switch blead-3 # branch of this PR

git rebase blead # put this commit on top
git push --force # re-push after rebasing

Tried on Windows where GitHub asked for authorization via the web browser.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.