Damir
Damir

Reputation: 56199

How to rebase local branch onto remote master

I have a cloned project from a master branch from remote repository remote_repo. I created a new branch and I committed to that branch. Other programmers pushed to the master branch in the remote_repo.

I now need to rebase my local branch RB onto remote_repo's master branch.

How to do this? What commands to type to a terminal?

Upvotes: 1460

Views: 1861226

Answers (9)

bh4r4th
bh4r4th

Reputation: 4430

Note: If you already have experience with rebase then use the one liner below for a fast rebase option.

The one-liner solution:

This assumes 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 the remote branch.

The longer solution for those new to rebase:

Step 1: This assumes that there are no commits or changes to be made on YourBranch at this point.

First we checkout YourBranch:

git checkout YourBranch
git pull --rebase

What happened? We just pulled all changes made by other developers working on YourBranch and rebased your changes on top of this rebased version.

Step 2: Resolve any conflicts brought up by the rebase.

Step 3: Rebase your local master on the remote master:

git checkout master
git pull --rebase

What happened? We just pulled all the latest changes from the remote master and rebased our local master on the remote master. I always keep the remote master clean and release ready! I also prefer to work on master or other branches locally. I recommend this approach until you become comfortable with git changes and commits.

Note: Step 3 is not needed if you are not maintaining local master, in which case you can do a fetch and rebase remote master directly on your local branch, as in the single-step solution above.

Step 4: Resolve any conflicts brought up by the rebase.

Step 5: Rebase your (rebased) local YourBranch branch on the (rebased) local master:

git checkout YourBranch
git rebase master

What happened? We just rebased our local YourBranch on the local master branch, both of which we had previously rebased on the remote versions.

Step 6: Resolve any conflicts, if any. Use git rebase --continue to continue the 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? We just pushed our changes to the remote YourBranch. --force-with-lease will determine whether there are any incoming changes for YourBranch from other developers while you rebasing. If there are such changes, git will fetch them to update your local YourBranch before pushing to the remote. This is more advisable than a plain force push, which will not fetch incoming changes from the remote.

Yahoooo...! You have successfully done a rebase!

You might also consider using:

git checkout master
git merge YourBranch

When and Why? This merges YourBranch into master if you and your co-developers are finished making changes to YourBranch. This makes YourBranch up-to-date with master when you want to work on this branch later.

                            ~:   (๑ơ ₃ ơ)♥ rebase   :~

Upvotes: 181

slisnychyi
slisnychyi

Reputation: 1980

simple solution:

git checkout master && git pull

git checkout branch

git rebase master -> resolve conflicts if any

git add .

git rebase --continue

git push --force-with-lease origin branch

Upvotes: 5

Erkka Mutanen
Erkka Mutanen

Reputation: 727

If the current branch has a lot of commits and they are needed to be squashed, fixed, and rewritten before rebasing, then interactive rebase is the correct answer. When software engineers say "rebase on top of master", what they usually mean is "do interactive rebase on top of origin/master and make sure it looks great and unnecessary commits are squashed, and commit messages are corrected".

First, check git status and make sure to start in feature branch.

If not in feature brach, try git checkout feature Then

git fetch origin
git rebase -i origin/master

Rarely, a commit history is ready to be rebased as is when rebase on top of master is requested. In most cases, the existing commits are first revised using the interactive rebase.

Upvotes: 6

Paul Draper
Paul Draper

Reputation: 83245

git pull --rebase origin master

Upvotes: 1375

GauthamManivannan
GauthamManivannan

Reputation: 617

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

Upvotes: 44

CharlesB
CharlesB

Reputation: 90316

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

Upvotes: 392

N Djel Okoye
N Djel Okoye

Reputation: 1078

1.Update Master first...

git checkout [master branch]
git pull [master branch]

2.Now rebase source-branch with master branch

git checkout [source branch]
git rebase [master branch]
git pull [source branch] (remote/source branch)
git push [source branch]

IF source branch does not yet exist on remote then do:

git push -u origin [source branch]

"et voila..."

Upvotes: 31

Naz
Naz

Reputation: 156

git fetch origin master:master pulls the latest version of master without needing to check it out.

So all you need is:

git fetch origin master:master && git rebase master 👌

Upvotes: 8

Frerich Raabe
Frerich Raabe

Reputation: 94319

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.

Upvotes: 1759

Related Questions