-1

I have try to use git rebase -i HEAD~3 to squash all the commit. But git throw below error, for the commit HEAD~3 is not exist.

fatal: invalid upstream 'HEAD~3'

$ git log --oneline
7cabc02 (HEAD -> master) fix bug
26a9c03 fix bug
59fe21b record video process

So, how can I squash all the commit to one.

3

2 Answers 2

1

The way to squash those three commits, including the first one, is to run the following command:

git rebase -i --root master

Please note that the --root option was introduced in git version 1.7.12.

Running this command will open the interactive dialog to perform the rebase and that dialog will include your first commit. In that dialog, leave the first commit as pick and set the other two to squash. Save and quit your editor to perform the operation.

pick 59fe21b record video process
squash 26a9c03 fix bug
squash 7cabc02 fix bug

# Rebase 7cabc02 onto 59fe21b (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
1

You only have two commits older than your HEAD, so HEAD~3 indeed doesn't exist. If you want to (interactively) rebase all of them, you can (should!) use HEAD~2:

git rebase -i HEAD~2

You can also explicitly use a commit hash:

git rebase -i 59fe21b
1
  • this will result in 2 commit, not noe. Commented Dec 5, 2020 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.