[英]How do I change a file's path in git's history?
Here is what I have - a git repo of my code:这是我所拥有的 - 我的代码的 git repo:
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:这就是我想要的——一个 git repo,它现在正在跟踪一个使用我的代码的新项目:
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?做到这一点最无痛的方法是什么?
Rewriting history can be done with the git filter-branch
command.可以使用git filter-branch
命令重写历史记录。 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
手册页中提供的可剪切粘贴示例之一:
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
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 并不擅长跟踪移动的文件,所以如果你想保留文件历史记录,你将不得不这样做,以便旧存储库始终将其文件放在你现在需要的子目录中在。
git-filter-branch
works decently enough, but is no longer recommended . git-filter-branch
工作得很好,但不再推荐。 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.手册页现在指向git-filter-repo
,它允许您以更简单的方式追溯移动文件(以及您可以使用git-filter-branch
执行的其他操作)。
Copied from my own answer here :从我自己的答案复制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?另请参阅如何合并两个 Git 存储库?
Just create the directory structure you want inside the repo - ie move all files and folders to "subproj1" folder.只需在 repo 中创建您想要的目录结构 - 即将所有文件和文件夹移动到“subproj1”文件夹。
Then stage all added and deleted files and git will work out that they are in fact renames:然后暂存所有添加和删除的文件,git 会发现它们实际上是重命名:
git add .
git add -u .
git commit -m "Moved to a subfolder"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.