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"
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>] [--shared[=<permissions>]] [directory]</permissions></git></template_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]
Only print error messages and warning messages; -
[--bare]
Create a bare warehouse, excluding the.git
folder, as follows: - ##[--template=
] is used to copy the files in the template folder to the
.gitstorage 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: <pre class="brush:php;toolbar:false">$ ls /usr/share/git-core/templates/ branches description hooks info</pre> If you specify your own default path, the initialized Git storage database is as follows:
The templates can be passed in turn through
- -template=settings,
$GIT_TEMPLATE_DIRenvironment variable settings,
init.templateDirconfiguration settings, and override the lower-level settings in turn.
- [--separate-git-dir
] By default
git initwill create a
.git in the current directoryfolder to store the Git database. This command can specify a path to initialize the Git storage database and create a
.gitfile locally to link to the specified directory:
You can see that there is only one
.gitfile 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
grouppermissions. If you are interested, you can
git init --helpto view the specific usage of this option.
- [directory]
If this option is specified, the
git initcommand 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>]
[--depth <depth>] [--[no-]single-branch] [--no-tags]
[--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
[--[no-]remote-submodules] [--jobs <n>] [--sparse] [--] <repository>
[<directory>]</directory></repository></n></pathspec></depth></git></repository></upload-pack></name></name></template_directory>
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>]</template_directory>
请看git init
相关选项获取此选项作用。 -
[-l, --local]
用于从本地Git存储仓库克隆Git存储数据库,此选项会拷贝本地的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的命令(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操作: -
[--bare]
创建一个裸的Git仓库。也就是说不创建<directory>/.git</directory>
目录也不会将管理文件放到<directory>/.git</directory>
中,而是为自己创建一个<directory></directory>
或者<directory>.git</directory>
目录,里面保存的就是实际的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
-
[-o <name>, --origin <name>]</name></name>
未使用此选项时默认使用origin来跟踪远程仓库,使用此选项后使用<name></name>
来跟踪远程仓库。 -
[-b <name>, --branch <name>]</name></name>
不要将新创建的HEAD指向克隆仓库HEAD指向的分支,而是指向<name></name>
分支。 -
[-u <upload-pack>, --upload-pack <upload-pack>]</upload-pack></upload-pack>
在使用ssh访问要克隆的Git存储库时,它为另一端运行的命令指定了一个非默认的路径。这个选项主要针对Git服务器使用,为服务器使用的git等指定了一个路径。一般是/usr/bin/git-upload-pack
,当服务器的git运行时会自动找到此路径的程序。 -
[--reference[-if-able] <repository>]</repository>
If the referenced Git repository is on the local machine, the.git/objects/info/alternates
file will automatically be set up to fetchobjects
from the referencing source repository, using the existing Git repositories instead will require fewerobjects
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]
Borrowingobjects
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 borrowedobjects
. When a local clone is already borrowingobjects
from another repository, this option can be used to stop the new repository from borrowingobjects
from the same repository. This option is also mainly used for Git servers. -
[--separate-git-dir <git dir>]</git>
Please seegit init
related options to get the effect of this option. -
[--depth <depth>]</depth>
Create a shallow clone with the number of commits that need to be cloned specified by<depth></depth>
, and get The top commits of all branches will be cloned locally with the number of<depth></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 setremote.<remote>.tarOpt=--no- in the configuration tags</remote>
to ensure that subsequentgit pull
andgit 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>]]</pathspec>
After the clone is created, initialize and clone the submodules according to the provided<pathspec></pathspec>
module, if<pathspec></pathspec>
is not specified then all submodules are initialized and cloned. This option may be given multiple times for<parhspec></parhspec>
with multiple entries.
Using this option by default is equivalent to runninggit submodule update --init --recursive <pathspec></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 togit submodule update
. -
[-j <n>, --jobs <n>]</n></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></repository>
The warehouse to be cloned may be a remote warehouse or a local warehouse, and it may behttps
protocol orssh
protocol orgit
protocol, etc. -
[<directory>]</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><br> Store the newly created Git when cloning the repository The library sets a configuration variable, which takes effect immediately after the cloning is completed</value></key></value></key>
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!

Git and GitHub are key tools for modern software development. Git provides version control capabilities to manage code through repositories, branches, commits and merges. GitHub provides code hosting and collaboration features such as Issues and PullRequests. Using Git and GitHub can significantly improve development efficiency and team collaboration capabilities.

Git is a distributed version control system developed by Linus Torvaz in 2005, and GitHub is a Git-based code hosting platform founded in 2008. Git supports branching and merges through snapshot management files, and GitHub provides pull requests, problem tracking and code review functions to facilitate team collaboration.

Git and GitHub are key tools in modern software development. Git is a distributed version control system, and GitHub is a Git-based code hosting platform. Git's core features include version control and branch management, while GitHub provides collaboration and project management tools. When using Git, developers can track file changes and work together; when using GitHub, teams can collaborate through PullRequests and Issues.

GitHubiscrucialforsoftwaredevelopmentduetoitscomprehensiveecosystemforcodemanagementandcollaboration.Itoffersversioncontrol,communitysupport,andtoolslikeGitHubActionsandPages.Startbymasteringbasicslikecreatingarepository,usingbranches,andautomatingwo

Git and GitHub are essential tools for modern developers. 1. Use Git for version control: create branches for parallel development, merge branches, and roll back errors. 2. Use GitHub for team collaboration: code review through PullRequest to resolve merge conflicts. 3. Practical tips and best practices: submit regularly, submit messages clearly, use .gitignore, and back up the code base regularly.

Git and GitHub are not the same thing: Git is a distributed version control system, and GitHub is an online platform based on Git. Git helps developers manage code versions and achieve collaboration through branching, merge and other functions; GitHub provides code hosting, review, problem management and social interaction functions, enhancing Git's collaboration capabilities.

After installing Git, in order to use more efficiently, the following settings are required: Set user information (name and mailbox) Select text editor Set external merge tool Generate SSH key settings Ignore file mode

Resolve: When Git download speed is slow, you can take the following steps: Check the network connection and try to switch the connection method. Optimize Git configuration: Increase the POST buffer size (git config --global http.postBuffer 524288000), and reduce the low-speed limit (git config --global http.lowSpeedLimit 1000). Use a Git proxy (such as git-proxy or git-lfs-proxy). Try using a different Git client (such as Sourcetree or Github Desktop). Check for fire protection


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software