tangy
tangy

Reputation: 3276

Understand git-filter-branch usage with update-index

I've been trying to decipher the git-filter-branch command used here to rewrite the index such that all the repo contents are within a new directory, which is then used to move this to the subdirectory of another repo. Focussing on the specific command:

git filter-branch --index-filter '
        git ls-files -s |
        sed "s,\t,&'"$dir"'/," |
        GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info &&
        mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
    ' HEAD

This essentially seems to change the index to look from /folder1/path/to/file to 'newrootfolder'/folder1/path/to/file.

Questions:

Upvotes: 2

Views: 461

Answers (1)

VonC
VonC

Reputation: 1327124

GIT_INDEX_FILE is one of the repository environment variables

the path to the index file (non-bare repositories only).

That filter command and example was introduced in June 2007, with this patch

GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info 

That part forces git to re-create a new index file, with the renamed folder in it.

mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"

Once the new index is created, it can replace the legacy one.


Please note the current option (in python, so cross-platform) is to use git filter-repo, which replaces the old obsolete git filter-branch or BFG

git filter-repo --path <olddirectory>/ --path-rename <olddirectory>/:<newdirectory>/

Example:

If you want two directories to be renamed (and maybe merged if both are renamed to the same location), use --path-rename; for example, to rename both cmds/ and src/scripts/ to tools/:

git filter-repo --path-rename cmds:tools --path-rename src/scripts/:tools/

Upvotes: 1

Related Questions