跨多个分支的Git interactive-rebase

我有一个git repo,它有一个包含一些我想删除的敏感内容的源文件(我不想删除该文件,只是修改其内容).提交很早就在项目开发中(当我们只有一个分支时),但现在在其他分支中找到它自己.

有没有git-fu我可以在所有分支中清理它?交互式rebase一次只能运行一个Head.

我确实在github找到了this命令:

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch DotNet/src/ExternalLibs/GemBox/' \
--prune-empty --tag-name-filter cat -- --all

但它只适用于删除,而不适用于修改.

A-B-C-**STUPID**-D-E-F
                    \
                     \-G-H-I
                          \-J-K

最佳答案 假设修改后的内容也不敏感,那么这可能会起作用:在您要修改的提交中签出新分支,修改文件,然后使用非交互式rebase,在先前未修改之后保留所有内容的合并重新提交新的修改提交:

# Checkout a branch "fix" at the commit STUPID
git checkout -b fix STUPID

# Make your file changes...

# Amend STUPID
git add .
git commit --amend -m "REDACTED MWA-HA-HA!"

# Do the BIGGEST rebase that you'll probably ever do in your entire life!
git rebase --preserve-merges --onto fix STUPID master
# or `-p` for short
git rebase -p --onto fix STUPID master

# Verify that the only difference between master and master@{1}
# is the amended content:
git diff master@{1} master

一旦你确认了rebase之前和之后master之间的唯一区别就是修改后的内容,你可能想要通过使你的reflog到期并运行git gc来清除旧提交的本地和远程repos:

git reflog expire --expire=now --all
git gc --prune=now

您可以阅读以下可能需要执行的任何其他清理步骤:

> Remove sensitive files and their commits from Git history.
> GitHub: Remove sensitive data.

顺便说一句,请先在另一个本地克隆上测试rebase,然后再尝试使用你的repo的唯一副本……万一出现问题.

哦,是的,我几乎忘记了,如果您在STUPID的修改导致与其上的重新提交的提交冲突,您将需要在rebase期间修复这些冲突.

这款1337 git-fu为您带来了Cupcake的礼貌:自2012年开始对git repos进行心脏直视手术;)

点赞