我读过关于这个主题的类似帖子,但我不知道如何正确地做到这一点。

我签入了大约1000个我不想要的文件,我宁愿不需要逐个检查并将它们全部从回购中删除。

我有一个远程主分支。 我有本地主分支。

他们都在同一个修订。

我想回滚我的远程1提交。

我的历史是A- B- C- D- E。 我想回滚我的本地到D。 然后把它推到远程,所以我的当前哈希将是D远程和本地。

我做这个有问题。 我正在使用Git Tower,但对命令行很熟悉。任何帮助吗?

更新: 下面的评论很棒。使用重置似乎在一定程度上是不鼓励的,特别是当存储库与其他用户共享时。 在不使用硬重置的情况下撤销之前提交的更改的最佳方法是什么?有办法吗?


当前回答

如果你只想在不破坏本地存储库的情况下从远程存储库中删除最后一次提交,这里有一行代码:

git push origin +origin/master~:master

使用以下语法:

git push <remote> <refspec>

这里,<remote>是origin, <refspec>有以下结构:

+origin/master~:master

详见git-push(1)。前面的+表示“force push this ref”,另一部分表示“from origin/master~ to master (from remote origin)”。不难知道origin/master~是origin/master之前的最后一次提交,对吗?

其他回答

在本地主机上

git reflog
-- this will list all last commit
  e.g Head@{0} -- wrong push
      Head@{1} -- correct push  
git checkout Head@{1} .
  -- this will reset your last modified files

git status 
git commit -m "reverted to last best"
git push origin/master

没必要担心别人拉不拉。

完成了!

**简短回答关于恢复和重置**

有很多方法可以做到这一点。根据您的需求,从下面选择任何内容。

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.

对我来说有两个命令:

git checkout commit_id
git push origin +name_of_branch

我只是想从远程和清除提交历史中删除最后一次提交。 下面的方法非常有效

git reset --hard HEAD^ 
git push -f 

将本地分支向后设置一个修订版本(HEAD^表示向后设置一个修订版本):

git reset --hard HEAD^

将更改推至原点:

git push --force

你必须强制push,否则git会识别出你在一次提交后落后于原点,什么都不会改变。

使用——force命令告诉git在远程回购中覆盖HEAD,而不尊重那里的任何进展。