git 批量提交
At Mozilla we're mostly strict about receiving contributions via git/GitHub. Aside from requiring tests and enforcing code quality, one basic of submitting a commit (or several) is that the commit message begins with "bug #######", which is a reference to its Bugzilla bug. The message requirement makes sense but I hate going back to contributors and asking them to do "the paperwork" of changing their commit message to match our standard, so I generally try to make the update for them.
在Mozilla,我们非常严格地通过git / GitHub接收捐款。 除了要求测试和强制执行代码质量外,提交(或多次提交)提交的一个基本原则是,提交消息以“ bug #######”开头,这是对其Bugzilla Bug的引用。 消息需求是合理的,但是我讨厌回到贡献者那里,要求他们完成更改提交消息以符合我们的标准的“文书工作”,因此我通常会尝试为他们进行更新。
Recently MDN received a contribution which upgraded CKEditor and did so via approximately 60 commits, none of which referenced the bug number for the feature update. I badly wanted to do the commit message updates for this contributor, but I wanted to do so in an automated way -- a batch update to save me the time updating each message one by one. Welcome to filter-branch
and --msg-filter
!
最近,MDN收到了对CKEditor进行升级的贡献,并通过大约60次提交进行了升级,但没有一次引用功能更新的bug号。 我非常想为该贡献者进行提交消息更新,但是我想以一种自动化的方式进行-批量更新,以节省我逐个更新每条消息的时间。 欢迎使用filter-branch
和--msg-filter
!
预先提交消息 (Prepending to Commit Messages)
To prepend text to every commit message in a given range, you'd execute a message like:
要将文本添加到给定范围内的每个提交消息之前,请执行以下消息:
git filter-branch --msg-filter 'echo "bug ###### - \c" && cat' master..HEAD
The combination of filter-branch
and --msg-filter
will allow you to recurse through every commit message. The echo piece allows you to build the new string. The last piece is the range whose commit messages should be targeted -- in this case, I'm prepending the text to every commit in the feature branch.
filter-branch
和--msg-filter
的组合将允许您遍历每个提交消息。 回声片段允许您构建新的字符串。 最后一部分是提交消息应该针对的范围-在这种情况下,我将文本放在feature分支中的每个提交之前。
You can also sed
to achieve this:
您也可以通过sed
实现:
git filter-branch -f --msg-filter 'sed "s/^/bug ###### - /"' master..HEAD
追加提交消息 (Appending to Commit Messages)
The case for appending to commit messages could be where you want to add the reviewer name(s) to the message. Appending is roughly the same:
追加提交消息的情况可能是您要在消息中添加审阅者姓名的地方。 追加大致相同:
git filter-branch -f --msg-filter 'cat && echo "[Reviewer Walsh]"' master..HEAD
The difference here is the position of the cat
command. Everything else can be the same.
这里的区别是cat
命令的位置。 其他所有内容都可以相同。
I have a love/hate relationship with git -- I love git because it's so easy for 90% of my needs but hate it when I need the other 10%. Hopefully these examples help you if you run into this need in the future!
我与git有爱/恨关系-我喜欢git,因为它很容易满足我90%的需求,但讨厌我需要的另外10%。 如果将来遇到这种需求,希望这些示例对您有所帮助!
git 批量提交