Home > Development Tools > git > body text

Detailed examples of git init and git clone to obtain git warehouse

WBOY
Release: 2022-04-02 13:18:29
forward
3629 people have browsed it

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.

Detailed examples of git init and git clone to obtain git warehouse

Recommended study: "Git Tutorial"

Use git init and git clone to obtain the git warehouse

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;

1 git init creates a Git repository in the local directory

git init [-q | --quiet] [--bare] [--template=<template_directory>]
           [--separate-git-dir <git dir>]
           [--shared[=<permissions>]] [directory]
Copy after login

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]
    Only print error messages and warning messages;
  • [--bare]
    Create a bare warehouse, excluding the .git folder, as follows:
    git init --bare
  • ##[--template=<template_directory>] is used to copy the files in the template folder to the
    .git 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: git init --template The templates can be passed in turn through
    - -template= settings, $GIT_TEMPLATE_DIR environment variable settings, init.templateDir configuration settings, and override the lower-level settings in turn.
  • [--separate-git-dir <git dir>] By default
    git init 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:
    git init --separate You can see that there is only one
    .git file locally. The file describes the specific location of the Git storage database of the current warehouse and is automatically linked to it.
  • [--shared[=]] 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
    group permissions. If you are interested, you can git init --help to view the specific usage of this option.
  • [directory] If this option is specified, the
    git init command will be run in this directory, and the directory will be created if it does not exist. .

2 git clone Clone the warehouse from the existing Git storage database to the local directory
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>]
Copy after login

Clone a warehouse to the newly created directory, for Each branch in the cloned Git repository creates a remote tracking branch (the tracking branch can be viewed through

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]
    用于从本地Git存储仓库克隆Git存储数据库,此选项会拷贝本地的refs,HEAD等信息到克隆的Git存储数据库,并将.git/objects通过硬链接形式链接到本地Git存储库以节约本地空间。
    如果未指定-l选项但[url]是本地路径则还是会默认进行-l选项的行为,但是如果指定的是--no-local选项对本地仓库进行克隆则会走默认的git clone流程:
    git clone --no-local
  • [-s, --shared]
    当克隆的仓库在本地时,默认是将本地仓库中.git/objects的对象通过硬链接的方式链接到本地的克隆仓库,使用此选项不会再硬链接.git/objects目录,而是在本地的.git/objects/info目录中创建一个alternates文件并在其中描述objects原先的位置并进行共享使用。
    注意:这个选项是一个危险的选项,除非你明白它的作用,否则不要使用它。如果使用这个选项克隆了本地仓库,然后删除了源仓库中的分支,一些对象可能会变成未被引用状态。而这些对象是可能被git的命令(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]
    克隆完成后不执行检出HEAD操作:
    git clone -n
  • [--bare]
    创建一个裸的Git仓库。也就是说不创建<directory>/.git目录也不会将管理文件放到<directory>/.git中,而是为自己创建一个<directory>或者<directory>.git目录,里面保存的就是实际的Git数据库。这个选项也默认是--no-checkout的,不会检出任何HEAD,也不会自动跟踪任何远程分支,也不会创建相关的配置变量。
  • [--mirror]
    设置源Git存储库的镜像。类似于--bare,对比--bare--mirror不仅仅映射源的本地分支到目标的本地分支,它还映射所有引用(包括远程跟踪分支,笔记等),并设置refspec配置,以便所有这些引用都被目标存储库中的git远程更新覆盖。
    注意:--bare--mirror都是针对服务器使用,因为服务器只需要保存Git存储数据库而不需要实际操作git命令,所以当你在这两个选项创建的存储库执行Git命令会得到下面的打印:
    fatal: this operation must be run in a work tree
    Copy after login
  • [-o <name>, --origin <name>]
    未使用此选项时默认使用origin来跟踪远程仓库,使用此选项后使用<name>来跟踪远程仓库。
  • [-b <name>, --branch <name>]
    不要将新创建的HEAD指向克隆仓库HEAD指向的分支,而是指向<name>分支。
  • [-u <upload-pack>, --upload-pack <upload-pack>]
    在使用ssh访问要克隆的Git存储库时,它为另一端运行的命令指定了一个非默认的路径。这个选项主要针对Git服务器使用,为服务器使用的git等指定了一个路径。一般是/usr/bin/git-upload-pack,当服务器的git运行时会自动找到此路径的程序。
  • [--reference[-if-able] <repository>]
    If the referenced Git repository is on the local machine, the .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]
    Borrowing 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>]
    Please see git init related options to get the effect of this option.
  • [--depth <depth>]
    Create a shallow clone with the number of commits that need to be cloned specified by <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]
    As the name suggests, --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]
    Will not clone any tags and set 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.
    Can be used with --single-branch to maintain a single branch, which is useful when only maintaining a certain default branch.
  • [--recurse-submodules[=<pathspec>]]
    After the clone is created, initialize and clone the submodules according to the provided <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.
    Using this option by default is equivalent to running git submodule update --init --recursive <pathspec>.
  • [--[no-]shallow-submodules]
    All cloned submodules have a shallow clone depth of 1.
  • [--[no-]remote-submodules]
    Update the status of the remote tracking branch of all cloned submodules to update the submodule instead of recording it in the Git database SHA1. Equivalent to passing the --remote option to git submodule update.
  • [-j <n>, --jobs <n>]
    The number of submodules fetched at the same time, the default is configurationsubmodule.fetchJobs .
  • [--sparse]
    Sparse checkout mode, the so-called sparse checkout means that when checking out the local repository, it does not check out all, but only removes the specified files from the local repository. Checked out to the workspace, other unspecified files are not checked out (even if these files exist in the workspace, their modifications will be ignored). This feature is not described in detail here.
  • [--]
    Has no practical effect, just to separate options and operation objects for easy differentiation.
  • <repository>
    The warehouse to be cloned may be a remote warehouse or a local warehouse, and it may be https protocol or ssh protocol or git protocol, etc.
  • [<directory>]
    If this directory is specified, the Git repository will be cloned into this directory.
  • -v, --verbose
    Verbose output of clone information.
  • [-c <key>=<value>, --config <key>=<value]
    Store the newly created Git when cloning the repository The library sets a configuration variable, which takes effect immediately after the cloning is completed

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!

Related labels:
git
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template