简体   繁体   English

如何使用git撤消挂起的更改?

[英]How to undo pending changes with git?

I am new to git. 我是git的新手。 I have extensive experience with centralized CVS like TFS and SourceGear Vault. 我在集中式CVS(如TFS和SourceGear Vault)方面拥有丰富的经验。

I am using git locally (no remotes). 我在本地使用git(没有遥控器)。 Today I have made a complete mess of my projects when trying to revert back all changes made since last commit. 今天,当我试图恢复自上次提交以来所做的所有更改时,我已经完成了我的项目。

My repository stores documentation, VS projects and some other dev artifacts. 我的存储库存储文档,VS项目和一些其他开发工件。 There are several thousand files in total. 总共有几千个文件。

How do I revert back to the last commit ( undo pending changes speaking TFS) for entire repository with git? 如何使用git恢复整个存储库的最后一次提交( 撤销说TFS的更改 )? git reset and git checkout just messed my files so I have had to use my file-level backups. git resetgit checkout刚搞砸了我的文件,所以我不得不使用我的文件级备份。

By undoing changes I mean: 通过撤消更改我的意思是:

  • revert files edited since last commit 恢复自上次提交以来编辑的文件
  • add files removed since last commit 添加自上次提交后删除的文件
  • delete files added since last commit 删除自上次提交后添加的文件

Items listed in .gitignore files should not be affected. .gitignore文件中列出的项目不应受到影响。

Is it possible to do such a revert on the folder (+subfolders) level instead of repository level? 是否可以对文件夹(+子文件夹)级别而不是存储库级别进行此类还原?

With Vault I can label a folder(+subfolders) and later on I can go back to that labeled state. 使用Vault我可以标记一个文件夹(+子文件夹),稍后我可以回到那个标记状态。 Can this be done with git? 这可以用git完成吗?

Yet another simple question – can I view deleted files in a folder and restore them as needed (from some older commits)? 还有一个简单的问题 - 我可以查看文件夹中已删除的文件并根据需要恢复它们(从一些较旧的提交中恢复)吗? I have not managed to do this with git state or git log . 我没有设法用git stategit log做到这一点。

How do I revert back to the last commit (undo pending changes speaking TFS) for entire repository with git? 如何使用git恢复整个存储库的最后一次提交(撤销说TFS的更改)? git reset and git checkout just messed my files so I have had to use my file-level backups. git reset和git checkout刚搞砸了我的文件,所以我不得不使用我的文件级备份。

An alternative would be git checkout , see the documentation : git-checkout - Switch branches or restore working tree files . 另一种方法是git checkout ,请参阅文档git-checkout - 切换分支或恢复工作树文件 For example: git checkout -- . 例如: git checkout -- . checks out all files from the current branch, hence restores their state. 检出当前分支中的所有文件,从而恢复其状态。

Is it possible to do such a revert on the folder (+subfolders) level instead of repository level? 是否可以对文件夹(+子文件夹)级别而不是存储库级别进行此类还原?

Yes, git checkout -- path/to/folder . 是的, git checkout -- path/to/folder

With Vault I can label a folder(+subfolders) and later on I can go back to that labeled state. 使用Vault我可以标记一个文件夹(+子文件夹),稍后我可以回到那个标记状态。 Can this be done with git? 这可以用git完成吗?

I only know of tags on a repository-basis. 我只知道基于存储库的标签 However, you can checkout a folder for that particular tag (or commit): git checkout tags/v1.0.3 -- path or git checkout commit -- path . 但是,您可以签出该特定标记(或提交)的文件夹: git checkout tags/v1.0.3 -- pathgit checkout commit -- path

can I view deleted files in a folder and restore them as needed (from some older commits)? 我可以查看文件夹中已删除的文件并根据需要(从一些较旧的提交)恢复它们吗? I have not managed to do this with git state or git log. 我没有设法用git state或git log做到这一点。

For example, use git log --name-status . 例如,使用git log --name-status Say I deleted README.md in some commit, it will show as D README.md . 假设我在某些提交中删除了README.md ,它将显示为D README.md Assume it was deleted in commit abc123 , then abc123~1 is the commit before that (where the README.md still existed). 假设它在commit abc123中被删除,那么abc123~1就是之前的提交(其中README.md仍然存在)。 You then can use git show abc123~1:README.md to see the contents of that file in the commit before it was deleted. 然后,您可以使用git show abc123~1:README.md在删除之前在提交中查看该文件的内容。 A simple way to restore it: git show 5235de78d28a07d2459130e7e3041b7720563b5d~1:README.md > README.md . 恢复它的简单方法: git show 5235de78d28a07d2459130e7e3041b7720563b5d~1:README.md > README.md

can I view deleted files in a folder ... 我可以查看文件夹中已删除的文件 ...

git log --name-status -- path/to/folder

git reset --hard HEAD

Should do the trick. 应该做的伎俩。 This command will reset all of your files to how they were in the last commit you made. 此命令会将您的所有文件重置为上次提交时的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM