How do I change a file's path in git's history?
Asked Answered
C

3

4

Here is what I have - a git repo of my code:

projects
       |-proj1 (no git repo here yet)
             |-subproj1 <- current git repo here

Here is what I want - a git repo which is now tracking a new project that uses my code:

projects
       |-proj1 <-git repo moved to here, but still tracking files in subproj1
             |-subproj1 (no git repo here)

I'd like to keep the history intact and therefore the new repository will be referring to files that are one level deeper than the original. What is the most pain free way to do this?

Charles answered 17/6, 2010 at 2:40 Comment(0)
F
13

Rewriting history can be done with the git filter-branch command. In fact, moving a directory tree into a subdirectory is one of the cut&paste-ready examples given in the git filter-branch manpage:

git filter-branch --index-filter '
  git ls-files -s |
  sed "s-\t\"*-&subproj1/-" |
  GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
  mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE
' HEAD
Flowing answered 17/6, 2010 at 15:19 Comment(3)
I'm trying to do something similar to this and notice that my temp index file isn't being written to ( gist.github.com/60d14f45a0b2fb98e641 ). Would there be any valid reason why the temp index isn't being written?Edmiston
The sed command seems flakey; I don't get the expected results.Smash
I also had problems with the sed command. The - as separator is unusual and unusable for directories with hyphens in the name. I used: sed "s#\t\"*#&sub-proj1/#"Tyeshatyg
U
0

Git isn't exactly good at tracking files getting moved around, so if you want to preserve file history, you're going to have to make it so the old repository always had its files in the sub-directory you now need it to be in.

git-filter-branch works decently enough, but is no longer recommended. The man page now points to git-filter-repo, which lets you retroactively move files around (among other things you can do with git-filter-branch) in a much simpler way.

Copied from my own answer here:

export SUBTREE_PREFIX="subproj1"

git remote add -f "${SUBTREE_PREFIX:?}-remote" https://my-git-repo.invalid/subproj1.git

git checkout "${SUBTREE_PREFIX:?}-remote"/master -b "${SUBTREE_PREFIX:?}-master"

# --force is to skip the "freshly cloned repo" check.
# All the refs we'll be operating on are fresh, even if the repo isn't

# Remove --dry-run once you've checked .git/filter-repo/fast-export.filtered
# to be sure that everything is correct.
git filter-repo --refs "${SUBTREE_PREFIX:?}-master" --to-subdirectory-filter "${SUBTREE_PREFIX:?}" --force --dry-run

git checkout master
git merge "${SUBTREE_PREFIX:?}-master" --allow-unrelated-histories

# Repeat for however many repos you need to add

See also How do you merge two Git repositories?

Underbrush answered 6/7, 2022 at 21:35 Comment(0)
W
-2

Just create the directory structure you want inside the repo - i.e. move all files and folders to "subproj1" folder.

Then stage all added and deleted files and git will work out that they are in fact renames:

git add .
git add -u .
git commit -m "Moved to a subfolder"
Weapon answered 17/6, 2010 at 2:49 Comment(1)
That will mean that if I checkout a previous commit, the files will be checked out inside proj1. I don't want that. I want to be able to checkout a previous commit and have the files in subproj1 affected.Charles

© 2022 - 2024 — McMap. All rights reserved.