How to rebase local branch onto remote master
Solution 1
First fetch the new master from the upstream repository, then rebase your work branch on that:
git fetch origin # Updates origin/master
git rebase origin/master # Rebases current branch onto origin/master
Update: Please see Paul Draper's answer for a more concise way to do the same - recent Git versions provide a simpler way to do the equivalent of the above two commands.
Solution 2
git pull --rebase origin master
Solution 3
After committing changes to your branch, checkout master
and pull it to get its latest changes from the repo:
git checkout master
git pull origin master
Then checkout your branch and rebase your changes on master
:
git checkout RB
git rebase master
...or last two commands in one line:
git rebase master RB
When trying to push back to origin/RB
, you'll probably get an error; if you're the only one working on RB
, you can force push:
git push --force origin RB
...or as follows if you have git configured appropriately:
git push -f
Solution 4
Note: If you have broad knowledge already about rebase then use below one liner for fast rebase. Solution: Assuming you are on your working branch and you are the only person working on it.
git fetch && git rebase origin/master
Resolve any conflicts, test your code, commit and push new changes to remote branch.
~: For noobs :~
The following steps might help anyone who are new to git rebase
and wanted to do it without hassle
Step 1: Assuming that there are no commits and changes to be made on YourBranch at this point. We are visiting YourBranch.
git checkout YourBranch
git pull --rebase
What happened? Pulls all changes made by other developers working on your branch and rebases your changes on top of it.
Step 2: Resolve any conflicts that presents.
Step 3:
git checkout master
git pull --rebase
What happened? Pulls all the latest changes from remote master and rebases local master on remote master. I always keep remote master clean and release ready! And, prefer only to work on master or branches locally. I recommend in doing this until you gets a hand on git changes or commits. Note: This step is not needed if you are not maintaining local master, instead you can do a fetch and rebase remote master directly on local branch directly. As I mentioned in single step in the start.
Step 4: Resolve any conflicts that presents.
Step 5:
git checkout YourBranch
git rebase master
What happened? Rebase on master happens
Step 6: Resolve any conflicts, if there are conflicts. Use git rebase --continue
to continue rebase after adding the resolved conflicts. At any time you can use git rebase --abort
to abort the rebase.
Step 7:
git push --force-with-lease
What happened? Pushing changes to your remote YourBranch. --force-with-lease
will make sure whether there are any other incoming changes for YourBranch from other developers while you rebasing. This is super useful rather than force push. In case any incoming changes then fetch them to update your local YourBranch before pushing changes.
Why do I need to push changes? To rewrite the commit message in remote YourBranch after proper rebase or If there are any conflicts resolved? Then you need to push the changes you resolved in local repo to the remote repo of YourBranch
Yahoooo...! You are succesfully done with rebasing.
You might also be looking into doing:
git checkout master
git merge YourBranch
When and Why? Merge your branch into master if done with changes by you and other co-developers. Which makes YourBranch up-to-date with master when you wanted to work on same branch later.
~: (๑ơ ₃ ơ)♥ rebase :~
Solution 5
Step 1:
git fetch origin
Step 2:
git rebase origin/master
Step 3:(Fix if any conflicts)
git add .
Step 4:
git rebase --continue
Step 5:
git push --force
Damir
Updated on July 08, 2022Comments
-
Damir almost 2 years
I have a cloned project from a master branch from remote repository
remote_repo
. I create a new branch and I commit to that branch. Other programmers pushed toremote_repo
to the master branch.I now need to rebase my local branch
RB
ontoremote_repo
'smaster
branch.How to do this? What commands to type to a terminal?
-
Glenn Lawrence over 7 yearsFor me this question is ambiguous as "with" could mean rebasing in either direction. Looking at the answers I see that the intent is to rebase your branch onto the remote master, not the other way around. I mention it in case somebody follows an answer below and gets the reverse of what they want.
-
daniel kullmann over 6 years@GlennLawrence I think it is better to edit the original question than to add a comment. This is also encouraged by stackoverflow. Besides, rebasing master onto RB will probably fail anyway, because RB depends on the history of master.
-
-
kayaker243 over 11 yearsthis is the only answer that actually does what was asked
-
erik over 10 years@kayaker243 No, it is the same as Paul Drapers answer but in long form, I think.
-
Frerich Raabe over 10 years@erik Note that Paul Draper wrote his answer about half a year after kayaker243's comment (and almost two years after this answer).
-
Dror over 10 yearsI get the following:
Your branch and 'origin/b1' have diverged, # and have 3 and 2 different commits each, respectively.
Seems like anothergit pull
is needed. Is this correct or am I missing something here? -
Frerich Raabe over 10 years@Dror No, there's no
git pull
needed. It means that since the last time you fetched from theorigin
repository, you did three commits. You later fetched again and git noticed that apparently other people did two commits since you last fetched. So now you can either rebase your branch on top oforigin
's branch (that's what this question is about and what my answer describes), or you can merge your local branch and the one fromorigin
usinggit merge
. -
Dror over 10 yearsI started a follow up question here: superuser.com/questions/667146/…
-
RGC about 10 years- git rebase master # will do the same job as the second command? - after the two commands, my local master and RB has the same version of code, right? But i want master to be intact as remote master (without any RB commits)
-
Frerich Raabe about 10 years@RGC No,
git rebase master
will not do the same job as the second command (git rebase origin/master
) sincemaster
andorigin/master
may well point to different commits (especially given that the first command wasgit fetch origin
, which may modifyorigin/master
). -
Paul Draper almost 10 years(Equivalent to Frerich's answer)
-
Jimmy Huch over 8 yearsisn't this slightly different than Frerich's answer, in that this will commit changes from origin master onto local master, whereas Frerich's answer leaves local master untouched? (pull vs. fetch)
-
adhominem about 8 yearsNo, in Frerich's answer, the rebase modifies the local master. A pull --rebase is the same thing as a fetch followed by a rebase
-
Joey Baruch about 8 yearswhen trying to push back to origin/RB, you'll probably get an error. If youre the only one working on RB, you can git push --force origin RB. source: stackoverflow.com/questions/8939977/…
-
emmby almost 8 yearsFYI you can do interactive rebases with
git pull --rebase=interactive origin master
-
Yoshita Arora over 7 years@FrerichRaabe I by chance just did
git rebase origin
and notgit rebase origin/master
. What will happen? -
jr. over 7 years@adhominem - I checked the git-pull documentation, and I can't see anything that supports the claim that the local master is modified. If I'm on a branch named
dev
and rungit pull --rebase origin master
, only branchdev
is going to be modified, notmaster
. The--rebase
flag documentation states that it attempts torebase the current branch on top of the upstream branch after fetching
and nothing about modifying local tracking branches. -
adhominem over 7 years@studro I was assuming you were calling pull --rebase while on the local master. Otherwise, I would have been wrong.
-
jr. over 7 yearsAhh right - that make sense. Thanks. I was just a little confused as I think the original question implies that they were on a local branch (
RB
I think they called it?) other thanmaster
. -
Andrew about 7 yearsIs there a way to do an interactive rebase using this one liner?
-
Gayan almost 7 yearsIs the origin here refered to an upstream repo or a fork?
-
Frerich Raabe almost 7 years@reversiblean
origin
refers to any other Git repository. -
Gayan almost 7 years@FrerichRaabe say for example I have upstream (read-only) and origin (fork). Should I rebase over
upstream
or theorigin
? -
Motti Shneor over 6 yearsAh.... I have exactly this. my "RB" is rebased correctly, but I get endless errors when trying to push it after the rebase. Aside from push --force origin RB - is there a "nicer" (non forced) way to do it? I just try to understand gits perception here - and fail.
-
Motti Shneor over 6 yearsMy problem is to later push my rebased feature-branch to remote. I get endless errors that I can't resolve. I usually give up - delete the remote branch and push it again from my local, but that's not very nice. Can you please elaborate just a little more on how to finalize the rebase by pushing it to remote?
-
Motti Shneor over 6 yearsMy problem is to later push my rebased feature-branch to remote. I get endless errors which I fail to resolve. I usually give up, - delete the feature branch on the remote repo, then push it again from my local. That's not very nice. Could you please elaborate just a little more on how to finalize the rebase by pushing the feature-branch to remote?
-
daniel kullmann over 6 years@MottiShneor No, there is no nice way. If someone else pushes to the branch in the mean time, their changes will be lost! If you want to be nice to the git commit history, you should rather merge master into your branch, which is safe (you can do
git push
without-f
). -
Qi Fan over 6 yearsI got
invalid upstream 'origin/master'
when I tried Frerich's answer, but this answer works. I think it was because I only cloned a single branch. -
Dren over 5 yearsAnd if you want to update the master branch then, without checking out to it, use:
git fetch origin master:master
-
Noitidart over 5 yearsDoesn't
git fetch
update master without needing to check it out too? Except thatgit fetch
doesn'tgit merge
the updates right? So if we checkoutmaster
it won't have the latest updates. So isn't it shorter to do while on feature branch,git fetch
thengit rebase origin/master
? We can't dogit rebase master
because that will try to rebase frommaster
in workspace, we needorigin/master
to get from the unmerged but sitting in local. -
Vasilis Tsirimokos over 5 yearsWhen I used the proposed 1 liner command from @PaulDraper I got:
Your branch and 'origin/XXX' have diverged, and have 17 and 5 different commits each, respectively. (use "git pull" to merge the remote branch into yours)
What does this mean exactly? Should I do a git pull? -
KeitelDOG over 5 yearsI prefer this one because
git fetch origin
brings all remote branches, even incomplete ones, while I only need a specific branch. And a one-line is preferred -
John Little about 5 yearsWhat is this for: "Pulls all the latest changes from master and rebases master on latest master.". Rebase master on master? Dont you just need to pull the latest master?
-
aerijman about 5 years@FrerichRaabe I understand that
origin
=remote_repo
as if previously he could have donegit remote add origin https://github.com/whoever/remote_repo.git
? -
bh4r4th about 5 years@JohnLittle Thanks for pointing out. I mean
Pulls latest changes from remote master to local master. I always prefer keeping remote master clean and release ready always!
. I will update my description. -
Suleka_28 almost 5 years@FrerichRaabe I have the same issue. Are both of these commands run on the local branch or one on local master and the other on local branch?
-
Frerich Raabe almost 5 years@Suleka_28 Both commands are run while having the same branch checked out: the branch which you want to rebase onto the remote
master
branch. -
Dave Liu almost 5 yearsI like the step-by-step nature of this answer. It helps break down what exactly is happening.
-
Michael Iyke almost 5 yearsThanks for this answer. Was really helpful to me and helped me to easily resolve my conflicts
-
basickarl over 4 yearsNo explanation as to which branch to start on. Not a good answer.
-
Jordi Nebot almost 4 yearsPlease don't do this if you don't know exactly what is implies. Force pushing is not a good idea.
-
slisnychyi almost 4 years!!! please don't do
git push --force
can be very very dangerous. datree.io/resources/git-push-force -
CpILL over 3 yearsThe
git push --force-with-lease
is the tricky bit and what nobody talks about when rebasing as you'll get ayour branch is X ahead and Y behind origin
which if you try to pull and push again will make a huge mess of things. -
Reaper over 3 yearsMost of the answers including the highest rated one is force pushing. If you will not force push you will not have the history lined up properly over master or whatever the branch you are rebasing on.
-
Mikhail Vasilyev about 3 yearsShould've been
git fetch origin master
-
maverick about 3 yearsThanks for this answer. This has helped me a lot. A lot.
-
Chris Parry over 2 years
git push --force-with-lease
is a safer way to push changes after a rebase. It basically checks if another member of your team has made a commit before pushing your changes. See stackoverflow.com/questions/52823692/… -
Snowcrash about 2 yearsFor most modern repos you'll find you'll need to swap
master
formain
. -
Tuomas Valtonen about 2 yearsThank you so much for the
step 7
, this always confused me in rebasing since you need to merge anyway in the end and the initial question is always "should i merge or rebase"! Also the push with--force-with-lease
top stuff.