如何将 git 存储库移动到另一个目录并使该目录成为 git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19097259/
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
How to move a git repository into another directory and make that directory a git repository?
提问by Pranjal Mittal
I have a directory gitrepo1. This directory is a git repository.
我有一个目录gitrepo1。这个目录是一个 git 存储库。
I would like to move this gitrepo1into another directory newrepo.
Directory newreposhould be the new git repository with no loss of git history and should contain the directory gitrepo1.
Directory gitrepo1should just be a directory now (inside newrepo), without any
.git
index, i.e. it should NO longer be an independent git repository or a submodule.
我想将此gitrepo1移动到另一个目录newrepo。
目录newrepo应该是没有丢失 git 历史记录的新 git 存储库,并且应该包含目录gitrepo1。
目录gitrepo1现在应该只是一个目录(在newrepo 内),没有任何
.git
索引,即它不再是一个独立的 git 存储库或子模块。
How can I do this?
我怎样才能做到这一点?
回答by Shahbaz
It's very simple. Git doesn't care about what's the name of its directory. It only cares what's inside. So you can simply do:
这很简单。Git 并不关心它的目录的名称是什么。它只关心里面的东西。所以你可以简单地做:
# copy the directory into newrepo dir that exists already (else create it)
$ cp -r gitrepo1 newrepo
# remove .git from old repo to delete all history and anything git from it
$ rm -rf gitrepo1/.git
Note that the copy is quite expensive if the repository is large and with a long history. You can avoid it easily too:
请注意,如果存储库很大且历史悠久,则副本非常昂贵。您也可以轻松避免它:
# move the directory instead
$ mv gitrepo1 newrepo
# make a copy of the latest version
# Either:
$ mkdir gitrepo1; cp -r newrepo/* gitrepo1/ # doesn't copy .gitignore (and other hidden files)
# Or:
$ git clone --depth 1 newrepo gitrepo1; rm -rf gitrepo1/.git
# Or (look further here: http://stackoverflow.com/q/1209999/912144)
$ git archive --format=tar --remote=<repository URL> HEAD | tar xf -
Once you create newrepo
, the destination to put gitrepo1
could be anywhere, even inside newrepo
if you want it. It doesn't change the procedure, just the path you are writing gitrepo1
back.
创建后newrepo
,放置的目的地gitrepo1
可以是任何地方,newrepo
如果您愿意,甚至可以在里面。它不会改变程序,只会改变你写gitrepo1
回的路径。
回答by emremrah
It's a bit late and the question is already answered but, to do it without any headache:
有点晚了,问题已经回答了,但是,要做到这一点,不要头疼:
- Check out what's the current branch you in in the older git folder with
git status
, let's say branch development - Change directory to the new git folder, then
git clone
the project from repo to the new folder - Checkout the current branch in your new folder:
git checkout development
- Syncronize your new folder with the older one, using
rsync
, excluding .git folder:rsync -azv --exclude '.git' gitrepo1 newrepo/gitrepo1
- 在旧的 git 文件夹中查看您当前所在的分支
git status
,假设分支开发 - 将目录更改为新的 git 文件夹,然后
git clone
将项目从 repo 更改为新文件夹 - 在新文件夹中签出当前分支:
git checkout development
- 使用
rsync
,不包括 .git 文件夹,将新文件夹与旧文件夹同步:rsync -azv --exclude '.git' gitrepo1 newrepo/gitrepo1
Then you are good to continue where you left off
然后你就可以从你离开的地方继续了
回答by david.pfx
It's even simpler than that. Just did this (on Windows, but it should work on other OS):
它甚至比这更简单。刚刚这样做(在 Windows 上,但它应该适用于其他操作系统):
- Create newrepo.
- Move gitrepo1into newrepo.
- Move .gitfrom gitrepo1to newrepo(up one level).
- Commit changes (fix tracking as required).
- 创建新仓库。
- 将gitrepo1移动到newrepo。
- 将.git从gitrepo1移动到newrepo(上一级)。
- 提交更改(根据需要修复跟踪)。
Git just sees you added a directory and renamed a bunch of files. No biggie.
Git 只是看到您添加了一个目录并重命名了一堆文件。没什么大不了的。