How to use interactive rebase on the first (root) commit of a branch?

It seems this parameter may help:

--root 

Rebase all commits reachable from <branch>, instead of limiting them with an <upstream>. This allows you to rebase the root commit(s) on a branch.

This should let you squash (I guess you actually want to fixup) your second commit onto the first:

git rebase --root -i

Take care in understanding what the --root option does, because in your case it answers your needs, but may be tricky for instance when used in branches, because it will rebase onto the farthest ancestor in the history that is reachable (ie. the root of the tree); so rebase --root will rebase z on a thru A-B-D-E-X-Y-Z:

master      A-B-C
               \
upstream        D-E  
                   \     
current branch      X-Y-Z

I came to this question looking for an answer to the title. The accepted answer, on a very large repo, yields an interactive rebase for every commit in the main branch (aka: master), not just for the given branch. For someone else who comes here, the alternative is to use the parent branch name (or commit):

git rebase -i <base_branch_name>

I realized this after digging around the git docs and finding this (https://git-scm.com/docs/git-rebase#_interactive_mode):

Start it with the last commit you want to retain as-is:

git rebase -i <after-this-commit>

An editor will be fired up with all the commits in your current branch (ignoring merge commits), which come after the given commit.

Say you have branch_a branched off of master, and you want to interactively rebase all commits on branch_a. To do that, you would do:

git rebase -i master

Alternatively, if you want to start in the middle (or at any commit), you can do:

git rebase -i <some-commit-hash-in-the-middle>

The other common form is if you know "I want to rebase back 5 commits from where I am now"

git rebase -i HEAD~5

Hope this helps someone else avoid a heart attack when their git rebase opens an editor with thousands of lines of commits. ─=≡Σ((( つ><)つ


You want to rebase to the root commit of your master branch. More specifically, to squash the two commits, you need to run

git rebase -i --root

and then substitute squash for pick on the second line in the buffer of the editor that pops up:

pick 123456 a                                                        
squash abcdef b

I refer you to the git-rebase man page for more details about that flag:

--root

Rebase all commits reachable from <branch>, instead of limiting them with an <upstream>. This allows you to rebase the root commit(s) on a branch. [...]

Example of an interactive rebase of the root

# Set things up
$ mkdir testgit
$ cd testgit
$ git init

# Make two commits
$ touch README
$ git add README
$ git commit -m "add README"
$ printf "foo\n" > README
$ git commit -am "write 'foo' in README"

# Inspect the log
$ git log --oneline --decorate --graph
* 815b6ca (HEAD -> master) write 'foo' in README
* 630ede6 add README

# Rebase (interactively) the root of the current branch: 
# - Substitute 'squash' for 'pick' on the second line; save and quit the editor.
# - Then write the commit message of the resulting commit; save and quit the editor.
$ git rebase -i --root
[detached HEAD c9003cd] add README; write 'foo' in README
 Date: Sat May 16 17:38:43 2015 +0100
 1 file changed, 1 insertion(+)
 create mode 100644 README
Successfully rebased and updated refs/heads/master.

# Inspect the log again
$ git log --oneline --decorate --graph
* c9003cd (HEAD -> master) add README; write 'foo' in README

Tags:

Git

Rebase

Squash