Reputation: 3276
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:
GIT_INDEX_FILE
variable coming from, what is it set to and how does that entire line work?newrootfolder
) get automatically created in the repository - does git internally detect and create the directory?Upvotes: 2
Views: 461
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 bothcmds/
andsrc/scripts/
totools/
:git filter-repo --path-rename cmds:tools --path-rename src/scripts/:tools/
Upvotes: 1