Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

git - 从Git中的分支删除提交(Delete commits from a branch in Git)

I would like to know how to delete a commit.

(我想知道如何删除提交。)

By delete , I mean it is as if I didn't make that commit, and when I do a push in the future, my changes will not push to the remote branch.

(通过delete ,我的意思是好像我没有进行该提交,并且以后进行推送时,所做的更改将不会推送到远程分支。)

I read git help, and I think the command I should use is git reset --hard HEAD .

(我读了git help,我认为我应该使用的命令是git reset --hard HEAD 。)

Is this correct?

(这个对吗?)

  ask by hap497 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES .

(小心: git reset --hard 将删除您的工作目录更改 。)

Be sure to stash any local changes you want to keep before running this command.

(在运行此命令之前,请确保存储要保留的所有本地更改 。)

Assuming you are sitting on that commit, then this command will wack it...

(假设您正坐在该提交上,那么此命令将使其失效...)

git reset --hard HEAD~1

The HEAD~1 means the commit before head.

(HEAD~1表示在head之前提交。)

Or, you could look at the output of git log , find the commit id of the commit you want to back up to, and then do this:

(或者,您可以查看git log的输出,找到要备份到的提交的提交ID,然后执行以下操作:)

git reset --hard <sha1-commit-id>

If you already pushed it, you will need to do a force push to get rid of it...

(如果您已经推过它,则将需要用力推动才能摆脱它...)

git push origin HEAD --force

However , if others may have pulled it, then you would be better off starting a new branch.

(但是 ,如果其他人可能已经撤消了,那么您最好开始新的分支。)

Because when they pull, it will just merge it into their work, and you will get it pushed back up again.

(因为当他们拉动它时,它只会将其合并到他们的工作中,并且您会再次将其推回去。)

If you already pushed, it may be better to use git revert , to create a "mirror image" commit that will undo the changes.

(如果已经推送了,最好使用git revert创建一个“镜像”提交,以撤消更改。)

However, both commits will be in the log.

(但是,两个提交都将在日志中。)


FYI -- git reset --hard HEAD is great if you want to get rid of WORK IN PROGRESS.

(仅供参考-如果您想摆脱正在进行的工作,则git reset --hard HEAD非常git reset --hard HEAD 。)

It will reset you back to the most recent commit, and erase all the changes in your working tree and index.

(它会将您重置为最新提交,并清除工作树和索引中的所有更改。)


Lastly, if you need to find a commit that you "deleted", it is typically present in git reflog unless you have garbage collected your repository.

(最后,如果您需要查找“删除”的提交,除非您已垃圾回收存储库,否则它通常存在于git reflog 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...