使用git filter-branch通过提交消息移除提交 [英] Using git filter-branch to remove commits by their commit message
问题描述
- foo 555:blah
- foo 123:blah blah
- foo 555:blah blah blah
- foo 321:blahblah
我想删除所有不以foo 555:开头的提交,有没有办法使用git filter-branch(或其他任何工具)?
<在我们的仓库中,我们有一个约定,每个提交消息都以特定的模式开始:原始版本(更详细):
#555:SOME_MESSAGE
做一些重定向将潜在发行分支的变化引入特定问题的分支。换句话说,我可能有分支foo-555,但在我将它合并到分支预发布之前,我需要获得任何提前发布的foo-555没有的提交(这样foo- 555可以快速合并到预发行版中)。
然而,因为预发行版有时会发生变化,所以我们有时会收到您从预发布版本,但之后的提交将从预发布版本中删除。很容易识别来自预发布的提交,因为来自提交消息的号码不会与分支号码匹配;例如,如果我在我的foo-555分支中看到Redmine#123:...,我知道它不是我分支提交的内容。
所以现在问题:我想删除不属于分支的所有提交;换句话说,任何提交:- 在我的foo-555分支中,但不在预发布分支中(预发布
- 提交消息不以Redmine#555开头
但当然555会因分支而异。有没有什么办法可以使用filter-branch(或其他工具)来完成这个任务?目前我能看到的唯一办法就是做一个交互式的rebase(git rebase -i)并手动删除所有坏的提交。
使用 Redmine#555
编写脚本以删除行:
<$ p $ $ $
grep -v'Redmine#555'< code>#!/ bin / sh
mv $ 1 $ 1. $$
< $ 1。$$> $ 1
rm -f $ 1。$$
当然,您可以做到这一点,无论您想要什么(例如 echo
命令脚本 ed
)。
<然后将
EDITOR
设置为您的脚本: EDITOR = / path / to / script git rebase -i REVISION
当然它不会是保证完成 - 由于不做修改而导致重新布局期间可能会出现错误。你仍然可以修复它们,然后手工修改 git rebase --continue
。
Simple Version:
If I have a branch "foo-555", with a bunch of commits with messages like:
- foo 555: blah
- foo 123: blah blah
- foo 555: blah blah blah
- foo 321: blahblah
and I want to remove all the commits that don't start with "foo 555:", is there any way to do that using git filter-branch (or any other tool for that matter)?
Original Version (More Detailed):
In our repository we have a convention where every commit message starts with a certain pattern:
Redmine #555: SOME_MESSAGE
We also do a bit of rebasing to bring in the potential release branch's changes to a specific issue's branch. In other words, I might have branch "foo-555", but before I merge it in to branch "pre-release" I need to get any commits that pre-release has that foo-555 doesn't (so that foo-555 can fast-forward merge in to pre-release).
However, because pre-release sometimes changes, we sometimes wind up with situations where you bring in a commit from pre-release, but then that commit later gets removed from pre-release. It's easy to identify commits that came from pre-release, because the number from their commit message won't match the branch number; for instance, if I see "Redmine #123: ..." in my foo-555 branch, I know that its not a commit from my branch.
So now the question: I'd like to remove all of the commits that "don't belong" to a branch; in other words, any commit that:
- Is in my foo-555 branch, but not in the pre-release branch (pre-release..foo-555)
- Has a commit message that doesn't start with "Redmine #555"
but of course "555" will vary from branch to branch. Is there any way to use filter-branch (or any other tool) to accomplish this? Currently the only way I can see to do it is to do go an interactive rebase ("git rebase -i") and manually remove all the "bad" commits.
Write a script to remove lines with Redmine #555
:
#!/bin/sh
mv $1 $1.$$
grep -v 'Redmine #555' < $1.$$ > $1
rm -f $1.$$
Of course you can do that however you want (eg echo
a script of commands to ed
).
Then launch your rebase with EDITOR
set to your script:
EDITOR=/path/to/script git rebase -i REVISION
Of course it still won't be guaranteed to complete -- there may be errors during the rebase caused by leaving out revisions. You can still fix them and git rebase --continue
manually.
这篇关于使用git filter-branch通过提交消息移除提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!