如何更改所有git消息提交中的特定字符串?
我想grep并更改所有推送到Github的消息中的特定字符串.是否可以?如何?我知道如何通过 git commit --amend
更改最后一条消息,但是我想更改所有提交的所有消息.
I want to grep and change specific string throughout all messages that are pushed to Github. Is it possible? How?
I know how to change last message bye git commit --amend
but I want to change all message of all commits.
使用 git-filter-branch
及其-msg-filter
选项,例如:
git filter-branch -f --msg-filter 'sed "s/git-svn.*$//g"' -- --all
请注意,这将在您的存储库中几乎完全更改您的提交ID,因此所有从事您的项目的人都需要进行全新的克隆.
Note that this will change pretty much all your commit ids in your repo, and so everyone who works on your project will thus need to do a fresh clone.
有关更多讨论,请参见此博客文章:
See this blog post for some more discussion:
http://mm0hai.net/blog/2011/03/10/rewriting-git-commit-message-history.html
请注意,上述命令仅针对您的本地副本执行,并且您需要推送到GitHub才能在其中反映更新...
Note that the above command only executes against your local copy, and you need to push to GitHub in order to get your updates reflected there...
逐步
首先使用-mirror
标志克隆您的仓库的新副本:
First clone a fresh copy of your repo, using the --mirror
flag:
$ git clone --mirror git://example.com/my-repo.git
这是一个裸仓库,这意味着您的常规文件将不可见,但它是存储库的Git数据库的完整副本,此时,您应该进行备份以确保您不会丢失任何东西.
This is a bare repo, which means your normal files won't be visible, but it is a full copy of the Git database of your repository, and at this point you should make a backup of it to ensure you don't lose anything.
现在,您可以运行 git-filter-branch
来修复提交消息:
Now you can run git-filter-branch
to fix your commit messages:
git filter-branch -f --msg-filter 'sed "s/git-svn.*$//g"' -- --all
一旦您对存储库的更新状态感到满意,请将其回退(请注意,由于您的克隆命令使用了-mirror
标志,因此此推送将更新 all 远程服务器上的引用):
Once you're happy with the updated state of your repo, push it back up (note that because your clone command used the --mirror
flag, this push will update all refs on your remote server):
$ git push
在这一点上,您已经准备好让所有人(包括您自己)放弃他们的回购的旧副本,并为新的,原始的好数据做新的克隆.
At this point, you're ready for everyone- including yourself -to ditch their old copies of the repo and do fresh clones of the nice, new pristine data.
...我不得不指出 BFG 通常是清理Git历史记录比 git-filter-branch
好得多,但是在这种情况下, git-filter-branch
听起来很适合您的需求.
...I'm compelled to point out that The BFG is often much better than git-filter-branch
for cleaning up Git history, but in this case just git-filter-branch
sounds perfect for your needs.