简体   繁体   English

如何更改 git 历史记录中的文件路径?

[英]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.

相关问题 如何通过git历史记录查看提交的路径,或“它如何进入当前分支”? - How do I see a commit's path through git history, or “how it got in the current branch”? 如何消除提交以及git历史记录中的更改? - How do I eliminate a commit and it's changes from git history? 如何查看git repo的接收历史记录? - How do I view a git repo's receive history? 如何使用 git 扩展名从历史记录中查看 Excel 文件的旧版本 - How do I use git extensions to view an old version of an Excel file from it's history 如何在 git 中有一个文件,但不是它的历史? - How to have a file in git but not it's history? 如何在 GitK 中查看单个文件的历史记录? - How do I view a single file's history in GitK? 如何维护Git提取到另一个文件的文件内容的更改历史记录? - How to maintain Git's change history of file content which is extracted to another file? 如何更新父存储库的历史记录,以反映使用&#39;git branch-filter&#39;对子模块的历史记录所做的更改? - How do I update a parent repository's history to reflect changes made to a submodule's history done using 'git branch-filter'? 如何搜索我的整个git repo的提交历史记录以进行字符串更改? - How can I search my ENTIRE git repo's commit history for a string change? 如何更改 git 提交者的姓名? - How do I change git committer's name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM