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 the
git 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 the
git push -fCommand to push code to the server. Check man git push. The description of – f option is as follows:
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>
git 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 same
git push origin :<branchName>It’s the same. Check man git push for the — delete option. The description is as follows:
All listed refs are deleted from the remote repository. This is the same as prefixing all refs with a colon.
--deleteOption is equivalent to pushing the local empty branch to the remote branch.