git 如何恢复推送提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46897033/
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 revert pushed commits?
提问by John Little
we want to undo the last 2 commits which were both pushed to origin master. in tortoise git, we can view the history, and click on the 3rd from last commit, and chose "reset master to this" which we are guessing means revert to this version.
我们想要撤消最后 2 次提交,它们都被推送到原始主控。在 tortoise git 中,我们可以查看历史记录,然后单击上次提交后的第 3 个,然后选择“将 master 重置为此”,我们猜测这意味着恢复到此版本。
however, when you select this option you have to choose from one of these three options:
但是,当您选择此选项时,您必须从以下三个选项之一中进行选择:
- soft - leave working tree and index untouched.
- mixed - leave working tree untouched, reset index
- hard - reset working tree and index.
- soft - 保持工作树和索引不变。
- 混合 - 保持工作树不变,重置索引
- 硬重置工作树和索引。
Unfortunately, we dont know which of these 3 options we should choose - we just want to undo the last two commits and go back in time.
不幸的是,我们不知道应该选择这 3 个选项中的哪一个 - 我们只想撤消最后两次提交并及时返回。
The next question is does this operation happen on the remote origin, your repo, or your working directory, or a combination of these 3? E.g. does a commit and push have to be done after reverting, or does it do this for you?
下一个问题是此操作是在远程源、您的存储库、您的工作目录还是这三者的组合上发生?例如,是否必须在还原后完成提交和推送,还是它为您执行此操作?
We would take a guess that HARD is what we want - it will revert the repository (not sure which) and our local source code back to a previous version. If this is the case, why is this not the default, and what is the use case for the other two options? Either you want to revert or not, and if you only revert the remote repo, and not your local working files then you are going to be in a messy state.
我们会猜测 HARD 就是我们想要的 - 它会将存储库(不确定是哪个)和我们的本地源代码恢复到以前的版本。如果是这种情况,为什么这不是默认值,另外两个选项的用例是什么?要么您想恢复,要么不恢复,如果您只恢复远程存储库,而不是您的本地工作文件,那么您将处于混乱状态。
We don't have any locally modified files.
我们没有任何本地修改的文件。
Note, we are quite desperate for a solution which doesnt break git (we have broken git in the past and had to craete a new repo). Perhaps another method possible, such as checkout out a prevous version, then checking it in over the top of the latest version, but we dont know how to do this.
请注意,我们非常渴望一个不会破坏 git 的解决方案(我们过去曾破坏过 git,不得不创建一个新的 repo)。也许另一种方法是可行的,例如检出以前的版本,然后在最新版本的顶部检入,但我们不知道如何执行此操作。
We get that we can do something like:
我们知道我们可以做这样的事情:
git checkout [revision]
But how do we then tell git that we want to make this version the new head or replace the head with this? I am guessing we cant just commit, as there is nothing to commit, as we are no longer on the master head. As an aside, if you check out a prev. version, and modify it, and commit, what are you committing and to where?
但是我们如何告诉 git 我们想要让这个版本成为新的头部或用这个替换头部?我猜我们不能只是提交,因为没有什么可提交的,因为我们不再是主人。顺便说一句,如果您查看上一个。版本,修改它,并提交,你提交什么,提交到哪里?
I have read https://www.atlassian.com/git/tutorials/undoing-changesbut it does not address the relationship between local and remote repos. If the commits to be undone are all pushed to the remote origin master, it is unclear what the recipe is to revert the remote origin, the local repo and the working directory - all 3 must be reverted to the commit 3 commits ago. We dont really care if this is done with a reset or revert - we just need a way to do it which works across all 3 locations.
我已经阅读了https://www.atlassian.com/git/tutorials/undoing-changes但它没有解决本地和远程 repos 之间的关系。如果要撤消的提交全部推送到远程源主机,则不清楚恢复远程源、本地存储库和工作目录的方法是什么——所有 3 项都必须恢复到 3 次提交之前的提交。我们并不真正关心这是通过重置还是恢复来完成的——我们只需要一种方法来做到这一点,它适用于所有 3 个位置。
It might be that the corrrect recipie is something like this:
正确的接收方可能是这样的:
git status (we are on master with a clean working dir)
git git revert HEAD~2
git commit -m "revert"
git push origin mater
But we have never seen this set of steps in the examples, and would rather do it with tortoise git if possible.
但是我们从来没有在例子中看到过这组步骤,如果可能的话,宁愿用 tortoise git 来做。
回答by Julian
There are two options:
有两种选择:
- go back in history, but preserve history:
revert
- go back in history and alter history, so the content of the commits is removed forever:
reset --hard
andpush --force
.
- 回到历史,但保留历史:
revert
- 回到历史并改变历史,因此提交的内容将被永久删除:
reset --hard
和push --force
。
If you're not sure, then use revert
, it's the safest option.
如果您不确定,请使用revert
,这是最安全的选择。
If you really, really, like to remove the commits and don't care about the changed history, use reset --hard
and push --force
. This has the following consequences:
如果您真的非常喜欢删除提交并且不关心更改的历史记录,请使用reset --hard
和push --force
。这有以下后果:
- Others could reintroduce the commit
- Others could depend on the commit that has been removed
- In public repositories, force pushing to the master is a no-go
- 其他人可以重新引入提交
- 其他人可能依赖于已删除的提交
- 在公共存储库中,强制推送到 master 是行不通的
Questions
问题
The next question is does this operation happen on the remote origin, your repo, or your working directory, or a combination of these 3? E.g. does a commit and push have to be done after reverting, or does it do this for you?
下一个问题是此操作是在远程源、您的存储库、您的工作目录还是这三者的组合上发生?例如,是否必须在还原后完成提交和推送,还是它为您执行此操作?
In general all changes in TortioseGit are in your clone ("working copy"). Also push is always a manual action
一般来说,TortioseGit 中的所有更改都在您的克隆(“工作副本”)中。也推送总是手动操作
We would take a guess that HARD is what we want - it will revert the repository (not sure which) and our local source code back to a previous version. If this is the case, why is this not the default, and what is the use case for the other two options?
我们会猜测 HARD 就是我们想要的 - 它会将存储库(不确定是哪个)和我们的本地源代码恢复到以前的版本。如果是这种情况,为什么这不是默认值,另外两个选项的用例是什么?
Because hard
removes commits and so the history, it is a dangerous operation. You could remove also changes of others!
因为hard
删除提交和历史,这是一个危险的操作。您也可以删除其他人的更改!
what is the use case for the other two options?
其他两个选项的用例是什么?
See What's the difference between git reset --mixed, --soft, and --hard?
请参阅git reset --mixed、--soft 和 --hard 之间的区别是什么?
Steps in TortoiseGit:
TortoiseGit 中的步骤:
Revert
恢复
- Revert commit (from log)
- 恢复提交(从日志)
Reset hard and force push
重置硬推
- Reset (from log)
- 重置(从日志)
- Choose hard
- 选择难
- Force push (checkbox "known changes")
- 强制推送(复选框“已知更改”)
回答by arul pushpam
this solution saved my time
这个解决方案节省了我的时间
1) git reset --hard 93827927ed6e245be27f94c93c7e3574e85b91c6 (this is your commit id)
2) git reset HEAD~1
3) git stash
4) git add .
5) git commit -m "your new commit message here"
6) git push --force
1) git reset --hard 93827927ed6e245be27f94c93c7e3574e85b91c6 (这是你的提交ID)
2) git reset HEAD~1
3) git stash
4) git add 。
5) git commit -m "你的新提交信息在这里"
6) git push --force
回答by Alexan
I think you're confusing revert and reset, see Undoing Changes:
我认为您混淆了还原和重置,请参阅撤消更改:
revert
恢复
The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.
git revert 命令撤消已提交的快照。但是,它不是从项目历史中删除提交,而是弄清楚如何撤消由提交引入的更改,并附加一个带有结果内容的新提交。这可以防止 Git 丢失历史记录,这对于您的修订历史记录的完整性和可靠的协作非常重要。
reset
重启
If git revert is a “safe” way to undo changes, you can think of git reset as the dangerous method. When you undo with git reset(and the commits are no longer referenced by any ref or the reflog), there is no way to retrieve the original copy—it is a permanent undo. Care must be taken when using this tool, as it's one of the only Git commands that has the potential to lose your work.
如果 git revert 是撤消更改的“安全”方式,您可以将 git reset 视为危险方法。当您使用 git reset 撤消(并且提交不再被任何 ref 或 reflog 引用)时,无法检索原始副本——这是一个永久的撤消。使用此工具时必须小心,因为它是仅有的可能会丢失您的工作的 Git 命令之一。
Probably in your case better to use revert:
可能在您的情况下最好使用还原:
in this case you just create new commit, which revert specific commit, no history change. You can do it locally and then push to remote.
在这种情况下,您只需创建新提交,即可还原特定提交,而不会更改历史记录。您可以在本地完成,然后推送到远程。
If you still want to do reset and don't need to keep local changes, you do hard reset:
如果您仍然想要重置并且不需要保留本地更改,您可以进行硬重置:
and then you should do force push:
然后你应该强制推送:
git push -f