Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I `git rebase -i` and prevent "You asked to amend the most recent commit, but doing so would make it empty."?

Tags:

git

git-rebase

I want to run a git rebase -i some-hash.

When I run it, I get the error:

You asked to amend the most recent commit, but doing so would make it empty. You can repeat your command with --allow-empty, or you can remove the commit entirely with "git reset HEAD^".
[...]
Could not apply [...]

That error seems specific to a single commit, as --allow-empty isn't an option I can pass to rebase.

Apparently --keep-empty IS an option I can pass to git rebase, but it doesn't seem to fix the problem.

How can I rebase, and tell git, don't worry if a commit in the rebase is ends up having no effect?

like image 912
Mark Bolusmjak Avatar asked Jun 02 '16 18:06

Mark Bolusmjak


People also ask

How do you rebase without going through each commit?

If you don't want rebase to stop at each commit if you get any merge conflicts and want it to stop only once during the rebase process, you can squash all the commits into one in your fix/align-div-vertically branch by squashing them using interactive rebase before rebasing with the master.

Does rebase rewrite commit history?

To modify older or multiple commits, you can use git rebase to combine a sequence of commits into a new base commit. In standard mode, git rebase allows you to literally rewrite history — automatically applying commits in your current working branch to the passed branch head.

How do I stop git rebase?

You can run git rebase --abort to completely undo the rebase. Git will return you to your branch's state as it was before git rebase was called. You can run git rebase --skip to completely skip the commit.

Do I need to commit before git rebase -- continue?

For each change you make, you'll need to perform a new commit, and you can do that by entering the git commit --amend command. When you're finished making all your changes, you can run git rebase --continue . As before, Git is showing the commit message for you to edit.


4 Answers

Seems like running:

git commit --allow-empty
git rebase --continue

Creates 2 squashed commits, instead of one. Running git rebase -i some-hash allows me to squash the 2 new commits into one.

like image 127
Mark Bolusmjak Avatar answered Oct 16 '22 20:10

Mark Bolusmjak


If you are not rebasing a single commit and facing the same problem, the reason is the final result of your rebasing is an empty commit.

Assuming you don't want to commit the empty commit, let's say you have these two commits:

commit 1
commit 2

And you are getting the error when you want to merge these two commits (say by git rebase -i HEAD~2), the fix is to reset those two commits by doing the following command for two times:

git reset HEAD^
like image 31
Yar Avatar answered Oct 16 '22 22:10

Yar


I found the simplest solution was to just interactively rebase (git rebase -i {some head}) and then drop the commits that were causing issues (with d in the rebase screen).

If you know that the commits are direct mirrors of each other (squashing them will result in an empty commit), and trying to squash through rebase isn't working then simply removing the commits themselves works.

As painful as it sounds, you're fiddling with the history anyway so it helps to clean up.

like image 2
scallaway Avatar answered Oct 16 '22 20:10

scallaway


The error message1 is a bit misleading, since git rebase itself does not support --allow-empty. So we need another command that does. We could make another commit (like @z5h), but then you still have two commits that you need to fixup/squash.

The solution is actually quite simple:

git commit --amend --allow-empty
git rebase --continue

This nicely prevents another commit from being created and directly results in an empty commit.


1 You asked to amend the most recent commit, but doing so would make
it empty. You can repeat your command with --allow-empty, or you can
remove the commit entirely with "git reset HEAD^".
[emphasis added]

like image 1
stk Avatar answered Oct 16 '22 20:10

stk