我想从我的远程存储库中删除一些提交.在我的回购中我几乎没有这样的提交:
commits messages
abcd123 some message
xyze456 another commit message
98y65r4 this commit is not required
987xcrt this commit is not required
bl8976t initial commit
Run Code Online (Sandbox Code Playgroud)
我想删除提交 98y65r4和987xcrt我的回购.怎么实现呢?
Mon*_*Kin 27
有两个替代方案:安全的一个让你有一个肮脏的git历史,或者不安全的一个让你有一个干净的git历史.你选:
你可以告诉git"恢复提交".这意味着它将引入一个更改,以恢复您在提交中所做的每个更改.您需要执行两次(每次提交一次):
git revert 98y65r4
git revert 987xcrt
Run Code Online (Sandbox Code Playgroud)
此解决方案将保留您的git历史记录(您可以执行gitk --all以查看repo状态的图形表示):
2222222 revert of 987xcrt: this commit is not required
1111111 revert of 98y65r4: this commit is not required
abcd123 some message
xyze456 another commit message
98y65r4 this commit is not required
987xcrt this commit is not required
bl8976t initial commit
Run Code Online (Sandbox Code Playgroud)
然后你可以将新的2次提交推送到你的远程仓库:
git push
Run Code Online (Sandbox Code Playgroud)
此解决方案是安全的,因为它不会对远程仓库进行破坏性操作.
您也可以使用交互式rebase.命令是:
git rebase -i bl8976t
Run Code Online (Sandbox Code Playgroud)
在其中,您告诉git让您选择要混合,重新排序或删除的提交.
执行命令时,编辑器将打开一个类似于以下内容的文本:
pick bl8976t initial commit
pick 987xcrt this commit is not required
pick 98y65r4 this commit is not required
pick xyze456 another commit message
pick abcd123 some message
Run Code Online (Sandbox Code Playgroud)
继续删除你不想要的行,如下所示:
pick bl8976t initial commit
pick xyze456 another commit message
pick abcd123 some message
Run Code Online (Sandbox Code Playgroud)
保存文件并关闭编辑器.
到目前为止,这只修改了您的存储库的本地副本(您可以看到提交树gitk --all).
现在您需要将更改推送到您的仓库,这是通过"推动力"完成的,但在执行命令之前请记住推力是破坏性操作,它将覆盖远程存储库的历史记录并可能导致将麻烦合并到其他工作人员身上.如果你没事,想要做推力,那么命令是:
git push -f
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18037 次 |
| 最近记录: |