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 the second parameter of the git clone command.
git clone supports a variety of protocols, in addition to HTTP (s), also supports 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. The git remote command is used to manage host names.
With no options, the git remote command lists all remote hosts.
$ git remoteorigin
With the-v option, you can 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 as Origin by Git. If you want to use a different host name, you need to specify it with the-o option of the git clone 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.
The GIT Remote show command plus the host name allows you to view the details of the host.
The GIT remote add command is used to add a remote host.
The GIT remote RM command is used to remove a remote host.
The GIT remote rename command is used to change the host's name.
Third, git fetch
The git fetch command is used when the remote host's repository is updated (git terminology is called commit) and needs to be retrieved locally.
The above command retrieves the updates for a remote host locally.
By default, git fetch retrieves updates for all branches (branch). If you want to retrieve only updates for a particular branch, you can specify a branch name.
For example, retrieve the master branch of the Origin 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, the Master of Origin host, must read with Origin/master.
The-r option of the git Branch command can be used to view the remote branch, the-a option 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 and the remote branch is origin/master.
After retrieving the updates from the remote host, you can create a new branch using the git Checkout command on its basis.
-b newBrach origin/master
The above command indicates that, on the basis of Origin/master, a new branch is created.
Alternatively, you can use the git merge command or the git rebase command to merge remote branches on the local branch.
$ git merge origin/master# 或者$ git rebase origin/master
The above command represents the merging of Origin/master on the current branch.
Iv. git Pull
The purpose of the git pull command is to retrieve updates from a branch of a remote host and merge with the local designated branch. Its full format is slightly more complex.
For example, retrieving the next branch of the Origin host, merging with the local master branch, needs 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. Essentially, this is equivalent to doing git fetch first and then 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, in Git clone, all local branches default to the remote host with the same name branch, establish a tracking relationship, that is, the local master branch automatically "track" Origin/master branch.
Git also allows you to manually establish tracking relationships.
--set-upstream master origin/next
The above command specifies that the master branch tracks the Origin/next branch.
If the current branch has a tracing relationship with the remote branch, git pull can omit the remote branch name.
$ 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 the merge requires Rebase mode, you can use the--rebase option.
--rebase <远程主机名> <远程分支名>:<本地分支名>
If a remote host deletes a branch, by default, git pull does not delete the corresponding local branch while pulling the remote branch. This is to prevent git pull from inadvertently deleting the local branch because someone else is operating the remote host.
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
The git push 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 is < remote branch >:< local branch, while 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 the master branch of the Origin host. 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 that the master branch of the origin host is deleted.
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 the corresponding branch of the Origin 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 multiple hosts, you can use the-u option to specify a default host so that you can use Git push without any parameters later.
-u origin master
The above command pushes the local master branch to the origin host, specifying origin as the default host, and then using git push without any parameters.
git push without any parameters pushes only the current branch by default, which is called simple mode. 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--all 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 Origin host.
If the version of the remote host is newer than the local version, Git will give an error when pushing, requiring that the git pull merge differences be made locally before being pushed to the remote host. At this point, if you must push, you can use the--force option.
The above command uses the--force option, resulting in an overwritten version of the update on the remote host. Unless you are sure you want to do this, you should avoid using the--force option as much as possible.
Finally, git push does not push tags (tag) unless you use the--TAGS option.
Git remote operation detailed