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

纵饮孤独 提交于 2019-12-05 02:01:35

问题


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?


回答1:


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



回答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"


来源:https://stackoverflow.com/questions/3058560/how-do-i-change-a-files-path-in-gits-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!