Home >Development Tools >git >What is the difference between push and pull in git
The difference between push and pull in git: 1. The "git push" command sends the latest information in the local library to the remote library, and is used to push updates to the local branch to the remote host; 2. " The "git pull" command is to get the latest version from the remote to the local library, and is used to get and integrate from another repository or local branch.
The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.
What is the difference between push and pull in git
git push and git pull are a pair of git commands that push/pull branches.
git push uses the local corresponding branch to update the corresponding remote branch
$ git push <远程主机名> <本地分支名>:<远程分支名>
Note: The local branch in the command refers to the branch that will be pushed to the remote end. The remote branch refers to the target branch of the push, that is, the local branch is merged into the remote branch.
If the remote branch name is omitted, it means that the local branch will be pushed to the remote branch that has a "tracking relationship" with it (usually both have the same name). If the remote branch does not exist, it will be created.
$ git push origin master
The above command indicates that the local master branch will be pushed to the master branch of the origin host. If the latter does not exist, it will be created.
origin is a remote factory address.
If the local branch name is omitted, it means deleting the specified remote branch, because this is equivalent to pushing an empty local branch to the remote branch. This command deletes the remote master branch.
$ git push origin :master # 等同于 $ git push origin --delete master
The above command means to delete the master branch of the origin host.
If there is a tracking relationship between the current branch and the remote branch (that is, the branch name is the same), both the local branch and the remote branch can be omitted.
$ git push origin
The above command indicates that the current branch will be pushed to the corresponding branch of the origin host.
If the current branch has only one tracking 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 adding any parameters later.
$ git push -u origin master
The above command pushes the local master branch to the origin host, and specifies origin as the default host. You can then use git push without adding any parameters.
Git push without any parameters will only push the current branch by default. This is called simple mode. In addition, there is a matching method that will push all local branches that have corresponding remote branches. Before Git version 2.0, the matching method was used by default, but now it is changed to the simple method by default. If you want to modify this setting, you can use the git config command.
$ git config --global push.default matching # 或者 $ git config --global push.default simple
Another situation is to push all local branches to the remote host regardless of whether there is a corresponding remote branch. In this case, you need to use the –all option.
$ git push --all origin
The above command indicates that all local branches will be pushed to the origin host.
If the version of the remote host is newer than the local version, Git will report an error when pushing, requiring you to do git pull locally to merge the differences before pushing to the remote host. At this time, if you must push, you can use the --force option.
$ git push --force origin
The above command uses the –force option, which results in a “non-fast-forward merge” on the remote host. Unless you are absolutely sure you want to do this, you should avoid using the --force option.
Finally, git push will not push tags unless the –tags option is used.
$ git push origin --tags
git pull obtains and merges other factory libraries or other local branches.
The purpose of git pull and git push operations is the same, but the goal of the operation is opposite. The command format is as follows:
git pull <远程主机> <远程分支>:<本地分支>
For example:
git pull origin master:my_test
The above command pulls the master branch of the origin factory library and merges it into the local my_test branch.
If you omit the local branch, it will automatically be merged into the current branch. As follows:
git pull origin master
Note: If you want to participate in some excellent projects on github, a general example is provided below:
First, you need a github account and fork the one you like Repository of interest.
The following description process will involve two remote master branches. For a good distinction, we call the forked master branch remote A repository, and the branch of this fork is called remote B repository
$git clone <远程Arepository> #克隆你fork出来的分支 $git remote add <远程Brepository标签> git@github.com:XXXX/ceph.git #添加远程Brepository标签 $git pull <远程B厂库标签> master:master #从远程Brepository的master分支拉取最新objects合并到本地master分支 $git checkout YYYY #切换到要修改的分支上 $git branch develop; git checkout develop #在当前分支的基础上创建一个开发分支,并切换到该分支上,你将在该分支上coding coding...... #在工作区coding $git add .#将修改保存到索引区 $git commit -a #将修改提交到本地分区 $git push origin my_test:my_test #将本地分支my_test提交到远程A repository的my_test分支上
Then merge the my_test branch into the remote B repository branch you need to change on the github web interface. Wait for the administrator to review. If there are any problems, continue to make changes in the develop branch, and commit –amend to make changes on the previous commit. Know by meger.
Recommended study: "Git Tutorial"
The above is the detailed content of What is the difference between push and pull in git. For more information, please follow other related articles on the PHP Chinese website!