git remote operation command related discrimination (remote, push, fetch, pull)

git remote

For ease of administration, Git remote repository requires that each must specify the name of a warehouse. For ease of administration, Git remote repository requires that each must specify the name of a warehouse.

  • git remote name] [View Loft
git remote

After we just clone the project will see the default origin of a remote repository

  • git remote -v / - version [information] to view warehouse

Url shows detailed alias name and the corresponding address.

Such as:

origin  git@github.com:zhaoJoeyuan/TestTwo.git (fetch)
origin  git@github.com:zhaoJoeyuan/TestTwo.git (push)
  • git remote add [add a remote repository]

command:

git remote add name url 

Such as:

git remote add joey git@github.com:zhaoJoeyuan/Test.git

After you add, view with git remote -v

joey    git@github.com:zhaoJoeyuan/Test.git (fetch)
joey    git@github.com:zhaoJoeyuan/Test.git (push)
  • git remote remove [deleted] added remote database
命令:git remote remove name 
  • gi remote show [view details of the designated warehouse]

command:

git remote show 仓库名

git push

git push command is used to update the local branch, pushed to the remote repository. Its format is similar to git pull command.

git branch push / pull sequence is written <Origin>: <dest> push and pull it certainly contrary, it is a source of native push, pull the remote source.

1. Complete wording

command:

git push <远程仓库名> <本地分支名>:<远程分支名> 
 

Note that the order of the branch push wording is <Origin>: <dest>, it is git pull <remote branch>: <local branch>, and git push the <local branch>: <remote branch>.

Such as:

git push origin master2master2

The local push your master branch to the remote master branch library, if the remote branch does not exist will be created.

2. omit the name of the local branch

If you omit the local branch name, then delete the specified remote branch, because this is equivalent to pushing an empty local branches to remote branches. Note that the first branch (origin / master) is not deleted,

Command: delete remote branch

git push origin :远程分支名   # origin后面有一个空格

或 git push origin --delete :远程分支名

或 git push origin :远程分支名 --delete 

3. omit remote branch name

If the remote branch name is omitted, then the local branch exists with push Tracking (Tracking relationship) remote branches, two branches of the same name generally. If the remote branch does not exist, it will automatically create a branch.

Such as:

git push origin master

The above represents a command, the master branch pushed to the master branch origin warehouse. If the latter does not exist, it is new.

 

Above the current branch and remote branch did not exist to track the relationship is no one who is the upstream / downstream.

4. entirely omitted

If the current track has only one branch branch, then the repository name can be omitted.

git push

If the current track relationship exists with multiple branch warehouse, you can use the -u option to specify a default warehouse, so that later you can use without any parameters git push.

git push -u origin master

The above command will be pushed to the master branch originck, specifying the origin as the default warehouse, the back can be used without any git push the parameters.

git push without any parameters, the default only push the current branch, which is called simple way. In addition, there is a matching way, will have to push all of the local branch of the corresponding remote branch. Prior to Git version 2.0, defaults to matching method, is now default to simple. If you want to change this setting, git config command can be used.

$ git config --global push.default matching
# 或者
$ git config --global push.default simple 

In another case, regardless of whether or not there is a corresponding remote branch, the local branches of all pushed to the remote repository, then need to use the --all option.

$ git push --all origin

The above command, said all local branches are pushed to the origin warehouse.

If the version of the remote repository is newer than the local version, Git push will complain when required to do git pull merge differences in local and then pushed to the remote repository. At this point, if you have to push, you can use the --force option.

$ git push --force origin

The above command --force option, resulting in an updated version on the remote repository is covered. Unless you are very determined to do this, or should try to avoid using the --force option.

Finally, git push will not push the label (tag), unless --tags option.

$ git push origin --tags

git fetch updates from the remote to the local pull

 

It is to get the latest version from the remote to the local repository, not the working directory, and does not automatically merge

Understanding fetch

The key fetch understanding, it is to understand FETCH_HEAD.

FETCH_HEAD means: the latest status of a branch on the server '.

A fetch operation performed each item 'FETCH_HEAD there will be a list of which is stored in .Git / FETCH_HEAD file, where each row corresponds to a branch of the remote server. FETCH_HEAD current branch points, is the first line of the file corresponding to that branch.

Generally speaking, there are two cases:

  • If not explicitly specified remote branch, the remote branch as a default master FETCH_HEAD.
  • If you specify a remote branch, remote branch as this will FETCH_HEAD.

git fetch <remote repository name> to a remote repository update, retrieve all the local repository

command:

git fetch <远程仓库名>

All branches of the remote repository to get dragged local

 

Without the branch name after fetch, it is all warehouse branch

 

Note: use the remote branch made in the form of "remote repository name / branch name" read on a local warehouse. Such as origin master warehouse, will use origin / master reading.

Retrieve a particular branch of the update, you can specify the name of the branch

command:

git fetch <远程仓库名> <分支名>

git fetch <远程仓库名> <远程分支名>:<本地分支名> 

Common usage includes four git fetch:

  • 1.git fetch

command:

git fetch

This step is actually carried out two key actions:

  1. Create and update all remote branch of the local branch;
  2. Set the current FETCH_HEAD branch of the remote server's master branch (the first case said above) It should be noted that, and push different, fetch remote branch branches will automatically get "new entrants" in
  • 2.git fetch origin

command:

git fetch origin

Function as above, but manually specify the remote

  • 3.git fetch origin <remote branch name>

command:

git fetch origin <远程分支名>
 

This command can be used to test remote repository of remote branch branch1 exists, if there is, returns 0 if there is no return to 128, an exception is thrown.

  1. git fetch origin <remote branch name>: <local branch name> command:
git fetch origin <本地分支名>:<远程分支名>

Use remote <remote branch name> is created in the local branch <local branch name> (but will not switch to that branch), if there is no local <local branch name> branch, it will automatically create a new <local branch name> branch If there is a local <remote branch name> branch, and is fast forwardthen automatically merge two branches, otherwise, it will prevent the above operations.

<Local branch name> and <remote branch name> of the same name, this is equivalent togit fetch origin :<远程分支名>

git pull

The latest version is available from the remote to the local, and low levels of auto-merge; but most still require the programmer to determine merge

Full command

git pull <远程仓库名> <远程分支名>:<本地分支名> 
 

Note that the remote branch name prefix is ​​without origin / a (prefix alias), there is a conflict, when conflict can be modified, the modification is completed add, commit

Non-complete, current <local branch name> is omitted

If the remote branches are combined with the current branch, the portion of the colon may be omitted.

git pull origin <远程分支名>

It represents the above command, retrieving origin / <remote branch name> branch, and then merged with the current branch. In essence, this is equivalent to do first git fetch, do git merge.

git pull & git fetch

git pull origin <远程分支名>

等价于

git fetch origin git fetch origin/<远程分支名> 

Two branch name omitted

If the current relationship between the presence of trace branch and remote branch, git pull will be omitted remote branch name.

git pull origin

It represents the above command, the current local branch origin corresponding to the automatic warehouse "Tracking branch" (remote-tracking branch) merge.

If the current track has only one branch branch, even the remote repository name can be omitted.

 git pull

After obtaining remote branch handled in two ways:

  • 1. Create a new branch-based remote directly at the local branch

command:

 git checkout -b <本地分支名> origin/<远程分支名>

The above wording is complete, other formulations as follows:

git checkout -b <本地分支名> 
#基于<本地master分支>而创建

git checkout -b <本地分支名> orgin # 基于<远程master分支>而创建 git checkout -b <本地分支名> <本地分支名2> # 基于<本地分支2>而创建 
 

Remote creation must take the orgin (remote repository name) / prefix, or are based on local branches to create, if not with the branch name, the default master is created (can be a remote master, it can also be local master, to see if there origin prefix)

  • 2.git merge command or git rebase command, merge remote branches on the local branch.

command:

 git merge origin/master
 
 git rebase origin/master
 

(Current branch) merge remote branch on the local branch

Git manually create trace relationships.

In some cases, Git automatically between the local branch and a remote branch, to establish a relationship tracking (tracking). For example, when git clone, the local branch of the same name in all branches of the default remote repository, a tracking relationship, that is, the local master branch automatic "track" origin / master branch.

git branch - -set-upstream master origin/next

The above command specifies the master branch to track origin / next branch.

If the current relationship between the presence of trace branch and remote branch, git pull will be omitted remote branch name.git pull origin

It represents the above command, the current local branch origin corresponding to the automatic warehouse "Tracking branch" (remote-tracking branch) merge.

If the current track has only one branch branch, even the remote repository name can be omitted.git pull

The above command, said the current branch is automatically merged with only one track branch.

If the merger need rebase mode, you can use --rebase option.

git pull - -rebase <远程仓库名> <远程分支名>:<本地分支名> 

If you delete a remote repository branch, by default, git pull when pulling the remote branch, it does not delete the corresponding local branch. This is to prevent, due to other people operating the remote repository, resulting in git pull unwittingly removed the local branch.

However, you can change this behavior, add -p parameter will delete remote branches that have been deleted locally.

$ git pull -p
# 等同于下面的命令
$ git fetch - -prune origin 
$ git fetch -p

Guess you like

Origin www.cnblogs.com/yinzhi/p/11599997.html