Git:撤消本地更改;git 添加。+ git rm?

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

Git: Undo local changes; git add . + git rm?

gitgit-resetgit-addgit-rm

提问by user979672

Need help figuring out a couple common workflows with Github. I come from a VS TFS background, so forgive me.

需要帮助弄清楚 Github 的几个常见工作流程。我来自 VS TFS 背景,所以请原谅我。

Undoing Pending Changes

撤消挂起的更改

Let's say I have cloned of a git repository to my local file system. At this point, the project's local files match exactly what's in the remote repoistory.

假设我已将 git 存储库克隆到我的本地文件系统。此时,项目的本地文件与远程存储库中的内容完全匹配。

Then I decided to make some changes to the code, and change the local versions of a couple files. After doing some testing, I figure out that I want to discard my local changes and revert the local files back to what they are in the remote repoistory.

然后我决定对代码进行一些更改,并更改几个文件的本地版本。在做了一些测试之后,我发现我想放弃我的本地更改并将本地文件恢复到它们在远程存储库中的内容。

How do I undo these local changes, restoring them to the current versions in the repository?

如何撤消这些本地更改,将它们恢复到存储库中的当前版本?

Committing all Changes

提交所有更改

Whenever I modify the contents of local files in my repository clone, or add new files, and want to push the changes, I issue "git add .", "git commit" with my comments, then "git push" to my master.

每当我在我的存储库克隆中修改本地文件的内容,或添加新文件,并想要推送更改时,我都会发出“git add .”、“git commit”和我的评论,然后“git push”给我的主人。

However, when I delete a file locally that's tracked in the repository, "git add ." doesn't capture the rm changes. Instead, I have to "git rm [filename]" before I "git commit" to update the repository. I always forget to do this though.

但是,当我在本地删除存储库中跟踪的文件时,“git add”。不捕获 rm 更改。相反,我必须在“git commit”更新存储库之前“git rm [filename]”。我总是忘记这样做。

Is there a git command that will "git add ." and "git rm" any files I've deleted locally, in one step? After modifying local files and deleting a couple, I'd like to issue just one command that captures all my changes before I "git commit".

是否有一个 git 命令可以“git add”。和“git rm”我在本地删除的任何文件,一步?在修改了本地文件并删除了几个文件之后,我想在“git commit”之前发出一个捕获我所有更改的命令。

回答by manojlds

How do I undo these local changes, restoring them to the current versions in the repository?`

如何撤消这些本地更改,将它们恢复到存储库中的当前版本?`

git reset --hard

(this will reset your index and working directory to HEAD)

(这会将您的索引和工作目录重置为 HEAD)

Is there a git command that will git add .and git rmany files I've deleted locally, in one step?`

是否有一个Git命令,将git add .git rm我本地删除任何文件,一步到位?`

git add -u

(it wont add new files)

(它不会添加新文件)

If you want to add new files, remove rm'ed files, and stage modifications to files:

如果要添加新文件,删除rm'ed 文件,并暂存对文件的修改:

git add -A

回答by p4010

You can revert to your last valid commit (i.e. copy of the remote repo, in tour example) by issuing:

您可以通过发出以下命令恢复到上次有效提交(即远程存储库的副本,在游览示例中):

git reset --hard HEAD

Additionally, I suggest reading the useful http://progit.org/2011/07/11/reset.htmlpage to better understand how reset work and what the index is, and why git add and git rm are separate commands.

此外,我建议阅读有用的http://progit.org/2011/07/11/reset.html页面以更好地了解重置的工作原理和索引是什么,以及为什么 git add 和 git rm 是单独的命令。