Detailed examples of git init and git clone to obtain git warehouse
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
1 2 3 |
|
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=<template_directory>]
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: <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
- -template=settings,
$GIT_TEMPLATE_DIRenvironment variable settings,
init.templateDirconfiguration settings, and override the lower-level settings in turn.
- [--separate-git-dir <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 1
2
3
4
5
6
7
8
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 1 2 3 4 5 6 7 8 |
|
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
流程: -
[-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>/.git
中,而是为自己创建一个<directory>
或者<directory>.git
目录,里面保存的就是实际的Git数据库。这个选项也默认是--no-checkout
的,不会检出任何HEAD,也不会自动跟踪任何远程分支,也不会创建相关的配置变量。 -
[--mirror]
设置源Git存储库的镜像。类似于--bare
,对比--bare
,--mirror
不仅仅映射源的本地分支到目标的本地分支,它还映射所有引用(包括远程跟踪分支,笔记等),并设置refspec配置,以便所有这些引用都被目标存储库中的git远程更新覆盖。
注意:--bare
和--mirror
都是针对服务器使用,因为服务器只需要保存Git存储数据库而不需要实际操作git命令,所以当你在这两个选项创建的存储库执行Git命令会得到下面的打印:1
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 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>]
Please seegit 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 setremote.<remote>.tarOpt=--no- in the configuration tags
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>]]
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 runninggit 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 togit 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 behttps
protocol orssh
protocol orgit
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





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

To download projects locally via Git, follow these steps: Install Git. Navigate to the project directory. cloning the remote repository using the following command: git clone https://github.com/username/repository-name.git

Steps to update git code: Check out code: git clone https://github.com/username/repo.git Get the latest changes: git fetch merge changes: git merge origin/master push changes (optional): git push origin master

To delete a Git repository, follow these steps: Confirm the repository you want to delete. Local deletion of repository: Use the rm -rf command to delete its folder. Remotely delete a warehouse: Navigate to the warehouse settings, find the "Delete Warehouse" option, and confirm the operation.

Git Commit is a command that records file changes to a Git repository to save a snapshot of the current state of the project. How to use it is as follows: Add changes to the temporary storage area Write a concise and informative submission message to save and exit the submission message to complete the submission optionally: Add a signature for the submission Use git log to view the submission content

When developing an e-commerce website, I encountered a difficult problem: How to achieve efficient search functions in large amounts of product data? Traditional database searches are inefficient and have poor user experience. After some research, I discovered the search engine Typesense and solved this problem through its official PHP client typesense/typesense-php, which greatly improved the search performance.

To submit an empty folder in Git, just follow the following steps: 1. Create an empty folder; 2. Add the folder to the staging area; 3. Submit changes and enter a commit message; 4. (Optional) Push the changes to the remote repository. Note: The name of an empty folder cannot start with . If the folder already exists, you need to use git add --force to add.

Git code merge process: Pull the latest changes to avoid conflicts. Switch to the branch you want to merge. Initiate a merge, specifying the branch to merge. Resolve merge conflicts (if any). Staging and commit merge, providing commit message.
