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

若如初见. 提交于 2019-11-27 10:22:15
Sylvain Defresne

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.

Hazok

If you use the following in a bare repo:

git reset --soft <commit>

then you don't run into the issues you have using --hard and --mixed options in a bare repo since you're not trying to change something the bare repo doesn't have (i.e. working tree and index). In your case specifically you would want to use (from the bare repo):

git reset --soft HEAD^

To switch branches on the remote repo do:

git symbolic-ref HEAD refs/heads/<branch_name>

The git push -f should work fine:
if you clone that bare repo, remove the last commit (git reset --hard HEAD^ as you mention, but in a local non-bare repo) and push back (-f):

  • you don't change any SHA1 for the other commits preceding the one you remove.
  • you are sure you push back the exact content of the bare repo minus the extra commit (because you just cloned it first).

You can also use git refspec notation and do something like this:

git push -f origin +<commit you want to revert to>:<destination_head | branch_name>

This forces update of destination branch (as denoted by the ref) to the source commit as denoted by the +<object ref> part.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!