在Git中维护自定义分支(隐藏文件)
我正在尝试实现一个用例,该用例在本页中描述为“通过对上游回购的定制维护一个fork”: 简而言之,我想将代码推送到3个不同的位置—一个主位置和两个简化版本。我将只对master进行修改,然后合并到分支中。我正试图通过3个不同的分支和不同的远程URL来实现这一点,但我想从分支1和2隐藏一些文件: 主->内部回购 分支1->远程#1(不包括文件A) 分支2->远程#2(不包括文件B) 我已经安装好遥控器了。我知道如何分支master并将其远程设置为其他对象。我关心的是从每个分支的历史记录中完全删除单个文件。我以前使用过git filter branch,但有一些问题:在Git中维护自定义分支(隐藏文件),git,version-control,Git,Version Control,我正在尝试实现一个用例,该用例在本页中描述为“通过对上游回购的定制维护一个fork”: 简而言之,我想将代码推送到3个不同的位置—一个主位置和两个简化版本。我将只对master进行修改,然后合并到分支中。我正试图通过3个不同的分支和不同的远程URL来实现这一点,但我想从分支1和2隐藏一些文件: 主->内部回购 分支1->远程#1(不包括文件A) 分支2->远程#2(不包括文件B) 我已经安装好遥控器了。我知道如何分支master并将其远程设置为其他对象。我关心的是从每个分支的历史记录中完全删除单
commitN message N
…
commit2 mesaage2
commit1 message1
- 在
中删除branch1
: First,通过fileA
在git log branch1--oneline
上获取第一次提交。它将列出branch1上的提交,如下所示:branch1
commitN message N … commit2 mesaage2 commit1 message1
的第一次提交在底部,而branch1
的最新提交在顶部。因此,您需要获取branch1
的提交id 然后,删除commit1
中的branch1
fileA
现在git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch fileA' --prune-empty -f -- commit1..branch1
在fileA
中被完全删除branch1
- 删除
中的branch2
(使用相同的方法): First,通过fileB
获取git log branch2--oneline
的第一次提交(假设是branch2
) 然后删除branch2中的文件Bcommitb1
git filter-branch --index-filter 'git rm -r --cached --ignore-unmatch fileB' --prune-empty -f -- commitb1..branch2
master
分别合并到branch1
和branch2
中时,将分别添加fileA
和fileB
。因此,在合并之前,您应该分别为branch1
和branch2
添加.gitignore
文件:
git checkout branch1
touch .gitignore
echo fileA >> .gitignore
git add .gitignore
git commit -m 'add .gitignore for branch1'
git checkout branch2
touch .gitignore
echo fileB >> .gitignore
git add .gitignore
git commit -m 'add .gitignore for branch2'
现在您可以将master
合并到branch1
和branch2
,并且fileA
和fileB
不会分别添加到branch1
和branch2
中
此外,没有必要在单独的回购中管理fileA
和fileB
。下面的方法可以更好地达到要求