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

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

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

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 (ie, that you have added), do this. Warning this will reset all of your unpushed commits to 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 (eg, new files, generated files):

git clean -f

Or untracked directories (eg, new or automatically generated directories):

git clean -fd

Note: You may also want to run

git clean -fd

as

git reset --hard

will not remove 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.

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.


如果您想恢复所有更改并且与当前的远程主服务器保持最新状态(例如,您发现主服务器主机已经向前移动,因为您已将其分支并且您的推送被“拒绝”),您可以使用

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.
链接地址: http://www.djcxy.com/p/3276.html

上一篇: 以下未跟踪的工作树文件将被checkout覆盖

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