Skip to content

Instantly share code, notes, and snippets.

@iluwatar
Created February 21, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iluwatar/9f994a92c3c4ebda440c to your computer and use it in GitHub Desktop.
Save iluwatar/9f994a92c3c4ebda440c to your computer and use it in GitHub Desktop.
git_rewrite_history
git filter-branch --index-filter \
'git ls-files -s | sed "s-\t\"*-&myfolder/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
' --tag-name-filter cat -f -- --all
@hfossli
Copy link

hfossli commented Mar 6, 2017

I'm on a mac and have a problem with that tab character...

hfossli in ~/Downloads/gitplay/first on master λ ls -la
total 8
drwxr-xr-x   4 hfossli  staff  136 Mar  6 09:07 .
drwxr-xr-x   6 hfossli  staff  204 Mar  6 09:50 ..
drwxr-xr-x  12 hfossli  staff  408 Mar  6 09:50 .git
-rw-r--r--   1 hfossli  staff   20 Mar  6 09:50 test.txt
hfossli in ~/Downloads/gitplay/first on master λ git ls-files -s | sed "s-\t\"*-&myfolder/-"
100644 bff22c96836a98b1f76cdc197d2796b2191dd53a 0	tmyfolder/est.txt

I expected

hfossli in ~/Downloads/gitplay/first on master λ git ls-files -s | sed "s-\t\"*-&myfolder/-"
100644 bff22c96836a98b1f76cdc197d2796b2191dd53a 0	myfolder/test.txt

@hfossli
Copy link

hfossli commented Mar 6, 2017

Got it working with this instead!

git filter-branch --index-filter \
    'git ls-files -s | sed -E "s/( [a-z0-9]{40} 0.)/\1first\//" |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
    git update-index --index-info &&
    mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
    ' --tag-name-filter cat -f -- --all

@hfossli
Copy link

hfossli commented Mar 6, 2017

I also had a problem with empty git commits. Therefore I added a check for those situations

git filter-branch --index-filter \
    'git ls-files -s | sed -E "s/( [a-z0-9]{40} 0.)/\1ios\//" |
    GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
    git update-index --index-info &&
    if [ -f "$GIT_INDEX_FILE.new" ]; then mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"; fi
    ' --tag-name-filter cat -f -- --all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment