如何使用git filter-branch将文件添加到特定提交? [英] How to add a file to a specific commit with git filter-branch?

查看:123
本文介绍了如何使用git filter-branch将文件添加到特定提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要在回购中的特定过去提交中添加一些文件,并且准备好重写历史记录,这样它就会影响到与该提交子级不同的所有分支.我们正在尝试使用git filter-branch,并使用add,因为一旦将文件添加到提交中,就不会将其添加到子项中,但是我们找不到合适的参数来阻止它影响某些并发不同的提交.参见图片了解.

We need to add some file in a specific past commit on a repo and we are ready to rewrite history so it affects to all branches diverging from that commit's children. We are trying to use git filter-branch for that, using add, since once we add the file to a commit it won't be added to the children, but we can't find the right parameters for stop it affecting to some concurrent diverging commits. See image for understanding.

我们正在使用此命令为红色提交,但文件显示在紫色提交上-为什么? -并且它也出现在绿色提交上,我们不希望它影响该代码路径,只是出现在红色提交上,然后通过所有子提交和合并继承.

We are using this command targeting the red commit, but the file is appearing on the purple commit - why? - and it's also appearing on the green commit, and we don't want it to affect that code path, just appear on the red commit and then be inherited through all the child commits and the merge.

git filter-branch --index-filter "cp C:/Users/asdf/Documents/IdeaProj ects/git-crypt-tests/.gitattributes . && git add .gitattributes" 72c7e292ddd134c04285f3530afc829fd38de4 28..HEAD

git filter-branch --index-filter "cp C:/Users/asdf/Documents/IdeaProj ects/git-crypt-tests/.gitattributes . && git add .gitattributes" 72c7e292ddd134c04285f3530afc829fd38de4 28..HEAD

我理解得不好吗?

谢谢.

推荐答案

您似乎认为,当您将提交范围写为A..B时,它会包含边界.但事实并非如此.该表示法是B ^A的缩写,即,导致B的所有内容,但不包括导致A的所有内容.这将从范围中删除下限" A.解决方案是编写A~,意思是"A的祖先":A~..B.

It looks like you thought that when you write a commit range as A..B that it would include the bounds. But it does not. This notation is short for B ^A, i.e., everything leading up to B, but excluding everything up to A. This removes the "lower" bound A from the range. The solution is that you write A~, which means "the ancestor of A": A~..B.

此外,由于您确切知道要将文件添加到哪些提交以及不希望添加这些提交,因此可以限制修订版行程序仅列出所需的提交:

Furthermore, since you know exactly which commits you want to add the file to and which you do not want to add them, you can restrict the revision walker to list only the wanted commits:

git filter-branch --index-filter "cp C:/Users/asdf/Documents/IdeaProjects/git-crypt-tests/.gitattributes . && git add .gitattributes" -- HEAD --not 72c7e29~ ":/Fix Prestapp"

也就是说,您说希望所有提交到HEAD的提交,但是在72c7e29~之前或消息以Fix Prestapp开头的提交之前什么都没有.

That is, you say that you want all commits leading up to HEAD, but nothing before 72c7e29~ nor before the commit whose message begins with Fix Prestapp.

这篇关于如何使用git filter-branch将文件添加到特定提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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