Skip to content

Commit f95eef1

Browse files
dschogitster
authored andcommitted
filter-branch: introduce convenience function "skip_commit"
With this function, a commit filter can leave out unwanted commits (such as temporary commits). It does _not_ undo the changeset corresponding to that commit, but it _skips_ the revision. IOW no tree object is changed by this. If you like to commit early and often, but want to filter out all intermediate commits, marked by "@@@" in the commit message, you can now do this with git filter-branch --commit-filter ' if git cat-file commit $GIT_COMMIT | grep '@@@' > /dev/null; then skip_commit "$@"; else git commit-tree "$@"; fi' newbranch Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7e0f170 commit f95eef1

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

Documentation/git-filter-branch.txt

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ OPTIONS
112112
As a special extension, the commit filter may emit multiple
113113
commit ids; in that case, ancestors of the original commit will
114114
have all of them as parents.
115+
+
116+
You can use the 'map' convenience function in this filter, and other
117+
convenience functions, too. For example, calling 'skip_commit "$@"'
118+
will leave out the current commit (but not its changes! If you want
119+
that, use gitlink:git-rebase[1] instead).
115120

116121
--tag-name-filter <command>::
117122
This is the filter for rewriting tag names. When passed,
@@ -209,24 +214,39 @@ To remove commits authored by "Darl McBribe" from the history:
209214
git filter-branch --commit-filter '
210215
if [ "$GIT_AUTHOR_NAME" = "Darl McBribe" ];
211216
then
212-
shift;
213-
while [ -n "$1" ];
214-
do
215-
shift;
216-
echo "$1";
217-
shift;
218-
done;
217+
skip_commit "$@";
219218
else
220219
git commit-tree "$@";
221220
fi' HEAD
222221
------------------------------------------------------------------------------
223222

223+
Note that the changes introduced by the commits, and not reverted by
224+
subsequent commits, will still be in the rewritten branch. If you want
225+
to throw out _changes_ together with the commits, you should use the
226+
interactive mode of gitlink:git-rebase[1].
227+
228+
The function 'skip_commits' is defined as follows:
229+
230+
--------------------------
231+
skip_commit()
232+
{
233+
shift;
234+
while [ -n "$1" ];
235+
do
236+
shift;
237+
map "$1";
238+
shift;
239+
done;
240+
}
241+
--------------------------
242+
224243
The shift magic first throws away the tree id and then the -p
225244
parameters. Note that this handles merges properly! In case Darl
226245
committed a merge between P1 and P2, it will be propagated properly
227246
and all children of the merge will become merge commits with P1,P2
228247
as their parents instead of the merge commit.
229248

249+
230250
To restrict rewriting to only part of the history, specify a revision
231251
range in addition to the new branch name. The new branch name will
232252
point to the top-most revision that a 'git rev-list' of this range

git-filter-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ map()
2323
fi
2424
}
2525

26+
# if you run 'skip_commit "$@"' in a commit filter, it will print
27+
# the (mapped) parents, effectively skipping the commit.
28+
29+
skip_commit()
30+
{
31+
shift;
32+
while [ -n "$1" ];
33+
do
34+
shift;
35+
map "$1";
36+
shift;
37+
done;
38+
}
39+
2640
# override die(): this version puts in an extra line break, so that
2741
# the progress is still visible
2842

t/t7003-filter-branch.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,7 @@ test_expect_success "remove a certain author's commits" '
138138
git-filter-branch -f --commit-filter "\
139139
if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
140140
then\
141-
shift;\
142-
while [ -n \"\$1\" ];\
143-
do\
144-
shift;\
145-
echo \"\$1\";\
146-
shift;\
147-
done;\
141+
skip_commit \"\$@\";
148142
else\
149143
git commit-tree \"\$@\";\
150144
fi" removed-author &&

0 commit comments

Comments
 (0)