How do you rebase the current branch's changes on top of changes being merged in?

GitMergeRebaseGit Rebase

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan M DavisView Question on Stackoverflow
Solution 1 - GithobbsView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitBen PerlmutterView Answer on Stackoverflow