git 如何执行“撤消挂起的更改”的 TFS 等效项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7999259/
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
How to perform the TFS-equivalent of 'Undo pending changes'
提问by Seb Nilsson
How do I perform the equivalent of the TFS 'Undo pending changes' in Git, on one or multiple files?
如何在 Git 中对一个或多个文件执行相当于 TFS 的“撤消挂起的更改”?
That basically means to do these steps:
这基本上意味着执行以下步骤:
- Undo changes on disk
- Resetting any changes Git has discovered
- Getting the latest changes on the file from Git
- 撤消磁盘上的更改
- 重置 Git 发现的任何更改
- 从 Git 获取文件的最新更改
It would be good to know the differences (if there are any) in commands for doing this if you've (1) just changed it on disk, without adding it, but also when you've (2) done the add-commandand for a bonus, (3) even when you have commit the change.
如果您(1) 只是在磁盘上更改了它而不添加它,并且当您(2) 完成添加命令时,那么了解执行此操作的命令中的差异(如果有的话)会很好并且作为奖励,(3) 即使您已提交更改。
回答by Adam Dymitruk
For 1 and 2, all you need to do is:
对于 1 和 2,您需要做的就是:
git stash -u #same effect as git reset --hard, but can be undone
this will throw away any changes. Be careful if you use reset
. Read up on manipulating the index and the permutations of the hard, soft and mixed options with the reset and checkout. The progit book explains this in detail: http://progit.org/2011/07/11/reset.html
这将丢弃任何更改。如果您使用reset
. 阅读如何通过重置和结帐操作索引和硬、软和混合选项的排列。progit 书详细解释了这一点:http://progit.org/2011/07/11/reset.html
For 3,
对于 3,
git reset --hard HEAD^
but would be better to issue a git stash -u
before this - just in case you have pending changes.
但最好git stash -u
在此之前发出- 以防万一您有待处理的更改。
This will reset the current branch to the parent of the current commit. Look up "tree-ish" online. ^ and ~N after a reference will allow you to point to any reachable points in the history of that reference. To understand how history is tracked in git, "Git for computer scientists" explains the Directed Acyclic Graph well: http://eagain.net/articles/git-for-computer-scientists/
这会将当前分支重置为当前提交的父分支。在线查找“树状”。引用后的 ^ 和 ~N 将允许您指向该引用历史记录中的任何可达点。要了解如何在 git 中跟踪历史,“计算机科学家的 Git”很好地解释了有向无环图:http: //eagain.net/articles/git-for-computer-scientists/
To get individual files from the state of the current commit (ie, throw away changes), you can use checkout
要从当前提交的状态中获取单个文件(即丢弃更改),您可以使用 checkout
git checkout HEAD -- <a list of files>
If you issued the last reset command above in error, you're not in trouble. Git keeps track of where the branches used to point in the reflog.
如果你错误地发出了上面的最后一个重置命令,你就没有问题。Git 会跟踪分支在引用日志中指向的位置。
git reflog
will list you the history. You can see in that output how to reference each, so:
会列出你的历史。您可以在该输出中看到如何引用每个,因此:
git reset --hard HEAD@{1}
will reset the branch to where it used to be 1 change before.
将分支重置为之前的 1 次更改。
To add, if you want to wipe ignored files and untracked files, you can wipe with this:
要添加,如果你想擦除被忽略的文件和未跟踪的文件,你可以用这个擦除:
git clean -xdf
回答by Manu
This command will undo local changes and restore them to the current versions in the repository:
此命令将撤消本地更改并将它们恢复到存储库中的当前版本:
git reset --hard
You can revert to your last valid commit by issuing:
您可以通过发出以下命令恢复到上次有效提交:
git reset --hard HEAD
If you just want to restore just one file, use git checkout instead:
如果您只想恢复一个文件,请改用 git checkout:
git checkout -- file_name.extension
git checkout HEAD file_name.extension
回答by Fred Foo
git checkout [path]
or (entire repo)git reset --hard HEAD
git reset [path]
followed bygit checkout [path]
git reset --hard [commit]
to restore the state of the repo at[commit]
, which must be a tree-ish
git checkout [path]
或(整个回购)git reset --hard HEAD
git reset [path]
其次是git checkout [path]
git reset --hard [commit]
恢复 repo 的状态[commit]
,它必须是树状的
回答by Henrique de Sousa
My equivalent to TFS undo
in Git with Eclipse is to simply right-click the file and select Replace with
-> HEAD Revision
(or whatever version you'd like).
我相当于TFS undo
在 Git 中使用 Eclipse 只需右键单击文件并选择Replace with
-> HEAD Revision
(或您喜欢的任何版本)。