我知道这是在改写历史,很糟糕。
但是如何从远程分支永久删除少数提交?
我知道这是在改写历史,很糟糕。
但是如何从远程分支永久删除少数提交?
当前回答
从pctroll的答案简化,类似的基于这篇博客文章。
# look up the commit id in git log or on github, e.g. 42480f3, then do
git checkout master
git checkout your_branch
git revert 42480f3
# a text editor will open, close it with ctrl+x (editor dependent)
git push origin your_branch
# or replace origin with your remote
其他回答
git reset --soft commit_id
git stash save "message"
git reset --hard commit_id
git stash apply stash stash@{0}
git push --force
从pctroll的答案简化,类似的基于这篇博客文章。
# look up the commit id in git log or on github, e.g. 42480f3, then do
git checkout master
git checkout your_branch
git revert 42480f3
# a text editor will open, close it with ctrl+x (editor dependent)
git push origin your_branch
# or replace origin with your remote
例如,如果你想删除最近3次提交,运行以下命令从文件系统(工作树)中删除更改,并在本地分支上提交历史(索引):
git reset --hard HEAD~3
然后运行以下命令(在您的本地机器上)强制远程分支重写其历史记录:
git push --force
恭喜你!全部完成!
一些注意事项:
您可以通过运行命令检索所需的提交id
git log
然后你可以像这样用<desired-commit-id>替换HEAD~N:
git reset --hard <desired-commit-id>
If you want to keep changes on file system and just modify index (commit history), use --soft flag like git reset --soft HEAD~3. Then you have chance to check your latest changes and keep or drop all or parts of them. In the latter case runnig git status shows the files changed since <desired-commit-id>. If you use --hard option, git status will tell you that your local branch is exactly the same as the remote one. If you don't use --hard nor --soft, the default mode is used that is --mixed. In this mode, git help reset says:
重置索引,但不重置工作树(也就是说,更改的文件是 保存但未标记为提交)并报告未保存的内容 更新。
TL:博士;
n . git switch -C branch_name origin/branch_name~ Git push—force
完成后,远程分支将被n次提交还原。
解释:
使用git开关,按n次提交重置分支。c选项将强制创建具有相同名称的新分支。 将branch_name替换为分支名称, 将n(在命令的末尾)替换为要恢复的提交数。 命令1:git switch -C branch_name origin/branch_name~n 例如:git switch -C feature/dashboard origin/feature/dashboard~1 //返回dashboard分支的1次提交。 强制推送局部更改 命令#2:git push——force
提示:要撤消已提交(未推送)的更改,git将重置HEAD~
重要提示:确保你在“git push -f”上指定了哪些分支,否则你可能会无意中修改其他分支!(*)
本教程中显示了三个选项。以防链接中断,我将把主要步骤留在这里。
恢复完整的提交 删除最后一次提交 从列表中删除提交
1恢复完整提交
git revert dd61ab23
2删除上次提交
git push <<remote>> +dd61ab23^:<<BRANCH_NAME_HERE>>
或者,如果分支在本地可用
git reset HEAD^ --hard
git push <<remote>> -f
在+ dd61…是你的提交哈希,git将x^解释为x的父节点,+解释为强制的非快进推。
3从列表中删除提交
git rebase -i dd61ab23^
这将打开一个编辑器,显示所有提交的列表。删除你想要删除的那个。完成rebase和推力回购。
git rebase --continue
git push <remote_repo> <remote_branch> -f