Git remote operations

Source: Internet
Author: User
Tags git commands

Git is currently the most popular version management system. Learning git has almost become a required skill for developers.

Git has many advantages, one of which is that remote operations are very simple. This article describes five git commands in detail, their concepts and usage, and understanding the content, you will have a full grasp of git remote operations.

  • Git clone
  • Git remote
  • Git fetch
  • Git pull
  • Git push

This article is intended for novice users, starting with the simplest, but readers need to understand the basic usage of git. At the same time, this article covers almost all the common usage of the above five commands, so it also has reference value for skilled users.

1. Git clone

The first step in remote operations is to clone a version library from the remote host. In this case, you need to use the GIT clone command.

$ Git clone <repository URL>

For example, clone the jquery version library.

$ git clone https://github.com/jquery/jquery.git

This command will generate a directory on the local host, with the same name as the version library of the remote host. If you want to specify different directory names, you can use the directory name as the second parameter of the GIT clone command.

$ Git clone <repository URL> <local directory NAME>

Git clone supports multiple protocols. In addition to HTTP (s), it also supports SSH, git, and local file protocols. Below 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 to write the SSH protocol.

$ git clone [[email protected]]example.com:path/to/repo.git/

Generally, the GIT protocol downloads the fastest, and the SSH protocol is used for scenarios requiring user authentication. For details about the advantages and disadvantages of various protocols, refer to the official documentation.

Ii. Git remote

To facilitate management, git requires that each remote host must specify a host name. The GIT remote command is used to manage the host name.

If no option is available, the GIT remote command lists all remote hosts.

$ git remoteorigin

Use the-V option to view the remote host URL.

$ git remote -vorigin  [email protected].com:jquery/jquery.git (fetch)origin  [email protected].com:jquery/jquery.git (push)

The preceding command indicates that there is currently only one remote host called origin and its URL.

When cloning the version library, the remote host used is automatically named as origin by git. To use another host name, use the-O option of the GIT clone command.

$ git clone -o jQuery https://github.com/jquery/jquery.git$ git remotejQuery

The preceding command indicates that the remote host is called jquery during cloning.

Run the GIT remote show command with the host name to view the details of the host.

$ Git remote show <Host Name>

The GIT remote add command is used to add a remote host.

$ Git remote add <Host Name> <URL>

The GIT remote RM command is used to delete a remote host.

$ Git remote Rm <Host Name>

The GIT remote RENAME command is used to rename the remote host.

$ Git remote rename <original host name> <New Host Name>
Iii. Git fetch

Once the version library of the remote host has been updated (git is called commit), you need to retrieve these updates locally and then use the git fetch Command.

$ Git fetch <Remote Host Name>

The above command Retrieves all updates from a remote host to the local device.

By default, git fetch retrieves updates from all branches. If you only want to retrieve updates from a specific branch, you can specify the branch name.

$ Git fetch <Remote Host Name> <branch name>

For example, retrieve the master branch of the origin host.

$ git fetch origin master

The retrieved update must be read on the local host in the form of "remote host name/branch name. For example, the master of the origin host must be read by the origin/master.

The-r option of the GIT branch command can be used to view remote branches, and the-A option can view all branches.

$ git branch -rorigin/master$ git branch -a* master  remotes/origin/master

The preceding command indicates that the current branch of the local host is master and the remote branch is origin/master.

After retrieving updates from the remote host, you can use the GIT checkout command to create a new branch.

$ git checkout -b newBrach origin/master

The preceding command indicates that a new branch is created based on the origin/master.

In addition, you can use the GIT merge command or git rebase command to merge remote branches on the local branch.

$ Git merge origin/master # Or $ git rebase origin/Master

The preceding command indicates that the origin/master is merged on the current branch.

Iv. Git pull

The GIT pull command is used to retrieve updates from a branch on the remote host and merge the updates with a specified branch on the local host. Its complete format is a little complicated.

$ Git pull <Remote Host Name> <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, you need to write it 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 preceding command indicates that the origin/next branch is retrieved and then merged with the current branch. In essence, this is equivalent to running git fetch and git merge.

$ git fetch origin$ git merge origin/next

In some cases, git Automatically Establishes a tracking relationship between the local branch and the remote branch ). For example, during git clone, all local branches are connected to the branch with the same name as the remote host by default. That is to say, the local master branch automatically "tracks" origin/Master branch.

GIT also allows manual tracing.

git branch --set-upstream master origin/next

The above command specifies the master branch to trace 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 preceding command indicates that the current local branch is automatically merged with the corresponding remote-tracking branch of the origin host.

If the current branch has only one tracing branch, the remote host name can be omitted.

$ git pull

The preceding command indicates that the current branch is automatically merged with the only tracing branch.

If the merge operation requires the rebase mode, you can use the -- rebase option.

$ Git pull -- rebase <Remote Host Name> <remote branch name>: <local branch name>
5. Git push

The GIT push command is used to push updates of local branches to remote hosts. Its format is similar to that of the GIT pull command.

$ Git push <Remote Host Name> <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>, while git push is <local branch>: <remote branch>.

If the remote branch name is omitted, the local branch is pushed to a remote branch with a "tracing relationship" (usually with the same name). If the remote branch does not exist, it is created.

$ git push origin master

The command above 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 the local branch name is omitted, the specified remote branch is deleted, because it is equivalent to pushing an empty local branch to the remote branch.

$ Git push origin: master # equivalent to $ git push Origin -- delete master

The preceding command deletes the master branch of the origin 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 command above indicates that the current branch is pushed to the corresponding branch of the origin host.

If the current branch has only one tracing branch, the host name can be omitted.

$ git push

If the current branch has a tracing relationship with multiple hosts, you can use the-u option to specify a default host, so that you can use git push without adding any parameters.

$ git push -u origin master

The above command pushes the local master branch to the origin host, and specifies the origin as the default host, so you can use git push without adding any parameters.

Git push without any parameters. By default, only the current branch is pushed, which is called the simple method. In addition, there is also a matching method that pushes all local branches with corresponding remote branches. Before git version 2.0, the matching method was used by default. 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

In another case, whether or not the corresponding remote branch exists, push all the local branches to the remote host. In this case, you need to use the -- all option.

$ git push --all origin

The command above indicates that all local branches are pushed to the origin host.

If the version of the remote host is later than the local version, git reports an error when pushing. You must merge the differences locally before pushing them to the remote host. In this case, you can use the -- force option if you must push it.

$ git push --force origin 

The preceding command uses the -- force option, resulting in a "non-straight forward" Merge (non-fast-forward merge) on the remote host ). Unless you are sure to do so, try to avoid using the -- force option.

Finally, git push does not push tags unless the -- tags option is used.

$ git push origin --tags

(End)

Http://www.ruanyifeng.com/blog/2014/06/git_remote.html

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.