Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96274

Pull & rebase from local branch

I would like to fetch all branches from the remote and then rebase my current branch on any changes to the master branch.

To do this, for some reason, I need to first checkout master, pull, and then go back to my current branch to rebase. Is there anyway to do this without having to switch branches?

Here is the workflow:

> git check_out main
> git checkout my_branch
> ...
> # do some work on my_branch
> ...

To then rebase on the latest changes to master, I can do the following:

Option 1:

> # Update my master with remote master
> git checkout master
> git pull
> git checkout my_branch
> git rebase master 

Option 2 (without switching branches). It doesn't seem to work:

> git pull # Or git fetch
> # It complains with "There is no tracking information for the current branch"
> git rebase master 

How can I rebase on master without switching branches?

Upvotes: 1

Views: 4705

Answers (2)

Gareth
Gareth

Reputation: 138042

The thing about this operation is that you are trying to update 2 local branches (both your master branch and your my_branch), where either branch may have conflicts to deal with – until you've fetched changes from the remote you don't know that the remote master is a clean fast-forward.

Since conflicts can only be resolved using a working copy, you need to perform each merge/rebase "from" the corresponding local branch.

Upvotes: 2

Per Johansson
Per Johansson

Reputation: 6877

You can use git rebase origin/master to rebase on the fetched master branch (provided the remote name is origin).

Your local master branch will not be updated by that though, but once you check it out and pull it they will be in sync.

Upvotes: 4

Related Questions