Mastering Git - Part 9: Filter Branch | Ray Wenderlich

Interactive rebase allows you to rewrite history one commit at a time. But what if you want to automate that? In this video you'll see how you can use the filter-branch tool to programmatically rewrite history—kinda like a nerdy time traveller.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4289-mastering-git/lessons/9

Sam,

I don’t have enough hair left for this! I’ve tried 15 ways from Sunday and nothing works. The filter-branch command appears to run as it does in your video but the 2 commits continue to show in the log and git show <treeish-of-the-commit> still shows the contents. Any clue what’s going wrong?

I’ll note that I still see those commits even in the 09_magicSquareJS_complete.zip version of the repo in the download materials.

-r

Hi @rcritz

Which commits are you referring to? The two empty ones? I’m trying to find them in the downloaded materials.

The one thing I can think of is that the commits will be retained in the backup tree that filter-branch creates. Therefore, you’d still be able to find them by hash. However, I’m unsure why you’d be able to see them in the log.

sam

I found it, @samdavies. One of the stashes is holding a reference to the old commit so it still shows up when doing
git log --oneline --decorate --graph --all -- SECRETS. By dropping the stashes, the log no longer shows the commit, though you can still do git show -p 69670e7 to see it. Expiring the reflog and running a gc gits rid of it for good.

See the transcript:

[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âš‘ 2]$ git log --oneline --decorate --graph --all -- SECRETS
* 69670e7 Adding a new secret
* 2d3ab12 Adding the license details to the README
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âš‘ 2]$ git for-each-ref --contains 69670e7
7086b9380846fa12f07205428d341e9ff44b3f65 commit	refs/stash
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âš‘ 2]$ git stash drop
Dropped refs/stash@{0} (7086b9380846fa12f07205428d341e9ff44b3f65)
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âš‘ 1]$ git stash drop
Dropped refs/stash@{0} (f6f062a73d4001da9214dc2eae6b3bc0c39a456d)
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ git log --oneline --decorate --graph --all -- SECRETS
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ git show -p 69670e7
commit 69670e785ec9611026340cfbff6cdb4358b9918e
Author: Will <will@example.com>
Date:   Mon Jul 3 14:10:59 2017 +0700

    Adding a new secret

diff --git a/SECRETS b/SECRETS
index 21ce8b4..feab599 100644
--- a/SECRETS
+++ b/SECRETS
@@ -1 +1,2 @@
 DEPLOY_KEY=THIS_IS_REALLY_SECRET
+RAYS_HOTTUB_NUMBER=012-555-6789
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ git reflog expire --expire=now --all
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ git gc --prune=now
Counting objects: 271, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (260/260), done.
Writing objects: 100% (271/271), done.
Total 271 (delta 142), reused 0 (delta 0)
[critz@ace 0 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ git show -p 69670e7
fatal: ambiguous argument '69670e7': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
[critz@ace 128 ~/Downloads/09_FilterBranch/magicSquareJS master â­‘|âś” ]$ 

@rcritz—well spotted! This is the problem with the --all argument on git log. What you really want is “everything relevant” not “everything ever”. I tend to use GUIs instead of git log directly, so I guess I didn’t realise.

sam

Likewise, @samdavies. I use almost exclusively SourceTree but I was being a good doobie and following along your instructions! I learned even more than you intended in the process! :]

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.