Git - 撤消推送提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22682870/
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
Git - Undo pushed commits
提问by Manolo
I have a project in a remote repository, synchronized with a local repository (development) and the server one (prod). I've been making some commited changes already pushed to remote and pulled from the server. Now, I want to undo those changes. So I could just git checkout
to the commit before the changes and commit the new changes, but I'm guessing that there will be problems to push them again to remote. Any suggestion on how should I proceed?
我在远程存储库中有一个项目,与本地存储库(开发)和服务器(生产)同步。我一直在进行一些已提交的更改,这些更改已经推送到远程并从服务器中提取。现在,我想撤消这些更改。所以我可以git checkout
在更改之前提交并提交新的更改,但我猜测将它们再次推送到远程会有问题。关于我应该如何进行的任何建议?
回答by gitaarik
You can revert individual commits with:
您可以通过以下方式还原单个提交:
git revert <commit_hash>
This will create a new commit which reverts the changes of the commit you specified. Note that it only reverts that specific commit, and not commits after that. If you want to revert a range of commits, you can do it like this:
这将创建一个新提交,该提交还原您指定的提交的更改。请注意,它仅还原该特定提交,之后不会提交。如果你想恢复一系列提交,你可以这样做:
git revert <oldest_commit_hash>..<latest_commit_hash>
It reverts the commits between and including the specified commits.
它恢复指定提交之间并包括指定提交的提交。
Look at the git-revert man pagefor more information about the git revert
command. Also look at this answerfor more information about reverting commits.
查看git-revert 手册页以获取有关该git revert
命令的更多信息。另请查看此答案以获取有关还原提交的更多信息。
回答by jo_
A solution that keeps no traces of the "undo".
一种不保留“撤消”痕迹的解决方案。
NOTE: don't do this if someone allready pulled your change (I would use this only on my personal repo)
注意:如果有人已经取消了您的更改,请不要这样做(我只会在我的个人仓库中使用它)
do:
做:
git reset <previous label or sha1>
this will re-checkout all the updates locally (so git status will list all updated files)
这将在本地重新检出所有更新(因此 git status 将列出所有更新的文件)
then you "do your work" and re-commit your changes (Note: this step is optional)
然后你“做你的工作”并重新提交你的更改(注意:这一步是可选的)
git commit -am "blabla"
At this moment your local tree differs from the remote
此时您的本地树与远程树不同
git push -f <remote-name> <branch-name>
will push and force remote to consider this push and remove the previous one (specifying remote-name and branch-name is not mandatory but is recommended to avoid updating all branches with update flag).
将推送并强制远程考虑此推送并删除前一个(指定 remote-name 和 branch-name 不是强制性的,但建议避免使用更新标志更新所有分支)。
!! watch-out some tags may still be pointing removed commit !! how-to-delete-a-remote-tag
!! 注意一些标签可能仍然指向已删除的提交!!如何删除远程标签
回答by fedorqui 'SO stop harming'
What I do in these cases is:
我在这些情况下做的是:
In the server, move the cursor back to the last known good commit:
git push -f origin <last_known_good_commit>:<branch_name>
Locally, do the same:
git reset --hard <last_known_good_commit> # ^^^^^^ # optional
在服务器中,将光标移回上次已知的良好提交:
git push -f origin <last_known_good_commit>:<branch_name>
在本地,做同样的事情:
git reset --hard <last_known_good_commit> # ^^^^^^ # optional
See a full example on a branch my_new_branch
that I created for this purpose:
请参阅my_new_branch
我为此目的创建的分支上的完整示例:
$ git branch
my_new_branch
This is the recent history after adding some stuff to myfile.py
:
这是添加一些东西后的最近历史myfile.py
:
$ git log
commit 80143bcaaca77963a47c211a9cbe664d5448d546
Author: me
Date: Wed Mar 23 12:48:03 2016 +0100
Adding new stuff in myfile.py
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
I want to get rid of the last commit, which was already pushed, so I run:
我想摆脱已经推送的最后一次提交,所以我运行:
$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To [email protected]:me/myrepo.git
+ 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)
Nice! Now I see the file that was changed on that commit (myfile.py
) shows in "not staged for commit":
好的!现在,我看到在该提交 ( myfile.py
) 中更改的文件显示在“未暂存以进行提交”中:
$ git status
On branch my_new_branch
Your branch is up-to-date with 'origin/my_new_branch'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: myfile.py
no changes added to commit (use "git add" and/or "git commit -a")
Since I don't want these changes, I just move the cursor back locally as well:
由于我不想要这些更改,因此我也将光标移回本地:
$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit
So now HEAD is in the previous commit, both in local and remote:
所以现在 HEAD 在之前的提交中,在本地和远程:
$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
回答by vibs2006
You can REVERT (or you can also call it DELETE) the Git Commit BOTH Locally and Remotelyif you follow the steps as given below via git command line.
如果您通过 git 命令行按照下面给出的步骤操作,您可以 REVERT(或者您也可以将其称为DELETE)本地和远程Git 提交。
Run the following command to see the commit id that you want to revert
运行以下命令以查看要还原的提交 ID
git log --oneline --decorate --graph
You will get like a following screenshot
If you also check remote (via Web Interface)then you can see that this would be same as shown below
如果您还检查远程(通过 Web 界面),那么您可以看到这将与下图相同
As per screenshot currently you are on commit id e110322however you want to revert back to 030bbf6BOTH LOCALLY and REMOTELY.
根据屏幕截图,您目前处于提交 ID e110322 上,但是您想恢复到030bbf6BOTH LOCALLY和REMOTELY。
Perform the following steps to DELETE/REVERT Commits Locally+Remotely
执行以下步骤以本地+远程删除/还原提交
First Locally Reverting to commit id 030bbf6
第一次本地恢复到提交 id 030bbf6
git reset --hard 030bbf6
followed by
其次是
git clean -f -d
These two commands clean force reset to commit stage 030bbf6as shown below in snapshot
这两个命令清除强制重置以提交阶段030bbf6,如下面的快照所示
now if you run
git status then you'll see that you are TWO Commits BEHIND from the remote branch as shown below
现在如果你运行 git status 那么你会看到你在远程分支后面有两个提交,如下所示
Run following to update your indexes (if there are any updates). It is recommended that you ask all developers not to accept any pull requests on main remote branch.
运行以下更新您的索引(如果有任何更新)。建议您要求所有开发人员不要接受主远程分支上的任何拉取请求。
git fetch --all
Once you are done with it then you are required to Pushthis commit forcefullyby using +symbol in-front of branch as shown below. I have used here as masterbranch, you can replace it with any
完成后,您需要通过在分支前使用+符号强制推送此提交,如下所示。我在这里用作主分支,您可以将其替换为任何
git push -u origin +master
now if you see the web interface of remote then commit there should be reverted as well.
现在,如果您看到远程的 Web 界面,则提交也应该恢复。
回答by Mo D Genesis
This will remove your pushed commits
这将删除您推送的提交
git reset --hard 'xxxxx'
git clean -f -d
git push -f
回答by Sireesh Yarlagadda
git revert HEAD -m 1
In the above code line. "Last argument represents"
在上面的代码行中。“最后一个参数代表”
1 - reverts one commits.
2 - reverts last two commits.
n - reverts last n commits.
1 - 还原一次提交。
2 - 恢复最后两次提交。
n - 恢复最后 n 次提交。
You need to push after this command to take the effect on remote. You have other options like specifying the range of commits to revert. This is one of the option.
您需要在此命令之后推送才能对远程生效。您还有其他选项,例如指定要还原的提交范围。这是选项之一。
Later use git commit -am "COMMIT_MESSAGE"
then git push
or git push -f
以后使用git commit -am "COMMIT_MESSAGE"
thengit push
或git push -f
回答by backslash112
Here is my way:
这是我的方法:
Let's say the branch name is develop
.
假设分支名称是develop
.
# Create a new temp branch based on one history commit
git checkout <last_known_good_commit_hash>
git checkout -b develop-temp
# Delete the original develop branch and
# create a new branch with the same name based on the develop-temp branch
git branch -D develop
git checkout -b develop
# Force update this new branch
git push -f origin develop
# Remove the temp branch
git branch -D develop-temp
回答by avrsanjay
You can do something like
你可以做类似的事情
git push origin +<short_commit_sha>^:<branch_name>
回答by Xys
2020 Simple way :
2020 简单方法:
git reset --hard <your_commit_hash>
git push -f