将 git 存储库向上移动一个层次结构级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5408475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving a git repository up one hierarchy level
提问by Sorcy
Git beginner question:
Git初学者问题:
I have a small private webproject which is versioned locally with msysgit. There is no exterior repository, as it's only for me, so i can bascially do whatever I want.
我有一个使用 msysgit 在本地进行版本控制的小型私有 web 项目。没有外部存储库,因为它只适合我,所以我基本上可以做任何我想做的事情。
I've had this set up in the project directory, ie in "webroot".
我已经在项目目录中设置了这个,即在“webroot”中。
Now a second directory had to be created, placed parallel to webroot. Let's call it assets.
现在必须创建第二个目录,与 webroot 平行放置。我们称之为资产。
So structure is now as follows:
所以现在结构如下:
\ project directory
----\webroot
----\assets
I'd love to include this new directory in the git repository, so that I'd also version changes to files stored there, but of course I can't use "git add ../assets". Neither am I inclined to create a new git project in project_directory, as this would loose all my previous commits.
我很想在 git 存储库中包含这个新目录,这样我也可以对存储在那里的文件进行版本更改,但我当然不能使用“git add ../assets”。我也不倾向于在 project_directory 中创建一个新的 git 项目,因为这会丢失我以前的所有提交。
So how do I go about moving the repository out of "webroot" up into "project_directory", while keeping my commits and then being able to include "assets"?
那么我如何将存储库从“webroot”移到“project_directory”,同时保持我的提交,然后能够包含“资产”?
采纳答案by Tim Henigan
So, you want your git repo to look like this:
所以,你希望你的 git repo 看起来像这样:
<projectdir>
/.git
/webroot
/assets
To do this, you must move the existing files in your repointo a new webroot
subdirectory.
要做到这一点,必须将现有的文件在您的回购到一个新的webroot
子目录。
cd <git repo root>
mkdir webroot
git mv <all your files> webroot
git commit --all -m "moved all existing files to new 'webroot' directory"
Then, on your local filesystem you want to relocate your clone one directory above where it is now:
然后,在您的本地文件系统上,您希望将您的克隆重新定位到它现在所在的上方一个目录:
cd <projectdir>
mv webroot/* .
rmdir webroot
Then you want to add the assets
directory (and files) to the git repo:
然后你想将assets
目录(和文件)添加到 git repo:
git add assets
git commit -m "added assets to the repo"
回答by braitsch
You can also just move your .git dir up one level and update your worktree.
您也可以将您的 .git 目录向上移动一级并更新您的工作树。
cd projectdir
mv ./webroot/.git ./.git
git config core.worktree /absolute-path-to-project-dir
git add assets
git commit -m 'adding assets folder'
Not positive but I'm pretty sure the path to core.worktree has to be absolute.
不是肯定的,但我很确定 core.worktree 的路径必须是绝对的。
回答by sehe
I assume you meant to rewrite the history to contain all the files in all revisions as if they had always been in a subdirectory webroot/ instead of in the root
我假设您打算重写历史记录以包含所有修订版中的所有文件,就好像它们一直在子目录 webroot/ 中而不是在根目录中一样
The git filter-branch manpage has the answer, here an improved version that rewrites all existing refs (branches) and tags:
git filter-branch 手册页有答案,这里有一个改进的版本,它重写了所有现有的引用(分支)和标签:
time git filter-branch --index-filter 'git ls-files -s |
sed "s-\t\"*-&webroot/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE' --tag-name-filter cat -- --all
Care has been taken to make this an index-only operation so that the process will run fast even for big repos. Remember to (when satisfied) get rid of the original refs (.git/refs/original/*) and repack the repo to loose the obsolete tree objects.
已注意使其成为仅索引操作,以便即使对于大型存储库,该过程也能快速运行。请记住(当满意时)摆脱原始 refs (.git/refs/original/*) 并重新打包 repo 以释放过时的树对象。
回答by Nick
Your commits are not locally tied to the "webroot" folder they are stored within the git repo.
您的提交未在本地绑定到它们存储在 git 存储库中的“webroot”文件夹。
You could simply remove the webroot directory recheckout the repository in the new location "/project directory" add the assets directory and commit.
您可以简单地删除 webroot 目录,重新检出新位置“/project 目录”中的存储库,添加资产目录并提交。
rm -Rf webroot
git clone path-to-repo
git add assets
git commit -m "Added assets directory"
git push
回答by Lars Schneider
The following command would rewrite your Git history. It would look like as if the content was in webroot
all along. Usually rewriting history is troublesome if multiple people are working with a repo. Since you are working alone on it, it should be fine.
以下命令将重写您的 Git 历史记录。看起来好像内容一直在webroot
。如果多人同时使用一个 repo,重写历史通常会很麻烦。既然你一个人在做,应该没问题。
git filter-branch --index-filter '
git read-tree --prefix="webroot/" $GIT_COMMIT && \
git ls-files \
| sed "s/\/.*//" \
| sort \
| uniq \
| grep -v "^webroot" \
| xargs -L1 git rm -r --cached > /dev/null'