Detailed explanation of Git remote operation

Git is currently the most popular version management system, and learning Git has almost become a necessary skill for developers.

Git has many advantages, one of which is that it is very easy to operate remotely. This article introduces 5 Git commands, their concepts and usage in detail. After understanding these contents, you will completely master Git remote operation.

git clone
git remote
git fetch
git pull
git push

This article is aimed at beginners, starting from the simplest, but requires readers to have an understanding of the basic usage of Git. At the same time, this article covers almost all the common usages of the above five commands, so it also has reference value for skilled users.


1. git clone
The first step in remote operation is usually to clone a repository from a remote host, in which case the git clone command is used.
$ git clone <url of repository>
For example, clone the jQuery repository.
$ git clone https://github.com/jquery/jquery.git
This command will generate a directory on the local host with the same name as the repository on the remote host. 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 <url of repository> <local directory name>
git clone supports multiple protocols, in addition to HTTP(s), it also supports SSH, Git, local file protocol, etc. 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://example.com/path/to/repo.git/
There is another way of writing the SSH protocol.
$ git clone [user@]example.com:path/to/repo.git/

Generally speaking, the Git protocol has the fastest download speed, and the SSH protocol is used for occasions that require user authentication. For a detailed discussion of the pros and cons of various protocols, please refer to the official documentation.

二、git remote
For ease of administration, Git requires that each remote host must specify a hostname. The git remote command is used to manage hostnames.
With no options, the git remote command lists all remote hosts.
$ git remote
origin
With the -v option, you can see the URL of the remote host.
$ git remote -v
origin  [email protected]:jquery/jquery.git (fetch)
origin  [email protected]: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 origin by Git. If you want to use another hostname, you need to specify it with the -o option of the git clone command.
$ git clone -o jQuery https://github.com/jquery/jquery.git
$ git remote
jQuery
The above command indicates that when cloning, the specified remote host is called jQuery.
The git remote show command plus the host name can view the detailed information of the host.
$ git remote show <hostname>
The git remote add command is used to add remote hosts.
$ git remote add <hostname> <url>
The git remote rm command is used to remove a remote host.
$ git remote rm <hostname>
The git remote rename command is used to rename the remote host.
$ git remote rename <old hostname> <new hostname>
3. git fetch
Once the repository on the remote host has been updated (called commit in Git terminology), these updates need to be fetched back to the local, then the git fetch command is used.
$ git fetch <remote hostname>
The above command will retrieve all the updates of a remote host to the local.
The git fetch command is often used to view other people's processes, because the code it fetches has no effect on your local development code.
By default, git fetch fetches updates for all branches. If you only want to get back updates from a specific branch, you can specify the branch name.
$ git fetch <remote hostname> <branch name>
For example, fetch the master branch of the origin host.
$ git fetch origin master
The retrieved updates are read on the local host as "remote hostname/branchname". For example, the master of the origin host should be read with origin/master.

The -r option of the git branch command can be used to view remote branches, and the -a option to view all branches.
$ git branch -r
origin/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
based on it using the git checkout command.
$ git checkout -b newBrach origin/master
The above command means to create a new branch on the basis of origin/master.
In addition, you can also use the git merge command or the git rebase command to merge the remote branch on the local branch.
$ git merge origin/master
# or
$ git rebase origin/master

The above command means to merge origin/master on the current branch.

Fourth, git pull
The function of the git pull command is to retrieve the update of a branch of the remote host, and then merge it with the specified local branch. Its full format is slightly more complicated.
$ git pull <remote hostname> <remote branch name>:<local branch name>
For example, to retrieve the next branch of the origin host and merge it with the local master branch, it needs to be written as follows.
$ git pull origin next:master
If the remote branch is merged with the current branch, the part after the colon can be omitted.
$ git pull origin next
The above command indicates that the origin/next branch is retrieved and merged with the current branch. In essence, this is equivalent to doing a git fetch first, followed by a git merge.
$ git fetch origin
$ git merge origin/next
In some cases, Git will automatically establish a tracking relationship between the local branch and the remote branch. For example, during git clone, all local branches default to the remote host's branch with the same name to establish a tracking relationship, that is, the local master branch automatically "tracks" the origin/master branch.

Git also allows manual tracking of relationships.
$ git branch --set-upstream master origin/next
The above command specifies that the master branch tracks the origin/next branch.
If the current branch has a tracking relationship with the remote branch, git pull can omit the remote branch name.
$ git pull origin
The above command indicates that the current local branch is automatically merged with the corresponding origin host "remote-tracking branch".
If the current branch has only one tracking branch, even the remote hostname can be omitted.
$ git pull
The above command means that the current branch is automatically merged with the only tracking branch.
If the merge needs to be in rebase mode, you can use the --rebase option.
$ git pull --rebase <remote hostname> <remote branch name>:<local branch name>
If the remote host deletes a branch, by default, git pull will not delete the corresponding local branch when pulling the remote branch. This is to prevent git pull from unknowingly deleting the local branch due to someone else operating the remote host.
However, you can change this behavior by adding the parameter -p to delete the remote deleted branch locally.
$ git pull -p
# Equivalent to the following command
$ git fetch --prune origin
$ git fetch -p
Five, git push
The git push command is used to push updates from the local branch to the remote host. Its format is similar to the git pull command.
$ git push <remote hostname> <local branch name>:<remote branch name>
Note that the branch push order is written as <source>:<destination>, so git pull is <remote branch>:<local branch>, and git push is <local branch>:<remote branch>.

If the remote branch name is omitted, it means that the local branch will be pushed to the remote branch with which it has a "tracking relationship" (usually the two have the same name). If the remote branch does not exist, it will be created.
$ git push origin master
The above command means to push the local master branch to the master branch of the origin host. If the latter does not exist, it will be created.
If the local branch name is omitted, it means to delete the specified remote branch, as this is equivalent to pushing an empty local branch to the remote branch.
$ git push origin :master
# Equivalent to
$ git push origin --delete master
The above command means to delete the master branch of the origin host.
If there is a tracking 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 means to push the current branch to the corresponding branch of the origin host.
If the current branch has only one tracking branch, the hostname 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.
$ git push -u origin master
The above command pushes the local master branch to the origin host, and specifies origin as the default host, and then you can use git push without any parameters.

Git push without any parameters will only push the current branch by default, which is called the simple method. In addition, there is a matching method that will push all local branches with corresponding remote branches. Before Git 2.0, the matching method was used by default, and now the simple method is used by default. If you want to modify this setting, you can use the git config command.
$ git config --global push.default matching
# or
$ git config --global push.default simple
There is another case, that is, regardless of whether there is a corresponding remote branch, all local branches are pushed to the remote host. In this case, the --all option needs to be used.
$ git push --all origin
The above command means to push all local branches to the origin host.

If the version of the remote host is newer than the local version, Git will report an error when pushing, requiring you to do git pull locally to merge the differences, and then push to the remote host. At this point, if you must push, you can use the --force option.
$ git push --force origin
The above command uses the --force option, which results in the updated version on the remote host being overwritten. You should try to avoid using the --force option unless you're pretty sure you want to.

Finally, git push will not push tags unless the --tags option is used.
$ git push origin --tags





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325580283&siteId=291194637