简体   繁体   English

如何从现有的git存储库中创建新的git存储库,第2部分

[英]how to create a new git repository from an existing one, part 2

My question is a direct followup to how to create a new git repository from an existing one . 我的问题是如何从现有库中直接创建新的git库的后续操作。 I have followed the procedure there to "git --bare init" and then "git push ssh://my_host/new_repo +topic1:master". 我按照那里的程序进行“ git --bare init”,然后“ git push ssh:// my_host / new_repo + topic1:master”。 The output from the second step looks like it is copying appropriately, but the new repository does not contain any of the source files and "git status" gives the error "fatal: This operation must be run in a work tree". 第二步的输出看起来像是在正确复制,但是新存储库不包含任何源文件,并且“ git status”给出错误“致命:此操作必须在工作树中运行”。 The anonymized log is below. 匿名日志如下。 What am I missing? 我想念什么? (In case it is not obvious, I am a git newbie.) (以防万一,我是git新手。)

> mkdir foo
> cd foo
> setenv GIT_DIR ./.git
> git --bare init
Initialized empty Git repository in /xxx/foo/.git/
> ls -A
.git
> cd ../bar
> git push ssh://127.0.0.1/xxx/foo +for-pr:master
Counting objects: 10171, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6647/6647), done.
Writing objects: 100% (10171/10171), 7.05 MiB | 9.62 MiB/s, done.
Total 10171 (delta 5461), reused 4992 (delta 2529)
remote: Resolving deltas: 100% (5461/5461), done.
To ssh://127.0.0.1/xxx/foo
 * [new branch]      for-pr -> master
> cd ../foo/
> ls
> git status
fatal: This operation must be run in a work tree

You have created a "bare" repository which contains only the versioning information in a .git subfolder. 您已经创建了一个“裸”存储库,该存储库仅在.git子文件夹中包含版本信息。 You can see this folder with ls -a . 您可以使用ls -a查看此文件夹。 (File and folder names starting with . are considered "hidden" in the Linux world.) (在Linux世界中,以.开头的文件和文件夹名称被视为“隐藏”。)

The bare repo does not contain any local copy of the files. 裸仓库不包含文件的任何本地副本。 This is the whole point of a bare repo. 这就是裸仓库的全部内容。 This blog article has more detailed information about bare git repos. 这篇博客文章提供了有关裸git仓库的更多详细信息。

Note that the command git status cannot be run with a bare repo because it compares the working directory with the current branch. 请注意,命令git status不能与裸仓库一起运行,因为它会将工作目录与当前分支进行比较。

If you want a local working directory, then remove the --bare when you initialize the repo: 如果要本地工作目录,请在初始化存储库时删除--bare

git init

my question is how to make a new project based on (part of) an old one, keeping its history but then building independently from there. 我的问题是如何根据旧项目(的一部分)创建一个新项目,保留其历史记录,然后从那里独立构建。

I 100% disagree with the accepted answer in the link you gave in your original answer. 我100%反对您在原始答案中提供的链接中接受的答案。 A bare repository is not the correct tool for your purpose, nor for that of the person who asked that question. 光秃秃的存储库既不是针对您的目的的正确工具,也不是提出该问题的人的正确工具。

Instead, the intended usage of Git is to create a new branch in your existing repo for continued development.: 相反,Git的预期用途是在您现有的存储库中创建一个新分支以继续开发。

git checkout -b my-new-branch

You can delete all of the other branches if you no longer wish to keep their history. 如果您不再希望保留它们的历史记录,则可以删除所有其他分支。

If you want a completely new repo to work in, then you should clone the existing one: 如果要使用全新的存储库,则应克隆现有的存储库:

cd <parent folder where you want the new repo>
git clone <URL or path to original repo> foo
cd foo

Now foo will contain the copy and you can make a new branch as explainedearlier. 现在foo将包含副本,您可以如先前所述创建新分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM