Skip to content

Instantly share code, notes, and snippets.

@iluwatar
Created February 21, 2015 11:19
Show Gist options
  • 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

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