如何取消提交 git 裸存储库中的最后一次提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4624881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:47:23  来源:igfitidea点击:

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

gitrepositorycommitresetgit-bare

提问by Lavinia-Gabriela Dobrovolschi

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 命令在裸仓库中没有意义(因为裸仓库不使用索引,也没有工作目录),

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 -ffrom a non-bare repository";
3. "git branch -f this $that".

通过 Internet 搜索,我能找到的与该主题相关的所有内容是this,其中介绍了三种执行此操作的方法:
1. “手动更新 ref(涉及管道)”;
2.“git push -f来自非裸存储库”;
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.

您认为哪种解决方案更合适,或者还有哪些其他方法可以做到这一点?不幸的是,我发现的关于 git 裸存储库的文档相当糟糕。

回答by Sylvain Defresne

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

您可以使用该git update-ref命令。要删除最后一次提交,您可以使用:

$ 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:

如果需要,您也可以传递 sha1:

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

See the documentation of the git-update-refcommand.

请参阅git-update-ref命令的文档。

回答by 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 --hardand --mixedoptions 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):

那么你就不会遇到你在裸仓库中使用的问题--hard--mixed选项,因为你没有试图改变裸仓库没有的东西(即工作树和索引)。在您的情况下,您特别想使用(来自裸仓库):

git reset --soft HEAD^

To switch branches on the remote repodo:

在远程仓库上切换分支,请执行以下操作:

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

To see current selected branch use:

要查看当前选定的分支,请使用:

git symbolic-ref HEAD

https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html

https://mirrors.edge.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html

回答by VonC

The git push -fshould 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):

git push -f应正常工作:
如果你克隆裸回购,除去最后提交的(git reset --hard HEAD^你提到,但在当地的一个非裸露回购)和推背(-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).
  • 在您删除的提交之前,您不会更改其他提交的任何 SHA1。
  • 你确定你推回了裸仓库的确切内容减去额外的提交(因为你只是先克隆了它)。

回答by alup

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

您还可以使用 git refspec 表示法并执行以下操作:

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

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.

这会强制将目标分支(由 ref 表示)更新到由+<object ref>部分表示的源提交。