Kevin
Kevin

Reputation: 195

Does git have a shortcut command to fix up HEAD into HEAD~1

I find myself doing this sometimes:

git rebase -i HEAD~2

This shows me two commits, HEAD~1 and HEAD. I change pick to f to fix up HEAD into HEAD~1. Now I combined my two most recent commits into a single commit that only has the commit message from the older commit.

Is there a command/shortcut/option to do this quickly?

What if I want to fix up the three most recent commits into the fourth most recent commit?

The only solution I was able to find was to write a script that does this for you. But I'm looking for something that's already built into git.

Upvotes: 4

Views: 348

Answers (1)

Robbie
Robbie

Reputation: 19500

One solution would be to use git reset HEAD~1. This would remove the last commit and put its changes in your staging area. Then you can amend those changes into the previous commit using git commit --amend -a --no-edit. The --no-edit flag makes it use the same commit message.

Be aware you can only do this if you have a clean working directory. Otherwise, you'll squash all your uncommitted changes into the same commit.

It ends up with this one-liner that you could put in an alias for convenient use:

git reset HEAD~1 && git commit --amend -a --no-edit

As a git alias:

git config --global alias.squashLast2 '!git reset HEAD~1 && git commit --amend -a --no-edit'

Upvotes: 4

Related Questions