如何将 Git 托管项目中的所有本地更改恢复到以前的状态?

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

How do I revert all local changes in Git managed project to previous state?

gitrevertgit-checkout

提问by Jacques René Mesrine

I have a project in which I ran git init. After several commits, I did git statuswhich told me everything was up to date and there were no local changes.

我有一个我运行的项目git init。经过几次提交后,我git status告诉我一切都是最新的,并且没有本地更改。

Then I made several consecutive changes and realized I wanted to throw everything away and get back to my original state. Will this command do it for me?

然后我连续做了几次改变,意识到我想扔掉所有东西,回到我原来的状态。这个命令会为我做吗?

git reset --hard HEAD

回答by 1800 INFORMATION

If you want to revert changes made to your working copy, do this:

如果要还原对工作副本所做的更改,请执行以下操作:

git checkout .

If you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:

如果您想恢复对索引所做的更改(即您添加的),请执行此操作。警告这会将您所有未推送的提交重置为 master!

git reset

If you want to revert a change that you have committed, do this:

如果要还原已提交的更改,请执行以下操作:

git revert <commit 1> <commit 2>

If you want to remove untracked files (e.g., new files, generated files):

如果要删除未跟踪的文件(例如,新文件、生成的文件):

git clean -f

Or untracked directories (e.g., new or automatically generated directories):

或未跟踪的目录(例如,新的或自动生成的目录):

git clean -fd

回答by Antony Stubbs

Note: You may also want to run

注意:您可能还想运行

git clean -fd

as

作为

git reset --hard

will notremove untracked files, where as git-clean will remove any files from the tracked root directory that are not under git tracking. WARNING - BE CAREFUL WITH THIS! It is helpful to run a dry-run with git-clean first, to see what it will delete.

删除未跟踪文件,其中为git的清洁会从跟踪的根目录不在下git的跟踪中删除任何文件。警告 - 小心!先用 git-clean 运行一次试运行,看看它会删除什么是有帮助的。

This is also especially useful when you get the error message

当您收到错误消息时,这也特别有用

~"performing this command will cause an un-tracked file to be overwritten"

Which can occur when doing several things, one being updating a working copy when you and your friend have both added a new file of the same name, but he's committed it into source control first, and you don't care about deleting your untracked copy.

这可能在做几件事时发生,其中一个是当您和您的朋友都添加了一个同名的新文件时更新工作副本,但他首先将其提交到源代码管理中,而您不在乎删除未跟踪的副本.

In this situation, doing a dry run will also help show you a list of files that would be overwritten.

在这种情况下,进行试运行还有助于向您显示将被覆盖的文件列表。

回答by William Entriken

Re-clone

重新克隆

GIT=$(git rev-parse --show-toplevel)
cd $GIT/..
rm -rf $GIT
git clone ...
  • ? Deletes local, non-pushed commits
  • ? Reverts changes you made to tracked files
  • ? Restores tracked files you deleted
  • ? Deletes files/dirs listed in .gitignore(like build files)
  • ? Deletes files/dirs that are not tracked and not in .gitignore
  • You won't forget this approach
  • Wastes bandwidth
  • ? 删除本地的、非推送的提交
  • ? 还原您对跟踪文件所做的更改
  • ? 恢复您删除的跟踪文件
  • ? 删除中列出的文件/目录.gitignore(如构建文件)
  • ? 删除未跟踪且不在的文件/目录.gitignore
  • 你不会忘记这个方法
  • 浪费带宽


Following are other commands I forget daily.

以下是我每天都忘记的其他命令。

Clean and reset

清洁和重置

git clean --force -d -x
git reset --hard
  • ? Deletes local, non-pushed commits
  • ? Reverts changes you made to tracked files
  • ? Restores tracked files you deleted
  • ? Deletes files/dirs listed in .gitignore(like build files)
  • ? Deletes files/dirs that are not tracked and not in .gitignore
  • ? 删除本地的、非推送的提交
  • ? 还原您对跟踪文件所做的更改
  • ? 恢复您删除的跟踪文件
  • ? 删除中列出的文件/目录.gitignore(如构建文件)
  • ? 删除未跟踪且不在的文件/目录.gitignore

Clean

干净的

git clean --force -d -x
  • ? Deletes local, non-pushed commits
  • ? Reverts changes you made to tracked files
  • ? Restores tracked files you deleted
  • ? Deletes files/dirs listed in .gitignore(like build files)
  • ? Deletes files/dirs that are not tracked and not in .gitignore
  • ? 删除本地的、非推送的提交
  • ? 还原您对跟踪文件所做的更改
  • ? 恢复您删除的跟踪文件
  • ? 删除中列出的文件/目录.gitignore(如构建文件)
  • ? 删除未跟踪且不在的文件/目录.gitignore

Reset

重启

git reset --hard
  • ? Deletes local, non-pushed commits
  • ? Reverts changes you made to tracked files
  • ? Restores tracked files you deleted
  • ? Deletes files/dirs listed in .gitignore(like build files)
  • ? Deletes files/dirs that are not tracked and not in .gitignore
  • ? 删除本地的、非推送的提交
  • ? 还原您对跟踪文件所做的更改
  • ? 恢复您删除的跟踪文件
  • ? 删除中列出的文件/目录.gitignore(如构建文件)
  • ? 删除未跟踪且不在的文件/目录.gitignore


