繁体   English   中英

从远程Git存储库中删除本地后面的提交

[英]Delete commits from remote Git repo that are behind local

我在Git中有四个未同步的提交,一个在前面,三个在后面。 我想将前面的提交推送到远程,并从远程分支中删除其他提交。 我怎样才能做到这一点?

为了保持历史记录的一致性,不建议删除存储库中已经存在的提交。 特别是如果有多个用户。 正确的方法是在单独的分支中还原不需要的提交,将revert-commits推送到存储库,而不是通过存储库分支与预先提交的合并/重新存储分支并推送结果。 这是命令示例(您需要根据需要对其进行修改):

git branch revert-commits origin/yourremotebranch
git checkout revert-commits
# check hashes of needed commits
git log --oneline
# revert them in reverse order
git revert 473f879
git revert 473f878
git revert 473f877
git push
# checkout to branch with ahead-commit
git checkout ahead-commit
# rebase reverts in ahead-commit branch to resolve possible conflicts
git pull --rebase origin yourremotebranch
git push
# delete unneeded revert-commits branch
git branch -D revert-commits

提交和分支的日志将有所帮助(例如git log --graph --decorate --pretty=oneline --abbrev-commit --all ),以便了解您想要做什么。

但是考虑到git中的提交就像是一个“依赖链”,并且对要执行的操作有所了解,最简单的方法可能就是从上次同步的点创建一个新分支,然后选择提交您想要并使用它(如果您正在像github那样推动分支,则适用)。

此后,如果您直接在master中工作,则将删除不需要的提交,然后合并回先前创建的分支。

但是,我只是猜测您发布的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM