Correct pose for git remote operation

Source: Internet
Author: User
Tags git clone git commands

Always use the Sourcetree software to image the operation of git, but feel a lot of git remote operation commands are still necessary

One of the many advantages of git is that remote operations are simple. This article describes 5 git commands in detail, their concepts and usage, and you get a complete grasp of git remote operations. git clone git remote git fetch git pull git push

This article is for beginners, from the simplest, but requires the reader to understand the basic use of git. At the same time, this article covers nearly all the common uses of the above 5 commands, so it is also useful for skilled users. First, git clone

The first step in remote operations is to clone a version library from a remote host, where the git clone command is used.

$ git clone < version library URL >

For example, clone the version library of jquery.

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

This command generates a directory on the local host that has the same name as the remote host's version library. 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 < version library URL > < local directory name >

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 file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

The SSH protocol also has another way of writing.

$ git clone [user@]example.com:path/to/repo.git/

Generally, the GIT protocol is the fastest download, and the SSH protocol is used in situations where user authentication is required. two, git remote

For ease of management, GIT requires that each remote host must specify a host name. The git remote command is used to administer the host name.

With no options, the git remote command lists all the remoted hosts.

$ git remote
Origin

Use the-V option to refer to the remote host's web address.

$ git remote-v
Origin Git@github.com:jquery/jquery.git (Fetch)
Origin Git@github.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 version library, the remote host used is automatically named 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.

$ 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 allows you 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 remove a remote host.

$ git remote RM < host name >

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

$ git Remote Rename < original host name > < new host name >
third, git fetch

Once the remote host's version library has been updated (git term is called commit), the updates need to be retrieved locally, and the git fetch command will be used.

$ git fetch < remote host name >

The above command retrieves a remote host's updates, all of which are local.

By default, GIT fetch retrieves the updates for all branches (branch). If you want to retrieve only the updates for 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 updates that are retrieved are read in the form of "remote host name/branch name" on the local host. For example, Master of the origin host, you need to 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.

$ 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 that the remote branch is origin/master.

After retrieving the update from the remote host, you can create a new branch on its basis using the git checkout command.

$ git checkout-b newbrach origin/master

The above command indicates that a new branch is created on a origin/master basis.

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

$ git Merge Origin/master
# or
$ git rebase origin/master

The above command indicates that the Origin/master is merged on the current branch. four, git pull

The role of the git pull command is to retrieve the update of a branch of a remote host and merge it with the specified branch locally. Its full format is slightly 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 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 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 equates to doing git fetch first and then git merge.

$ GIT fetch origin
$ git Merge Origin/next

In some cases, Git automatically establishes a tracking relationship (tracking) between the local branch and the remote branch. For example, when 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 "trace" Origin/master branch.

Git also allows you to manually establish trace relationships.

Git branch--set-upstream Master origin/next

The command above specifies that the master branch traces the Origin/next branch.

If the current branch has a tracing relationship with a 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 tracking branch, the remote host name can be omitted.

$ git pull

The above command indicates that the current branch is automatically merged with the only tracking branch.

If the merge requires Rebase mode, you can use the –rebase option.

$ git pull--rebase < remote host name > < remote branch name >:< local branch name >
Five, git push

The git push command is used to push updates of the local branch to the remote host. Its format is similar to the git pull command.

$ git push < remote host name > < local branch name >:< Remote Branch name >

Note that the branching push order is written in < source >:< destination, so git pull is < remote branch >:< local branch, and git push is the < local branch >:< remote branch >.

If the remote branch name is omitted, the local branch is pushed to the remote branch that has a trace relationship with it (usually the same name) and is created if the remote branch does not exist.

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

$ git push origin:master
# Equal To
$ 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.

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.