如何将git-replace推送到远程仓库? [英] How can I push git-replace to a remote repo?

查看:75
本文介绍了如何将git-replace推送到远程仓库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用"git replace"来替换分支(没有共同祖先)22b2b25来进行master提交.我希望此更改是永久的.我对以前的历史不感兴趣.在下面的输出中,前五个提交来自原始master分支,下两个提交来自另一个分支.

I have used "git replace" to substitute a branch (with no common ancestor) 22b2b25 for a commit in master. I want this change to be permanent. I'm not interested in the previous history. In the output below the top five commits are from the original master branch and the bottom 2 come from a different branch.

当我将其推送到新的存储库时,git-replace丢失了,旧的历史记录又出现了.有没有办法使这项更改永久生效?

When I pushed this to a new repository the git-replace was lost and the old history reappeared. Is there a way to make this change permanent?

分支母版看起来像原始回购中的以下内容.我想在远程仓库和后续克隆中看到这一点.

branch master looks like the below in the original repo. I want to see this in the remote repo and subsequent clones.

[mike@localhost canal_site]$ git log --oneline --decorate
cdf0ae3 (HEAD, origin/master, master) MM: sqlencode course name for ad-hoc courses
2b57df5 MM: fixes for changes so far
7916eaf MM: ad hoc - more refactoring
1e00d22 MM: reformatted code
e36cee1 factored out equal ops
22b2b25 (origin/prod20140313, prod20140313) initial load from production 9-June-2015
08379cd initial inclusion of production files from 9-June-2015

推荐答案

git replace 仅凭光是不够的:替换一次提交是不够的.

git replace alone isn't enough: replacing one commit isn't enough.

下一个提交现在必须将替换的提交作为父引用.

The next commit must now reference the replaced commit as a parent.

如"

通过git clone备份仓库.
在您不喜欢的提交之后找出您喜欢的第一个提交,并记住其直接祖先的哈希(在以下bbb中).
然后在您不喜欢的提交之前找到您喜欢的第一个提交(在以下aaa中):

Backup your repo via git clone.
Find out the first commit you do like after the commits you do not like and remember the hash of its direct ancestor (in the following bbb).
Then find the first commit you do like before the commits you do not like (in the following aaa):

git replace bbb aaa

查看您的存储库(使用git log),并检查您的预期更改之后一切是否都应该保持原样.
但是replace命令只是将一些元信息添加到.git/refs/replace中,而git命令会相应地解释这些信息.
为了使这些更改永久生效,您可以使用
git filter-branch :

Take a look at your repository (with git log) and check whether everything is as it should be after your intended change.
But the replace command just adds some meta information into .git/refs/replace, which git commands interpret correspondingly.
In order to make these changes permanent, you may use git filter-branch:

git filter-branch -- --all

然后,您需要强制执行推送(git push --force),以便使用分支的新历史记录覆盖远程仓库.或者,您只需推送到一个新的空裸仓库即可.

Then you would need to force push (git push --force) in order to override the remote repo with the new history of the branch. Or you van just push to a new empty bare repo.

更新(灵感来自 Stephen 的评论)

Update (inspired from Stephen's comment)

正如我在2019年10月在"可能破坏/重写历史记录的Git命令"中所述,git filter-分支或BFG已过时.
git filter-repo 更加易于使用和高效.

As I noted in Oct. 2019 in "Git commands that could break/rewrite the history", git filter-branch or BFG are obsolete.
git filter-repo is easier to use and more efficient.

这篇关于如何将git-replace推送到远程仓库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