How to update local master and rebase to it using one line command from a dev branch?

Viewed 72

Say I am working on a branch called dev that is tracking master. If I want to update my master branch, I need to run these series of commands frequently:

git checkout master
git pull 
git checkout dev
git rebase -i master

Is there a one-line to achieve above, using git commands while I am on the dev branch (i.e. updating local master and rebase to it)

2 Answers

I copy here my old answer with some modifications. I assume your last command should be git rebase master (without -i).

One command:

git pull --rebase origin master:master

Let's me split it and explain. git pull --rebase master:master is equivalent to

git fetch origin master:master
git rebase master

git fetch origin master:master means: fetch new commits from remote origin branch master and update local branch master to point to the same commit as remote master. It's what you do with

git checkout master
git pull
git checkout dev

but much faster (no need to do 2 checkouts).

If you need to stash uncommitted changes add --autostash:

git pull --autostash --rebase origin master:master

In Command Prompt in Windows and in Linux bash, you could use &&, which executes the right-hand command, the next command of && only if the previous command was successful.

Related