Answers:
您可以使用以下方法还原单个提交:
git revert <commit_hash>
这将创建一个新的提交,该提交将还原您指定的提交的更改。请注意,它仅还原特定的提交,之后不提交。如果要还原一定范围的提交,可以这样进行:
git revert <oldest_commit_hash>..<latest_commit_hash>
它还原指定提交之间(包括指定提交)的提交。
查看git-revert手册页以获取有关该git revert
命令的更多信息。另请参阅此答案以获取有关还原提交的更多信息。
git log
<oldest_commit_hash>
包含在还原列表中
一种不保留“撤消”痕迹的解决方案。
注意:如果有人已经撤回了您的更改,请不要这样做(我只会在我的个人仓库中使用它)
做:
git reset <previous label or sha1>
这将在本地重新签出所有更新(因此git status将列出所有更新的文件)
然后您“完成工作”并重新提交更改(注意:此步骤是可选的)
git commit -am "blabla"
此时,您的本地树与远程树不同
git push -f <remote-name> <branch-name>
将推送并强制远程考虑此推送,并删除上一个推送(指定remote-name和branch-name不是强制性的,但建议避免使用update标志更新所有分支)。
!! 当心一些标签可能仍指向删除提交!如何删除远程标签
git push -f
了最后一次正确的提交,并清除了远程历史记录!谢谢!
在这些情况下,我要做的是:
在服务器中,将光标移回上一个已知的良好提交:
git push -f origin <last_known_good_commit>:<branch_name>
在本地,执行相同的操作:
git reset --hard <last_known_good_commit>
# ^^^^^^
# optional
请参阅my_new_branch
我为此创建的分支上的完整示例:
$ git branch
my_new_branch
这是向中添加一些内容后的最近历史记录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
我想摆脱已经被推送的最后一次提交,所以我运行:
$ git push -f origin b4zad078237fa48746a4feb6517fa409f6bf238e:my_new_branch
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:me/myrepo.git
+ 80143bc...b4zad07 b4zad078237fa48746a4feb6517fa409f6bf238e -> my_new_branch (forced update)
真好!现在,我看到在该commit(myfile.py
)上更改的文件显示为“ notstaged for commit”:
$ 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")
由于我不需要这些更改,因此我也只需将光标移回本地:
$ git reset --hard b4zad078237fa48746a4feb6517fa409f6bf238e
HEAD is now at b4zad07 Initial commit
因此,现在HEAD在本地和远程的上一次提交中:
$ git log
commit b4zad078237fa48746a4feb6517fa409f6bf238e
Author: me
Date: Tue Mar 18 12:46:59 2016 +0100
Initial commit
如果您通过git命令行执行以下步骤,则可以本地(或远程)禁止(或也可以将其称为DELETE)Git提交。
运行以下命令以查看要还原的提交ID
git log --oneline --decorate --graph
如果您还通过“ Web Interface”(Web界面)检查“远程”,则可以看到如下所示
按照目前的截图你在提交ID e110322但是要恢复到030bbf6 BOTH 本地和远程。
执行以下步骤以本地/远程删除/删除提交
第一次本地还原以提交ID 030bbf6
git reset --hard 030bbf6
其次是
git clean -f -d
这两个命令清除强制重置以提交阶段030bbf6,如下快照所示
现在,如果您运行git status,那么您将看到您是来自远程分支的两次提交行为,如下所示
运行以下命令更新索引(如果有任何更新)。建议您要求所有开发人员不要在主远程分支上接受任何拉取请求。
git fetch --all
完成后,需要使用分支前面的+符号强行推送此提交,如下所示。我在这里用作主分支,您可以将其替换为
git push -u origin +master
现在,如果您看到远程的Web界面,则提交也应还原。
这将删除您的推送提交
git reset --hard 'xxxxx'
git clean -f -d
git push -f
git revert HEAD -m 1
在上面的代码行中。“最后一个参数代表”
1-恢复一次提交。
2-恢复最后两次提交。
n-恢复最后n次提交。
您需要按此命令后才能在远程上生效。您还有其他选项,例如指定要还原的提交范围。这是选项之一。
以后使用git commit -am "COMMIT_MESSAGE"
,然后git push
或git push -f
git revert ref1..ref2
这是我的方式:
假设分支名称为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