Search code examples
gitrebase

git rebase --onto works with tags?


After some thinking I think I have a similar case of rebase as the one in the documentation although with other branch names.

enter image description here

Here the docs say to apply

git rebase --onto master next topic

to get

enter image description here

In my case I have a branch called develop where master in the figure is, and a branch called MyFeature where topic is in the figure. In the point where "next" is I don't have a branch but I have a tag "OLDdevelop"

Can I apply git rebase --onto here without expressly creating a branch in the "next" position?


Solution

  • Yes, you can use git rebase with tags (any ref really: tags, branches (branch heads), commits). So something like

    git rebase --onto tag1 tag2 mybranch
    

    is totally fine. Even git rebase --onto tag1 tag2 somecommit works, but you will end up in detached head state.

    The full form of git rebase is:

    git rebase --onto <newbase> <upstream> <branch>
    

    The man page contains this about --onto:

    --onto <newbase>

    Starting point at which to create the new commits. If the --onto option is not specified, the starting point is <upstream>. May be any valid commit, and not just an existing branch name. [emphasis mine]

    and about <upstream>:

    <upstream>

    Upstream branch to compare against. May be any valid commit, not just an existing branch name. Defaults to the configured upstream for the current branch. [emphasis mine]

    A tag (usually) points to a commit, so you can use a tag in place of a commit. The tag will be peeled to reach the commit. Git calls these types of refs commit-ish:

    commit-ish (also committish)

    A commit object or an object that can be recursively dereferenced to a commit object. The following are all commit-ishes: a commit object, a tag object that points to a commit object, a tag object that points to a tag object that points to a commit object, etc.