git 获取裸存储库的工作副本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12450245/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:36:13  来源:igfitidea点击:

Getting a working copy of a bare repository

gitworking-directorygit-bare

提问by Jonathan Allard

I have a server on which I have a bare repository for pushing. However, my server needs to have a working copy of the master branch.

我有一台服务器,上面有一个用于推送的裸存储库。但是,我的服务器需要有一个 master 分支的工作副本。

How do I get a working copy and that only from a bare repository?

如何获取工作副本并且仅从裸存储库获取?

回答by user4815162342

You can simply clone the repository to another directory on the same machine:

您可以简单地将存储库克隆到同一台机器上的另一个目录:

git clone /bare/repo/dir

The current directory will become a non-bare clone of your repo, and you'll get a checkout of the masterbranch automatically. Then use the usual commands like git pullto update it as needed.

当前目录将成为您的 repo 的非裸克隆,您将master自动获得分支的结帐。然后使用常用命令如git pull根据需要更新它。

As a side benefit, this operation is very efficient — if you specify a local directory to git clone, git will use hard links to share the read-only parts of the object databases of the two repos.

附带的好处是,这个操作非常有效——如果你指定一个本地目录到git clone,git 将使用硬链接来共享两个 repos 的对象数据库的只读部分。

回答by ygoe

A bare repository is just the .git directory of a working directory, and an entry in the local config file. What I did to convert a bare repository into a full oneis:

裸仓库只是工作目录的 .git 目录,以及本地配置文件中的一个条目。我将裸存储库转换为完整存储库的操作是:

  • Create a new subdirectory .gitand move all files from the bare repository in there
  • Edit the .git/configfile to change bare = trueto bare = false
  • Check out the branch you want. This extracts all files from the repository into the working directory.
  • 创建一个新的子目录.git并将裸存储库中的所有文件移动到那里
  • 编辑.git/config文件以更改bare = truebare = false
  • 查看您想要的分支。这会将存储库中的所有文件提取到工作目录中。

You could set the Hidden attribute on the .gitdirectory on Windows, but noton the files inside the directory.

您可以.git在 Windows上的目录上设置 Hidden 属性,但不能在目录内的文件上设置。

回答by Jonathan Allard

I was looking for the "detached working tree" approach (as seen here):

我一直在寻找的“沾边的工作树”的方法(如看到这里):

git init --bare

git config core.bare false
git config core.worktree /somewhere/else/

git checkout -f

回答by GregT

This is a riff off the other 2 answers but it fills the gap for my use case -- it works for updating the repo from the origin and checking out branches and any git operation because you end up with a normal complete git repo.

这是其他 2 个答案的即兴演奏,但它填补了我的用例的空白——它适用于从源更新 repo 并检查分支和任何 git 操作,因为你最终会得到一个正常的完整 git repo。

git clone /path/to/bare/repo /new/path/to/full/repo
cd /new/path/to/full/repo
git remote set-url origin [email protected]:swift/swift.git

After executing these 3 lines of code you can then git pulland git branchand git checkout some_branchand so on because you now have a normal complete git repo connected to your remote repo.

执行这些3行代码,你可以在此之后git pull,并git branchgit checkout some_branch上,因为你现在有一个正常完整的混帐回购协议连接到您的远程回购等。

回答by Adrian W

This is how it works:

这是它的工作原理:

$ git init --separate-git-dir /path/to/existing-bare-repository /path/to/workdir
$ cd /path/to/workdir
$ git checkout .

Voilà!

瞧!

For info: git initwill report: Reinitialized existing Git repository in /path/to/existing-bare-repository. But be confident. man git-initsays: Running git init in an existing repository is safe. It will not overwrite things that are already there.

有关信息:git init将报告:Reinitialized existing Git repository in /path/to/existing-bare-repository。但要自信。man git-init说: 在现有存储库中运行 git init 是安全的。它不会覆盖已经存在的东西。

The magic is that git initalone does not make your files appear in the working directory. You have to checkout the root directory.

神奇的是,git init单独这样做不会使您的文件出现在工作目录中。您必须签出根目录。

回答by Ben Martin

You can use 'git show' for this.

您可以为此使用“git show”。

http://www.kernel.org/pub/software/scm/git/docs/git-show.html

http://www.kernel.org/pub/software/scm/git/docs/git-show.html

Basically:

基本上:

git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file.txt

回答by Tomas

This works for me:

这对我有用:

git --git-dir=path-to-bare-repo.git --work-tree=path-to-target checkout master .

git --git-dir=path-to-bare-repo.git --work-tree=path-to-target checkout master .

this command will extract all files from the repository to the directory specified by path-to-target

此命令将从存储库中提取所有文件到指定的目录 path-to-target