Git tutorial: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000
Git is by far the most popular version management system, and learning git is almost a necessary skill for developers.
One of the advantages of git is that it's very easy to operate remotely. This article details 5 git commands, their concepts and usage, and you'll get a complete grasp of git remote operations.
- git clone
- Git remote
- git fetch
- Git pull
- git push
For beginners, this article starts with the simplest, but requires a reader to understand the basic uses of Git. At the same time, this article covers almost all of the above 5 commands of common use, so for the skilled users also have reference value.
One, Git clone
The first step in a remote operation is to clone a repository from a remote host and use the
git clone command.
For example, clone the repository of jquery.
$ git clone https://github.com/jquery/jquery.git
The command generates a directory on the local host with the same name as the remote host's repository. If you want to specify a different directory name, you can use the directory name as
git clone the second parameter of the command.
git cloneSupport for a variety of protocols, in addition to HTTP (s), but also support SSH, Git, local file protocol, and so on, here are some examples.
$ git clone http[s]://example. com/path/to/repo.git/$ git clone ssh://example. com/path/to/repo.git/$ git clone git://example. com/path/to/repo.git/$ git clone/opt/git/project.git $ git clone file:///opt/ git/project.git$ git clone ftp[s]://example .com/path/to/repo.git/$ git clone rsync:/< Span class= "token operator" >/example.com/path/to/repo.git/
There is another way of writing the SSH protocol.
In general, the GIT protocol is the fastest to download and the SSH protocol is used for applications requiring user authentication. For a detailed discussion of the pros and cons of the various protocols, please refer to official documentation.
Second, git remote
For ease of administration, GIT requires that each remote host must specify a host name.
git remotecommand is used to manage host names.
With no options, the
git remote command lists all remote hosts.
$ git remoteorigin
-v option to see the URL of the remote host.
-vorigin [email protected].com:jquery/jquery.git (fetch)origin [email protected].com:jquery/jquery.git (push)
The above command indicates that there is currently only one remote host, called Origin, and its URL.
When cloning a repository, the remote host used is automatically named by Git
origin . If you want to use a different host name, you need to specify it with
git clone the option of a command
-o jQuery https://github.com/jquery/jquery.git$ git remotejQuery
The above command indicates that when cloning, the specified remote host is called jquery.
git remote showcommand with the host name, you can view the details of the host.
git remote addcommand to add a remote host.
git remote rmcommand to delete a remote host.
git remote renameThe command is used for renaming the remote host.
Third, git fetch
Once the repository for the remote host has been updated (the git term is "commit"), the updates need to be retrieved locally, using the
git fetch command.
The above command retrieves the updates for a remote host locally.
git fetchCommands are often used to view other people's processes because the code they retrieve does not affect your local development code.
git fetch all branches (branch) are retrieved for updates. If you want to retrieve only updates for a particular branch, you can specify a branch name.
For example, retrieve
origin the branch of the host
$ git fetch origin master
The retrieved updates are read in the form of a "remote hostname/branch name" on the local host. For example
origin , the host
master , you need to
git branchThe options for the command
-r can be used to view the remote branch,
-a options to view all branches.
-rorigin/master$ git branch -a* master remotes/origin/master
The above command indicates that the current branch of the local host is
master , the remote branch is
After retrieving the update for the remote host, you can
git checkout create a new branch using the command on its basis.
-b newBrach origin/master
The above command indicates that, on
origin/master the basis of, a new branch is created.
Alternatively, you can use
git merge commands or
git rebase commands to merge remote branches on the local branch.
$ git merge origin/master# 或者$ git rebase origin/master
The above command represents the merge on the current branch
Iv. git Pull
git pullThe purpose of the command is to retrieve updates from a branch of a remote host and merge with the local specified branch. Its full format is slightly more complex.
For example, retrieving
origin a host's
next branch and merging it with a local
master branch would need to be written as follows.
$ git pull origin next:master
If the remote branch is merged with the current branch, the part following the colon can be omitted.
$ git pull origin next
The above command indicates that the
origin/next branch is retrieved and then merged with the current branch. In essence, this is equivalent to doing it first
git fetch and doing it again
git merge .
$ git fetch origin$ git merge origin/next
In some cases, Git automatically creates a tracking relationship (tracking) between the local branch and the remote branch. For example, at
git clone the time, all local branches default to the remote host with the same name branch, establishing a tracking relationship, that is, the local
master branch automatic "tracking"
Git also allows you to manually establish tracking relationships.
--set-upstream master origin/next
The above command specifies the
master branch tracking
If the current branch has a tracing relationship with the remote branch, the
git pull remote branch name can be omitted.
$ git pull origin
The above command indicates that the local current branch is automatically merged with the corresponding
origin host "Trace Branch" (Remote-tracking Branch).
If the current branch has only one trace branch, even the remote host name can be omitted.
$ git pull
The above command indicates that the current branch is automatically merged with the only one tracking branch.
If merging requires rebase mode, you can use
--rebase <远程主机名> <远程分支名>:<本地分支名>
If a remote host deletes a branch, by default, the
git pull corresponding local branch is not deleted when the remote branch is pulled. This is to prevent the local branch from being inadvertently deleted because other people have manipulated the remote host
git pull .
However, you can change this behavior, plus the parameter
-p will delete the remote deleted branch locally.
-p# 等同于下面的命令$ git fetch --prune origin $ git fetch -p
Five, Git push
git pushThe command is used to push updates to the local branch to the remote host. Its format is similar to the
git pull command.
Note that the branch push sequence is written in < source >:< destination, so
git pull it is < remote branch >:< local branch, and
git push is < local branch >:< remote branch >.
If you omit the remote branch name, it means that the local branch is pushed to the remote branch with which the trace relationship exists (usually the same name), and if the remote branch does not exist, it is created.
$ git push origin master
The above command indicates that the local
master branch is pushed to
origin the branch of the host
master . If the latter does not exist, it will be created.
If you omit the local branch name, it means that the specified remote branch is deleted because it is equivalent to pushing an empty local branch to the remote branch.
:master# 等同于$ git push origin --delete master
The above command indicates
origin the branch that removed the host
If there is a tracing relationship between the current branch and the remote branch, both the local branch and the remote branch can be omitted.
$ git push origin
The above command indicates that the current branch is pushed to
origin the corresponding branch of the host.
If the current branch has only one trace branch, the host name can be omitted.
$ git push
If the current branch has a tracking relationship with more than one host, you can use the
-u option to specify a default host so that you can use it later without any parameters
git push .
-u origin master
The above command pushes the local
master branch to the
origin host and designates it
origin as the default host, which can be used without any parameters
git push .
With no arguments, the
git push default is to push only the current branch, which is called the simple method. In addition, there is a matching method that pushes all local branches that have corresponding remote branches. Prior to Git version 2.0, the default is to use the matching method, which now defaults to Simple mode. If you want to modify this setting, you can use the
git config command.
--global push.default matching# 或者$ git config --global push.default simple
In another case, the option is required to push all local branches to the remote host, regardless of whether there is a corresponding remote branch
The above command indicates that all local branches are pushed to the
If the version of the remote host is newer than the local version, Git will give an error when pushed, requiring that the merge differences be made locally before being
git pull pushed to the remote host. At this point, if you must push, you can use the
The above command uses
--force the option, resulting in an overwritten version of the update on the remote host. Unless you are sure you want to do this, you should try to avoid using
git push tag is not pushed unless the option is used
Git remote operation