简体   繁体   English

跨多个分支的Git interactive-rebase

[英]Git interactive-rebase across multiple branches

I have a git repo that has a source file containing some sensitive content which I wish to remove ( I don't want to delete the file, just amend its contents ). 我有一个git repo,它有一个包含一些我想删除的敏感内容的源文件( 我不想删除该文件,只是修改其内容 )。 The commit was pretty early on in the project development (when we only had a single branch), but now finds its self in n other branches. 提交很早就在项目开发中(当我们只有一个分支时),但现在在其他分支中找到它自己。

Is there any git-fu I can run to clean it in all branches? 有没有git-fu我可以在所有分支中清理它? An interactive rebase only works one Head at a time. 交互式rebase一次只能运行一个Head。

I did find this command over at github: 我确实在github上找到了这个命令:

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

but it only works for deletions, not amendments . 但它只适用于删除,而不适用于修改

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

Assuming that the amended content is not also sensitive, then this might work: checkout a new branch at the commit that you want to amend, amend the file, then use a non-interactive rebase that preserves merges of everything after the previously un-amended commit back onto the new amended commit: 假设修改后的内容也不敏感,那么这可能会起作用:在您要修改的提交中签出新分支,修改文件,然后使用非交互式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

Once you've verified that the only difference between master before and after the rebase is the amended content, you'll probably want to purge your local and remote repos of the old commits by expiring your reflog and running git gc : 一旦你确认了rebase之前和之后master之间的唯一区别就是修改后的内容,你可能想要通过使你的reflog到期并运行git gc来清除旧提交的本地和远程repos:

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

You can read about any other cleanup steps that you might need to do at the following: 您可以阅读以下可能需要执行的任何其他清理步骤:

  1. Remove sensitive files and their commits from Git history . 从Git历史记录中删除敏感文件及其提交
  2. GitHub: Remove sensitive data . GitHub:删除敏感数据

By the way, please test the rebase on another local clone first before you try this on the only copy of your repo that you have...in case something goes wrong. 顺便说一句, 请先在另一个本地克隆上测试rebase,然后再尝试使用你的repo的唯一副本......万一出现问题。

Oh yeah, I almost forgot, if your amendment at STUPID causes conflicts with the commits that are being rebased on top of it, you'll need to fix those conflicts during the rebase. 哦,是的,我几乎忘记了,如果您在STUPID的修改导致与其上的重新提交的提交冲突,您将需要在rebase期间修复这些冲突。

This 1337 git-fu brought to you courtesy of Cupcake : Performing open-heart surgeries on git repos since 2012 ;) 这款1337 git-fu为您带来了 Cupcake的 礼貌自2012年开始对git repos进行心脏直视手术;)

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

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