Git remote operation detailed

Source: Internet
Author: User
Tags using git

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.

$ git clone < repository URL >

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 < repository 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/

There is another way of writing the SSH protocol.

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

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.

$ git remote-vorigin  [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 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.

$ git clone-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.

$ 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 change the host's name.

$ git Remote Rename < original hostname > < new host 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.

$ git fetch < remote host name >

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.

$ git fetch < remote host name > < 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.

$ git branch-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.

$ git checkout-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# or 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.

$ git Pull < remote host name > < remote branch name >:< local branch name >

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.

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 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.

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

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.

$ 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 to 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 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.

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

$ Git push-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.

$ git config--global push.default matching# or 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.

$ 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 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.

$ git push--force origin

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 push origin--tags

The original: Git remote operation detailed

Git remote operation detailed

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.