Git remote operation detailed _ other synthesis

Source: Internet
Author: User
Tags ssh using git git clone git commands

Git is the most popular version management system at the moment, and learning to git is almost an essential skill for developers.
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/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/

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. Please refer to the official documentation for detailed discussions on the pros and cons of various protocols.

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.

$ 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 tracking branch, then the hostname can be omitted.

$ git push
If the current branch has a tracing relationship with more than one host, you can use the-u option to specify a default host so that you can use git push without any arguments later.

$ Git push-u Origin Master
The above command pushes the local master branch to the origin host, specifying origin as the default host, followed by using git push without any parameters.
git push with no arguments, by default, pushes only the current branch, which is called the simple method. In addition, there is a matching way to push all local branches that have a corresponding remote branch. Before Git 2.0, the default is to use the matching method, which is now by default in simple mode. 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, the--all option is required to push all branches of the locality to a remote host, regardless of whether or not there is a corresponding remote branch.

$ git push--all origin
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 local git pull merge the differences before pushing to the remote host. At this point, if you are sure to push, you can use the--force option.

$ git push--force origin
The above command uses the--force option, resulting in a "non-straight" merge (Non-fast-forward merge) on the remote host. Unless you're sure you want to do this, avoid using the--force option as much as possible.
Finally, git push does not push the tag (tag) unless the--tags option is used.

$ GIT push origin--tags

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.