Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Git push command usage


May 25, 2021 Git



Git push is a common command in Git that pushes updates from local branches to remote hosts.


git push format:

The format of git push is similar to git pull:

$ git push <远程主机名> <本地分支名>:<远程分支名>

Attention:

Branch push order is written in the order of branch push, so git pull is the local branch of the remote branch, and git push is the local branch of the local branch. Omitting the remote branch name means pushing the local branch to a remote branch with which there is a "tracking relationship" (usually the two have the same name), and if the remote branch does not exist, it is created.


Common usage of git push:

$ git push origin master

The effect of this command is to push the local master branch to the master branch of the origin host. I f the latter does not exist, it is created. Omitting the local branch name means deleting the specified remote branch because it is equivalent to pushing an empty local branch to the remote branch.

$ git push origin :master
# 等同于
$ git push origin --delete master

The above command indicates the removal of the master branch of the origin host. If there is a trace 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 git push can be used without any parameters.

$ git push -u origin master

The above command pushes the local master branch to the origin host and specifies the origin as the default host, after which you can use git push without any parameters.

git push without any parameters, pushing only the current branch by default, which is called the simple method. I n addition, there is a matching method that pushes all local branches with corresponding remote branches. P rior to Git 2.0, the matching method was used by default, and now the simple method is the 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 scenario is to push all local branches to the remote host regardless of the existence of a corresponding remote branch, at which point the -all option is required.

$ git push --all origin

The command above indicates that all local branches are pushed to the origin host.

If the version of the remote host is updated compared to the local version, Git will report an error when pushing, requiring the git pull merge difference to be made locally before being pushed to the remote host. At this point, if you have to push, you can use the -force option.

$ git push --force origin

The above command uses the -force option, which results in a "non-direct-forward-merge" merge on the remote host. U nless you're 10 sure you want to do this, try to avoid using the force option.

Finally, git push does not push tags unless the -tags option is used.

$ git push origin --tags

Sometimes when the remote xxx branch is deleted, you can also see the local remote/origin/xxx branch with git branch -a, so you can use the git fetch-p command to help you synchronize the latest remote branch and delete the local deleted remote branch.

These are some common uses of Git push commands, and I hope they will help you. To learn more about Git commands, click on: Git Common Command Quick Checker