Examples of using git push command

Time:2021-6-14

When using git push command to push code to the server, different requirements will have different usages. Some specific examples are as follows.

Force to override git log information of server

When we use the GIT reset command to rollback the commit information displayed in the local git log, we use thegit pushIf you submit the change to the remote server, an error will be reported, and an error message similar to the following will be printed:

Tip: the update was rejected because the latest commit of your current branch is behind its corresponding remote branch.

At this time, if you want to force the commit information of the local git log to cover the GIT log of the server, you can use thegit push -fCommand to push code to the server. Check man git push. The description of – f option is as follows:

-f, –force
Usually, the command refuses to update a remote ref that is not an ancestor of the local ref used to overwrite it.
This flag disables these checks, and can cause the remote repository to lose commits; use it with care.

In other words, the – f option can forcibly cover the GIT log information of the server with the local git log information. As this will result in the loss of some submitted log information, please use it carefully to confirm the necessity of doing so.

Delete remote server branch

In git, you can use the following two commands to delete the branch on the remote server.

  • git push origin :<branchName>

staygit push origin :<branchName>In the command, the parameter before the colon ‘:’ is the name of the local branch to be pushed, which is not provided here. It is equivalent to pushing an empty local branch. The parameter after the colon is the name of the remote branch to be pushed to the server.

This command pushes an empty branch to the remote branch, which means that the remote branch is set to be empty, thus deleting the remote branch specified by branchname.

  • git push origin –delete <branchName>

git push origin --delete <branchName>Command is essentially the samegit push origin :<branchName>It’s the same. Check man git push for the — delete option. The description is as follows:

-d –delete
All listed refs are deleted from the remote repository. This is the same as prefixing all refs with a colon.

That is,--deleteOption is equivalent to pushing the local empty branch to the remote branch.