git 撤销所有未提交或未保存的更改

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

git undo all uncommitted or unsaved changes

gitcommand-lineundogit-reset

提问by Antarr Byrd

I'm trying to undo all changes since my last commit. I tried git reset --hardand git reset --hard HEADafter viewing this post. I responds with head is now at 18c3773... but when I look at my local source all the files are still there. What am I missing?

我正在尝试撤消自上次提交以来的所有更改。我试着git reset --hardgit reset --hard HEAD观察后,这个职位。我的回答是 head 现在在 18c3773...但是当我查看本地源时,所有文件都还在那里。我错过了什么?

回答by mvp

  • This will unstage all files you might have staged with git add:

    git reset
    
  • This will revert all local uncommitted changes (should be executed in repo root):

    git checkout .
    

    You can also revert uncommitted changes only to particular file or directory:

    git checkout [some_dir|file.txt]
    

    Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):

    git reset --hard HEAD
    
  • This will remove all local untracked files, so onlygit tracked files remain:

    git clean -fdx
    

    WARNING:-xwill also remove all ignored files, including ones specified by .gitignore! You may want to use -nfor preview of files to be deleted.

  • 这将取消暂存您可能已暂存的所有文件git add

    git reset
    
  • 这将恢复所有本地未提交的更改(应在 repo root 中执行):

    git checkout .
    

    您还可以仅将未提交的更改还原到特定文件或目录:

    git checkout [some_dir|file.txt]
    

    恢复所有未提交更改的另一种方法(键入时间更长,但适用于任何子目录):

    git reset --hard HEAD
    
  • 这将删除所有本地未跟踪的文件,因此保留 git 跟踪的文件:

    git clean -fdx
    

    警告:-x还将删除所有被忽略的文件,包括由.gitignore! 您可能希望-n用于预览要删除的文件。



To sum it up: executing commands below is basically equivalent to fresh git clonefrom original source (but it does not re-download anything, so is much faster):

总结一下:执行下面的命令基本上等同于git clone从原始来源新鲜(但它不会重新下载任何东西,因此速度要快得多):

git reset
git checkout .
git clean -fdx

Typical usage for this would be in build scripts, when you must make sure that your tree is absolutely clean - does not have any modifications or locally created object files or build artefacts, and you want to make it work very fast and to not re-clone whole repository every single time.

典型用法是在构建脚本中,当您必须确保您的树绝对干净 - 没有任何修改或本地创建的目标文件或构建工件,并且您希望使其工作得非常快并且不重新 -每次都克隆整个存储库。

回答by Abram

If you wish to "undo" all uncommitted changes simply run:

如果您希望“撤消”所有未提交的更改,只需运行:

git stash
git stash drop

If you have any untracked files (check by running git status), these may be removed by running:

如果您有任何未跟踪的文件(通过运行检查git status),可以通过运行来删除这些文件:

git clean -fdx

git stashcreates a new stash which will become stash@{0}. If you wish to check first you can run git stash listto see a list of your stashes. It will look something like:

git stash创建一个新的 stash,它将成为stash@{0}。如果您想先检查,您可以运行git stash list查看您的藏品列表。它看起来像:

stash@{0}: WIP on rails-4: 66c8407 remove forem residuals
stash@{1}: WIP on master: 2b8f269 Map qualifications
stash@{2}: WIP on master: 27a7e54 Use non-dynamic finders
stash@{3}: WIP on blogit: c9bd270 some changes

Each stash is named after the previous commit messsage.

每个 stash 都以前一个提交消息命名。

回答by keshav

there is also git stash- which "stashes" your local changes and can be reapplied at a later time or dropped if is no longer required

还有git stash- 它“隐藏”了您的本地更改,可以在以后重新应用或在不再需要时删除

more info on stashing

有关藏匿的更多信息

回答by user1872384

I'm using source tree.... You can do revert all uncommitted changes with 2 easy steps:

我正在使用源代码树......您可以通过两个简单的步骤还原所有未提交的更改:

1) just need to reset the workspace file status

1)只需要重置工作区文件状态

enter image description here2) select all unstage files (command +a), right click and select remove

在此处输入图片说明2)选择所有取消暂存的文件(命令+a),右键单击并选择删除

enter image description here

在此处输入图片说明

It's that simple :D

就是这么简单:D

回答by Ralph

What I do is

我做的是

git add . (adding everything)
git stash 
git stash drop

One liner: git add . && git stash && git stash drop

一个班轮: git add . && git stash && git stash drop

回答by Abdul Rahman K

For those who reached here searching if they could undo git clean -f -d, by which a file created in eclipsewas deleted,

对于那些到达这里搜索是否可以撤消的人git clean -f -d,通过该撤消删除了在eclipse中创建的文件,

You can do the same from the UI using "restore from local history" for ref:Restore from local history

您可以在 UI 中使用“从本地历史还原”来执行相同的操作:从本地历史还原

回答by Zii

States transitioning from one commit to new commit

从一次提交过渡到新提交的状态

0. last commit,i.e. HEAD commit
1. Working tree changes, file/directory deletion,adding,modification.
2. The changes are staged in index
3. Staged changes are committed

Action for state transitioning

状态转换操作

0->1: manual file/directory operation
1->2: git add .
2->3: git commit -m "xxx"

Check diff

检查差异

0->1: git diff
0->2: git diff --cached
0->1, and 0->2: git diff HEAD
last last commit->last commit: git diff HEAD^ HEAD

Revert to last commit

恢复到上次提交

2->1: git reset
1->0: git checkout .     #only for tracked files/directories(actions include modifying/deleting tracked files/directories)
1->0: git clean -fdx     #only for untracked files/directories(action includes adding new files/directories)
2->1, and 1->0: git reset --hard HEAD

Equivalent of git clone, without re-downloading anything

相当于 git clone,无需重新下载任何东西

git reset && git checkout . && git clean -fdx