我读过关于这个主题的类似帖子,但我不知道如何正确地做到这一点。
我签入了大约1000个我不想要的文件,我宁愿不需要逐个检查并将它们全部从回购中删除。
我有一个远程主分支。
我有本地主分支。
他们都在同一个修订。
我想回滚我的远程1提交。
我的历史是A- B- C- D- E。
我想回滚我的本地到D。
然后把它推到远程,所以我的当前哈希将是D远程和本地。
我做这个有问题。
我正在使用Git Tower,但对命令行很熟悉。任何帮助吗?
更新:
下面的评论很棒。使用重置似乎在一定程度上是不鼓励的,特别是当存储库与其他用户共享时。
在不使用硬重置的情况下撤销之前提交的更改的最佳方法是什么?有办法吗?
**简短回答关于恢复和重置**
有很多方法可以做到这一点。根据您的需求,从下面选择任何内容。
1. 通过REVERTing commit:
如果你想要恢复你上次提交的所有更改,这意味着如果你在你的文件中添加了一些东西,在恢复完成后将被删除。如果你删除文件中的某些东西,还原过程将添加这些文件。
您可以还原最后一次提交。如:
1.git revert HEAD^
2.git push origin <Branch-Name>
或者您可以使用该提交的散列恢复到任何以前的提交。如:
1. git revert <HASH>
2.git push origin <Branch-Name>
或者如果提交是合并提交,你可以尝试这样做:
1.git revert -m 1 <HASH> (-m 1 refers to the first parent of two merged branches)
2.git push origin <Branch-Name>
2. 通过重置前头部
如果你只想指向之前的提交使用reset;它将本地环境指向上一次提交。你可以重置你的头到以前的提交或重置你的头到以前的任何提交。
重置到最后一次提交。
1.git reset HEAD^
2.git push -f origin <Branch-name>
重置到任何以前的提交:
1.git reset <HASH>
2.git push -f origin <Branch-name>
REVERT和RESET之间的交易:
Why would you choose to do a revert over a reset operation? If you have already pushed your chain of commits to the remote repository (where others may have pulled your code and started working with it), a revert is a nicer way to cancel out changes for them. This is because the Git workflow works well for picking up additional commits at the end of a branch, but it can be challenging if a set of commits is no longer seen in the chain when someone resets the branch pointer back.
And resetting a branch can be destroying what you have done till now.Because when you reset a commit, GIT will delete all the commits that have done after this commit.One silly mistake can destroy all your hard work and it doesn't keep any history what you are resetting. On the other hand reverting a commit is better option in this scenario. When you revert a commit, GIT creates a new commit with the completely opposite changes of the commit you are willing to revert.And it points to the end of that branch. So it won't mess up anything on our silly mistake.