How to change the specific string throughout all git message commits?

依然范特西╮ 提交于 2020-12-12 11:31:22

问题


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.


回答1:


Use git-filter-branch with it's --msg-filter option, e.g.:

git filter-branch -f --msg-filter 'sed "s/git-svn.*$//g"' -- --all

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

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...

Step-by-step

First clone a fresh copy of your repo, using the --mirror flag:

$ git clone --mirror git://example.com/my-repo.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.

Now you can run git-filter-branch to fix your commit messages:

git filter-branch -f --msg-filter 'sed "s/git-svn.*$//g"' -- --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.

...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.



来源:https://stackoverflow.com/questions/15872872/how-to-change-the-specific-string-throughout-all-git-message-commits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!