简体   繁体   English

如何撤消 Git 中的本地提交?

[英]How do I undo the local commits in Git?

I accidentally committed the wrong files to Git , but didn't push the commit to the server yet.我不小心将错误的文件提交到 Git ,但还没有将提交推送到服务器。

How can I undo those commits from the local repository?如何从本地存储库撤消这些提交?

The only way seems to be to copy the edits in some kind of GUI text editor, then wipe the whole local clone, then re-clone the repository, then re-applying the edits.唯一的方法似乎是在某种 GUI 文本编辑器中复制编辑,然后擦除整个本地克隆,然后重新克隆存储库,然后重新应用编辑。 However,然而,

  • This can cause data loss.这可能会导致数据丢失。
  • It's very hard to do this when only an accidental git commit was run.当只运行一个意外的 git 提交时,很难做到这一点。
  • Is there a better way?有没有更好的办法?

Firstly, use the command git log to see all the commits.首先,使用命令 git 日志查看所有提交。

git log

Copy the commit ID which you want to remove and then use,复制您要删除然后使用的提交 ID,

git revert <commit-id>

to undo the changes.撤消更改。

The command git reset will also help.命令git reset也将有所帮助。 https://git-scm.com/docs/git-reset https://git-scm.com/docs/git-reset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM