在 git 中最后一次提交后重置所有更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630312/
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:57  来源:igfitidea点击:

Reset all changes after last commit in git

gitgit-commitgit-resetgit-revert

提问by Dogbert

How can I undo everychange made to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?

如何撤消上次提交后对目录所做的所有更改,包括删除添加的文件、重置修改过的文件和添加回已删除的文件?

回答by Benjamin Bannier

First reset the changes

首先重置更改

git reset HEAD --hard

then clean out everything untracked. If you want to keep files that are not tracked due to .gitignore, be careful with this command.

然后清除所有未跟踪的内容。如果要保留由于 未跟踪的文件,请.gitignore小心使用此命令。

git clean -fd

回答by Ortomala Lokni

How can I undo every changemade to my directory after the last commit, including deleting added files, resetting modified files, and adding back deleted files?

如何撤消上次提交后对目录所做的所有更改,包括删除添加的文件、重置修改过的文件和添加回已删除的文件?

  1. You can undo changes to trackedfiles with:

    git reset HEAD --hard
    
  2. You can remove untrackedfiles with:

    git clean -f
    
  3. You can remove untrackedfiles and directories with:

    git clean -fd
    

    but you can't undo change to untracked files.

  4. You can remove ignored and untrackedfiles and directories

    git clean -fdx
    

    but you can't undo change to ignored files.

  1. 您可以使用以下命令撤消对跟踪文件的更改:

    git reset HEAD --hard
    
  2. 您可以使用以下方法删除未跟踪的文件:

    git clean -f
    
  3. 您可以使用以下命令删除未跟踪的文件和目录:

    git clean -fd
    

    您无法撤消对未跟踪文件的更改

  4. 您可以删除被忽略和未跟踪的文件和目录

    git clean -fdx
    

    您无法撤消对忽略文件的更改

You can also set clean.requireForceto false:

您还可以设置clean.requireForcefalse

git config --global --add clean.requireForce false

to avoid using -f(--force) when you use git clean.

避免在使用时使用-f( --force) git clean

回答by RKS

There are two commands which will work in this situation,

有两个命令可以在这种情况下工作,

root>git reset --hard HEAD~1

root>git reset --hard HEAD~1

root>git push -f

根>git push -f

For more git commands refer this page

有关更多 git 命令,请参阅此页面