How do I interactively rebase a git topic branch in isolation without counting commits?

If I have branches master and topic like so:

A -- B -- C -- D (master)
     \
      W -- X -- Y -- Z (topic)

And I need to manipulate the commits on ‘topic’ with various fixups, squashes, amends, etc., I would typically checkout ‘topic’ and run git rebase -i. Lets say I do that and I squash Z into Y, so it goes away as a unique commit. The git rebase -i will default to rebasing onto the head of master, and so I will get:

A -- B -- C -- D (master)
               \
                W -- X -- YZ

Now, often I actually don’t want the new commits on master to become visible on my topic branch (e.g. might cause an expensive recompile).

The recommended way to do this is to issue git rebase -i HEAD~4 while on ‘topic’. Great, that gets me what I really want, with Y and Z squashed and topic still originating at ‘B’.

A -- B -- C -- D (master)
     \
      W -- X -- YZ (topic)

However, what I don’t like about this is that I need to manually count the number of commits in topic. This might be large, and will certainly change, so I can’t just re-run it from my shell history. Every time I want to do this I need to run ‘git log’, count commits, edit the command, etc., and I ‘rebase -i’ all the time.

How can I achieve the same effect without needing to manually count the commits on the topic branch to synthesize ‘HEAD~N’? In other words, I want a non-context-dependent command that means to interactively rebase all commits on the current topic branch without migrating them to the head of master.

It is fine if this is of the form git rebase -i $(git ...) since I just plan to alias this command or recover it from my history as needed, however, if possible I would prefer that it not mention the name of the topic branch, since that again makes the command context sensitive and I would need to edit it when I change topic branches.

  • How to change a fresh git repository into a branch from another repo?
  • Switching Git workflow from merge to rebase: how to clean up a merge commit history?
  • Taking your work Hostage with git?
  • Git: squashing material shared between branches
  • where is git-svn's git-svn TAG?
  • git - Easier way to do a simple one-commit squash?
  • How to rebase my feature branch to development branch in git with least possible conflicts?
  • How to split a branch in two with Git?
  • One Solution collect form web for “How do I interactively rebase a git topic branch in isolation without counting commits?”

    You can get the commit where the topic branch was created

    git merge-base topic master
    

    So in one command, this is

    git rebase -i `git merge-base topic master`
    

    This will do what you want: interactively rebase from the starting commit of topic.

    If you want this to not contain any branch names and have set up your upstream branches correctly, you can also use this:

    git rebase -i `git merge-base HEAD @{u}`
    
    Git Baby is a git and github fan, let's start git clone.