How can I uncommit the last commit in a git bare repository?

Lavinia-Gabriela Dobrovolschi picture Lavinia-Gabriela Dobrovolschi · Jan 7, 2011 · Viewed 36.2k times · Source

Taking into consideration that there are several git commands that make no sense in a bare repository (because bare repositories don't use indexes and do not have a working directory),

git reset --hard HEAD^ 

is not a solution to uncommit the last change in such a repository.

Searching through the Internet, all I could find related to the topic is this, in which I am presented three ways of doing this:
1. "update the ref manually (which involves plumbing)";
2. "git push -f from a non-bare repository";
3. "git branch -f this $that".

Which solution do yo think is more appropriate or what other ways are there to do this? Unfortunately, the documentation I found about git bare repositories is fairly poor.

Answer

Sylvain Defresne picture Sylvain Defresne · Jan 7, 2011

You can use the git update-ref command. To remove the last commit, you would use:

$ git update-ref HEAD HEAD^

Or if you're not in the branch from which you cant to remove the last commit:

$ git update-ref refs/heads/branch-name branch-name^

You could also pass a sha1 if you want:

$ git update-ref refs/heads/branch-name a12d48e2

See the documentation of the git-update-ref command.