Notes

笔记

Test case for confirming all the above (use bash or sh):

用于确认上述所有内容的测试用例(使用 bash 或 sh):

mkdir project
cd project
git init
echo '*.built' > .gitignore
echo 'CODE' > a.sourceCode
mkdir b
echo 'CODE' > b/b.sourceCode
cp -r b c
git add .
git commit -m 'Initial checkin'
echo 'NEW FEATURE' >> a.sourceCode
cp a.sourceCode a.built
rm -rf c
echo 'CODE' > 'd.sourceCode'

See also

也可以看看

  • git revertto make new commits that undo prior commits
  • git checkoutto go back in time to prior commits (may require running above commands first)
  • git stashsame as git resetabove, but you can undo it
  • git revert进行新的提交以撤销先前的提交
  • git checkout及时返回到之前的提交(可能需要先运行上面的命令)
  • git stashgit reset上面相同,但您可以撤消它

回答by Michael Durrant

If you want to revert all changes AND be up-to-date with the current remote master (for example you find that the master HEAD has moved forward since you branched off it and your push is being 'rejected') you can use

如果您想恢复所有更改并与当前远程主服务器保持同步(例如,您发现主 HEAD 自从您分支以来已经向前移动并且您的推送被“拒绝”),您可以使用

git fetch  # will fetch the latest changes on the remote
git reset --hard origin/master # will set your local branch to match the representation of the remote just pulled down.

回答by William Pursell

Look into git-reflog. It will list all the states it remembers (default is 30 days), and you can simply checkout the one you want. For example:

查看 git-reflog。它将列出它记住的所有状态(默认为 30 天),您只需签出您想要的状态即可。例如:

$ git init > /dev/null
$ touch a
$ git add .
$ git commit -m"Add file a" > /dev/null
$ echo 'foo' >> a
$ git commit -a -m"Append foo to a" > /dev/null
$ for i in b c d e; do echo $i >>a; git commit -a -m"Append $i to a" ;done > /dev/null
$ git reset --hard HEAD^^ > /dev/null
$ cat a
foo
b
c
$ git reflog
145c322 HEAD@{0}: HEAD^^: updating HEAD
ae7c2b3 HEAD@{1}: commit: Append e to a
fdf2c5e HEAD@{2}: commit: Append d to a
145c322 HEAD@{3}: commit: Append c to a
363e22a HEAD@{4}: commit: Append b to a
fa26c43 HEAD@{5}: commit: Append foo to a
0a392a5 HEAD@{6}: commit (initial): Add file a
$ git reset --hard HEAD@{2}
HEAD is now at fdf2c5e Append d to a
$ cat a
foo
b
c
d

回答by Tobias Gassmann

DANGER AHEAD: (please read the comments. Executing the command proposed in my answer might delete more than you want)

前方危险:(请阅读评论。执行我的答案中建议的命令可能会删除比您想要的更多的内容)

to completely remove all files including directories I had to run

完全删除所有文件,包括我必须运行的目录

git clean -f -d

回答by Scott Davey

After reading a bunch of answers and trying them, I've found various edge cases that mean sometimes they don't fully clean the working copy.

在阅读了一堆答案并尝试之后,我发现了各种边缘情况,这意味着有时它们不能完全清理工作副本。

Here's my current bash script for doing it, which works all the time.

这是我当前用于执行此操作的 bash 脚本,它一直有效。

#!/bin/sh
git reset --hard
git clean -f -d
git checkout -- HEAD

Run from working copy root directory.

从工作副本根目录运行。

回答by piyushmandovra

simply execute -

只需执行 -

git stash

it will remove all your local changes. and you can also use it later by executing -

它将删除您所有的本地更改。您也可以稍后通过执行来使用它 -

git stash apply 

回答by Patrick

I met a similar problem. The solution is to use git logto look up which version of the local commit is different from the remote. (E.g. the version is 3c74a11530697214cbcc4b7b98bf7a65952a34ec).

我遇到了类似的问题。解决方案是使用git log查找本地提交的哪个版本与远程提交不同。(例如版本是3c74a11530697214cbcc4b7b98bf7a65952a34ec)。

Then use git reset --hard 3c74a11530697214cbcc4b7b98bf7a65952a34ecto revert the change.

然后用于git reset --hard 3c74a11530697214cbcc4b7b98bf7a65952a34ec恢复更改。

回答by Manohar Reddy Poreddy

I searched for a similar issue,

我搜索了一个类似的问题,

Wanted to throw away local commits:

想扔掉本地提交:

  1. cloned the repository (git clone)
  2. switched to dev branch (git checkout dev)
  3. did few commits (git commit -m "commit 1")
  4. but decided to throw away these local commits to go back to remote (origin/dev)
  1. 克隆存储库 (git clone)
  2. 切换到开发分支(git checkout dev)
  3. 提交很少(git commit -m "commit 1")
  4. 但决定扔掉这些本地提交回到远程(来源/开发)

So did the below:

以下也是如此:

git reset --hard origin/dev

Check:

查看:

git status  

        On branch dev  
        Your branch is up-to-date with 'origin/dev'.  
        nothing to commit, working tree clean  

now local commits are lost, back to the initial cloned state, point 1 above.

现在本地提交丢失,回到最初的克隆状态,上面的第 1 点。