This article brings you relevant knowledge about Git, which mainly introduces the related issues of git init and git clone to obtain the git warehouse, including git clone from the existing Git storage database Clone the warehouse to a local directory and other related content, I hope it will be helpful to everyone.
Recommended study: "Git Tutorial"
Usually There are two ways to obtain a git repository:
Convert a local directory that is not under version control to a Git repository;
From other servers Clone an existing Git repository;
git init [-q | --quiet] [--bare] [--template=<template_directory>] [--separate-git-dir <git dir>] [--shared[=<permissions>]] [directory]
This command creates an empty Git To store the database, objects
, refs/heads
, refs/tags
, and template files will basically be created in the .git
directory. An initial HEAD file is also created that references the HEAD of the master branch.
If the $GIT_DIR
environment variable is specified, it will replace the ./.git
directory as the basis for a repository.
If the objects
directory is specified through the $GIT_OBJECT_DIRECTORY
environment variable, then the sha1 directory is created in this directory, otherwise it is the default $GIT_DIR/objects
Table of contents.
It is safe to run git init
in an existing Git repository, it will not overwrite existing things. The main reason to rerun git init
is to get the newly added templates (or to move the Git repository to another place in the case of the --separate-git-dir
option).
[-q, --quite]
[--bare]
.git
folder, as follows:
is used to copy the files in the template folder to the storage database when we initialize the Git warehouse. If not specified, the default copy is
/usr Templates under the path /share/git-core/templates, which include the following content: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$ ls /usr/share/git-core/templates/
branches description hooks info</pre><div class="contentsignin">Copy after login</div></div> If you specify your own default path, the initialized Git storage database is as follows:
The templates can be passed in turn through settings,
$GIT_TEMPLATE_DIR environment variable settings,
init.templateDir configuration settings, and override the lower-level settings in turn.
By default will create a
.git in the current directory folder to store the Git database. This command can specify a path to initialize the Git storage database and create a
.git file locally to link to the specified directory:
file locally. The file describes the specific location of the Git storage database of the current warehouse and is automatically linked to it.
Used to specify the read and write permissions of the created Git storage database, including permissions for users in the same group, all users, etc. Setting, if not specified, defaults to permissions. If you are interested, you can
git init --help to view the specific usage of this option.
If this option is specified, the command will be run in this directory, and the directory will be created if it does not exist. .
git clone [--template=<template_directory>] [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror] [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>] [--dissociate] [--separate-git-dir <git dir>] [--depth <depth>] [--[no-]single-branch] [--no-tags] [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules] [--[no-]remote-submodules] [--jobs <n>] [--sparse] [--] <repository> [<directory>]
git branch --remotes), and creates and checks out the currently active branch of the cloned repository to the local initial branch.
After cloning is completed, a
git fetch command without parameters can update all remote tracking branches, and a
git pull command without parameters will also merge the remote master branch into in the current branch.
This default configuration is created by creating a reference to the remote branch head under
refs/remotes/origin and initializing
remote.origin.url and
remote.origin.fetch Implemented by configuration variables.
[--template=<template_directory>]
git init
相关选项获取此选项作用。[-l, --local]
refs
,HEAD
等信息到克隆的Git存储数据库,并将.git/objects
通过硬链接形式链接到本地Git存储库以节约本地空间。-l
选项但[url]
是本地路径则还是会默认进行-l
选项的行为,但是如果指定的是--no-local
选项对本地仓库进行克隆则会走默认的git clone
流程:[-s, --shared]
.git/objects
的对象通过硬链接的方式链接到本地的克隆仓库,使用此选项不会再硬链接.git/objects
目录,而是在本地的.git/objects/info
目录中创建一个alternates
文件并在其中描述objects
原先的位置并进行共享使用。git commit
内部可能自动调用git gc --atuo
)删除的,从而导致仓库被破坏。-s
选项克隆的存储库中运行git repack
时,如果没有指定--local,-l
选项,则会将源存储库中的objects
复制到克隆存储库中的一个包里面,从而消除了--shared
选项带来的共享效果和节省的空间。直接运行git gc
是安全的,因为默认使用的--local,-l
选项。-s
选项指定的仓库中打破对共享的依赖,则可以使用git repack -a
命令将源存储库中的所有对象复制到克隆的存储库的一个包中。[--no-hardlinks]
.git/objects
中的内容而不是使用硬链接的形式,在进行Git存储库备份时这个选项就很有用。[-q, --quite]
[-n, --no-checkout]
[--bare]
<directory>/.git
目录也不会将管理文件放到<directory>/.git
中,而是为自己创建一个<directory>
或者<directory>.git
目录,里面保存的就是实际的Git数据库。这个选项也默认是--no-checkout
的,不会检出任何HEAD,也不会自动跟踪任何远程分支,也不会创建相关的配置变量。[--mirror]
--bare
,对比--bare
,--mirror
不仅仅映射源的本地分支到目标的本地分支,它还映射所有引用(包括远程跟踪分支,笔记等),并设置refspec配置,以便所有这些引用都被目标存储库中的git远程更新覆盖。--bare
和--mirror
都是针对服务器使用,因为服务器只需要保存Git存储数据库而不需要实际操作git命令,所以当你在这两个选项创建的存储库执行Git命令会得到下面的打印:fatal: this operation must be run in a work tree
[-o <name>, --origin <name>]
<name>
来跟踪远程仓库。[-b <name>, --branch <name>]
<name>
分支。[-u <upload-pack>, --upload-pack <upload-pack>]
/usr/bin/git-upload-pack
,当服务器的git运行时会自动找到此路径的程序。[--reference[-if-able] <repository>]
.git/objects/info/alternates
file will automatically be set up to fetch objects
from the referencing source repository, using the existing Git repositories instead will require fewer objects
to be copied from the source repository, thus reducing network and local storage costs. When --reference-if-able
is used, non-existing directories are skipped and a warning is issued instead of aborting cloning. [--dissociate]
objects
objects from a Git repository referenced by --reference
only reduces network transmission, and Stop borrowing objects from the reference library after cloning by making the necessary local copies of the borrowed objects
. When a local clone is already borrowing objects
from another repository, this option can be used to stop the new repository from borrowing objects
from the same repository. This option is also mainly used for Git servers. [--separate-git-dir <git dir>]
git init
related options to get the effect of this option. [--depth <depth>]
<depth>
, and get The top commits of all branches will be cloned locally with the number of <depth>
commits. If you also want to simply clone submodules, you can also pass the --shallow-submodules
option. [--[no-]single-branch]
--single-branch
will only clone a specified branch in the Git repository , other branches in the remote Git repository will not be cloned locally, nor will other remote branches be tracked locally, only a single remote branch will be tracked. [--no-tags]
remote.<remote>.tarOpt=--no- in the configuration tags
to ensure that subsequent git pull
and git fetch
will not operate on the tag unless the tag is explicitly manipulated. --single-branch
to maintain a single branch, which is useful when only maintaining a certain default branch. [--recurse-submodules[=<pathspec>]]
<pathspec>
module, if <pathspec>
is not specified then all submodules are initialized and cloned. This option may be given multiple times for <parhspec>
with multiple entries. git submodule update --init --recursive <pathspec>
. [--[no-]shallow-submodules]
[--[no-]remote-submodules]
--remote
option to git submodule update
. [-j <n>, --jobs <n>]
submodule.fetchJobs
. [--sparse]
[--]
<repository>
https
protocol or ssh
protocol or git
protocol, etc. [<directory>]
-v, --verbose
[-c <key>=<value>, --config <key>=<value]
Recommended learning: "Git Tutorial"
The above is the detailed content of Detailed examples of git init and git clone to obtain git warehouse. For more information, please follow other related articles on the PHP Chinese website!