Edward Z. Yang
Edward Z. Yang

Reputation: 26742

Git one-liner to revert unwanted submodule change

So, let's say I messed up and ran git commit -a, accidentally committing a submodule change that I didn't want (probably because I ran git pull and forgot to run git submodule update afterwards). I want to get rid of this change. Is there a one-liner I can use to do this?

The way I do this today, is:

  1. I run git show to figure out what the old hash was
  2. I cd into the submodule, and I run git reset --hard OLD_HASH
  3. Go back to parent directory and git commit --amend -a

This is too many steps. Is there really nothing shorter?

NB: This question is different from How do I revert my changes to a git submodule? where the user accidentally made actual changes to files in the submodule. In this case, no files were edited in the submodule; I just updated the commit pointer in the wrong direction.

Upvotes: 0

Views: 139

Answers (1)

torek
torek

Reputation: 488453

You can write a one-liner, by writing a script. The script itself is inevitably going to be more than one line. It can be those three lines, if you like, or some other set of three or so lines (e.g., git rev-parse, a carefully-constructed git update-index, and git commit --amend). You can make that script into a Git command (by naming it git-foo and putting it in your path, after which git foo will run your git-foo script) or make it into a Git alias using git config and the [alias] section.

You might want to set your settings so that it's less likely to happen in the future, using submodule.recurse to true for instance. (Note that this affects git push unless you also set push.recurseSubmodules; peruse all the submodule settings in the git config documentation for more).

Upvotes: 1

Related Questions