Search code examples
gitrebasegit-rebasegit-stash

Rebasing git branches on top of tracking branches


Suppose you have a Git repository with a master branch tracking origin/master and a long-lived release branch tracking origin/release. Now, suppose that a git fetch operation has changed the tracking branches such that release is behind. Finally, suppose you have unstashed changes.

If we do git rebase --autostash while we are on master, then master will be up-to-date with its remote, and the unstashed changes will be preserved, short of a rebase/unstash conflict. The question is, if I am on master with unstashed changes (and possibly behind origin/master), what should I do in order to have release no longer behind origin/release?

My best guess is using some form of git rebase --autostash --onto, but I'm not sure what the rest of the arguments would be like. Maybe git rebase --autostash --onto release origin/release release?

Also, would it suffice to reset release to origin/release instead?


Solution

  • If you don't have new commits on your local copies of master and release, then yes, it would suffice to reset them to their remote counterparts without using rebase. Furthermore, you can achieve your example scenario without even checking out the branch:

    If you are "on master with unstashed changes", and you wish to update release with origin/release. You can simply run the command:

    git fetch
    git branch -f release origin/release
    

    However, if you are able to do that, then as torek points out in a comment, you probably don't even need a local copy of release, since anytime you would reference it you can use origin/release instead.