Git:如何回滚到上次推送/提交

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

Git: how to roll back to last push/commit

gitversion-control

提问by Mikey

I am programming. I add beautiful code and commit and push it to my repo like:

我在编程。我添加漂亮的代码并提交并将其推送到我的仓库,例如:

git add *
git commit
//writes message
git push unfuddle master

Now i go in and screw everything up. I have not issued a git command since I push the beauty. How do i make my local files go back to what was committed?

现在我进去把一切都搞砸了。推美之后就没发出过git命令。如何让我的本地文件恢复到提交的内容?

git pullsays my local repo is "up to date"

git pull说我的本地仓库是“最新的”

git checkoutspams out my screen and doesnt seem to change anything.

git checkout我的屏幕垃圾邮件,似乎没有改变任何东西。

To be clear: I am not trying to modify my remote repo, just get my local repo to look like the server.

需要明确的是:我不是要修改我的远程仓库,只是让我的本地仓库看起来像服务器。

My current solution is to delete the folder and reclone the repo. That can't be right.

我目前的解决方案是删除文件夹并重新克隆存储库。那不可能是对的。

回答by three

You can reset to HEAD: git reset --hard HEAD

您可以重置为 HEAD: git reset --hard HEAD

回答by Adam Dymitruk

UPDATE:

更新:

The best way to do this is to

做到这一点的最好方法是

git stash -u

Do not

不要

git reset --hard HEAD

it is an operation that cannot be undone and you can truly nuke your work unless you're using time machine on a Mac or the new backup service on Windows 8. Linux may have a scheme like that although I don't use one.

这是一项无法撤消的操作,除非您在 Mac 上使用时间机器或在 Windows 8 上使用新的备份服务,否则您可以真正破坏您的工作。Linux 可能有这样的方案,尽管我不使用。

If you also want ignored files nuked, you can

如果您还想要被忽略的文件,您可以

git clean -xdf

回答by simont

git pull says my local repo is "up to date"

git pull 说我的本地仓库是“最新的”

git pullis telling you your repository is up to date because your local repository and your remote repository both point to the same HEAD (the last commit). This is due to your git push, which synced the remotewith your localrepository.

git pull告诉您您的存储库是最新的,因为您的本地存储库和远程存储库都指向同一个 HEAD(最后一次提交)。这是由于您的git push,它将远程与您的本地存储库同步。

Git doesn't compare the changes that haven't been committed to the remote branch when it decides what to pull; thus, from Gits point of view, your localand remoterepositories are at the same point in time, even though your localrepository has unstaged changes (changes that you have not git added).

Git 在决定拉取什么时不会比较尚未提交到远程分支的更改;因此,从 Git 的角度来看,您的本地远程存储库在同一时间点,即使您的本地存储库具有未暂存的更改(您尚未git add编辑的更改)。

There's a nice diagram on the bottom of this pagewhich shows you how the process works - you make some changes, stage them (by running git add), and then finally commit them to the repository (through the creatively named git commit).

此页面底部有一个很好的图表,它向您展示了该过程是如何工作的 - 您进行一些更改,将它们暂存(通过运行git add),然后最后将它们提交到存储库(通过创造性命名的git commit)。

To be clear: I am not trying to modify my remote repo, just get my local repo to look like the server.

需要明确的是:我不是要修改我的远程仓库,只是让我的本地仓库看起来像服务器。

Others have pointed out the methods to do this: git reset --hard HEAD(reset the index [staged files] and the working tree [unstaged files] to the last commit), but it's worth having an understanding of what you're trying to achieve - if I'd known howgit was tracking my files the first time I messed up my working tree, I'd have saved hours of fretting.

其他人指出了执行此操作的方法:( git reset --hard HEAD将索引 [暂存文件] 和工作树 [未暂存文件] 重置为最后一次提交),但值得了解您要实现的目标 - 如果我' d知道如何混帐正在跟踪我的文件,我第一次搞砸了我的工作树,我救了微动小时。

回答by ralphtheninja

Since you haven't committed anything yet, the following command will reset your modified files:

由于您尚未提交任何内容,因此以下命令将重置您修改的文件:

git checkout .

回答by tonny zhang

you can try

你可以试试

git stash
git pull origin your-branch

回答by Rodrigo Flores

If you does not have committed anything, you can simply do a $ git reset --hard. If you have committed some stuff, you can do a git reset --hard origin/masterto go back to the version that is on the remote.

如果你没有提交任何东西,你可以简单地做一个$ git reset --hard. 如果你提交了一些东西,你可以做一个git reset --hard origin/master回到遥控器上的版本。

回答by jjlin

Did you try git checkout -f master?

你试了git checkout -f master吗?

回答by sjcoder

git checkout branchname git reset --hard c4e0424 git push origin +branchname

git checkout branchname git reset --hard c4e0424 git push origin +branchname

You can use --soft instead of --hard
--hardyou will LOSE your changes
--softleave your changed files of commit

您可以使用 --soft 而不是 --hard
--hard您将丢失您的更改,并
--soft保留已更改的提交文件