git 如何丢弃本地更改并从 GitHub 存储库中提取最新版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38776517/
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 discard local changes and pull latest from GitHub repository
提问by Coding Duchess
I have a directory on my machine where I store all projects from GitHub. I opened one of them and made changes locally on my machine. Project got messed up and now I want to discard all the changes I made and pull the latest version from the repository. I am relatively new to GitHub, using Git Shell.
我的机器上有一个目录,用于存储 GitHub 中的所有项目。我打开了其中一个并在我的机器上本地进行了更改。项目搞砸了,现在我想放弃我所做的所有更改并从存储库中提取最新版本。我对 GitHub 比较陌生,使用 Git Shell。
Would git pull
command be sufficient?
Do I need to do anything extra to discard the changes made locally?
Can anyone help?
将git pull
命令是否足够呢?我是否需要做任何额外的事情来放弃本地所做的更改?任何人都可以帮忙吗?
回答by Cody
git reset is what you want, but I'm going to add a couple extra things you might find useful that the other answers didn't mention.
git reset 是您想要的,但我将添加一些其他答案未提及的您可能会觉得有用的额外内容。
git reset --hard HEAD
resets your changes back to the last commit that your local repo has tracked. If you made a commit, did not push it to GitHub, and want to throw that away too, see @absiddiqueLive's answer.
git reset --hard HEAD
将您的更改重置回本地存储库跟踪的最后一次提交。如果您进行了提交,但没有将其推送到 GitHub,并且也想将其丢弃,请参阅@absiddiqueLive的回答。
git clean -df
will discard any new files or directories that you may have added, in case you want to throw those away. If you haven't added any, you don't have to run this.
git clean -df
将丢弃您可能添加的任何新文件或目录,以防您想丢弃它们。如果您还没有添加任何内容,则不必运行它。
git pull
(or if you are using git shell with the GitHub client) git sync
will get the new changes from GitHub.
git pull
(或者,如果您在 GitHub 客户端上使用 git shell)git sync
将从GitHub获得新的更改。
Edit from way in the future:I updated my git shell the other week and noticed that the git sync
command is no longer defined by default. For the record, typing git sync
was equivalent to git pull && git push
in bash. I find it still helpful so it is in my bashrc.
从未来的方式进行编辑:我在前一周更新了我的 git shell,并注意到git sync
默认情况下不再定义该命令。作为记录,打字git sync
相当于git pull && git push
在 bash 中。我发现它仍然有用,所以它在我的 bashrc 中。
回答by absiddiqueLive
Run the below commands
运行以下命令
git log
From this you will get your last push commit hash key
从此您将获得最后一次推送提交哈希键
git reset --hard <your commit hash key>
回答by General_Twyckenham
If you already committed the changes than you would have to revert changes.
如果您已经提交了更改,则必须还原更改。
If you didn't commit yet, just do a clean checkout git checkout .
如果你还没有提交,就做一个干净的结帐 git checkout .
回答by ScottM
In addition to the above answers, there is always the scorched earth method.
除了上面的答案,还有焦土法。
rm -R <folder>
in Windows shell the command is:
在 Windows shell 中,命令是:
rd /s <folder>
Then you can just checkout the project again:
然后你可以再次签出项目:
git clone -v <repository URL>
This will definitely remove any local changes and pull the latest from the remote repository. Be careful with rm -R as it will delete your good data if you put the wrong path. For instance, definitely do notdo:
这肯定会删除任何本地更改并从远程存储库中提取最新版本。使用 rm -R 时要小心,因为如果您输入错误的路径,它会删除您的好数据。例如,绝对不要这样做:
rm -R /
edit: To fix spelling and add emphasis.
编辑:修复拼写并增加重点。
回答by Ronnie Royston
To push over old repo.
git push -u origin master --force
推动旧的回购。
git push -u origin master --force
I think the --force
would work for a pull as well.
我认为这--force
也适用于拉动。