Git

How to Properly Force a Git Push

February 28, 2023

This guide is part of the "Snippets" series. This series is focused on providing simple and accessible tutorials on various topics relating to development!


To force a Git push, you can use the --force or --force-with-lease option with the git push command.

Here are the steps to force push:

  1. First, make sure that you have committed all the changes that you want to push to the remote repository.
  2. Use the git push command followed by the remote repository name and branch name.
  3. If you want to overwrite the remote branch with your local branch, use the --force or -f option. This will overwrite the remote branch with your local branch, regardless of any changes that may have been made on the remote branch since you last pulled it.
  4. If you want to force push only if there have been no changes to the remote branch since you last pulled it, use the --force-with-lease option. This option will check if the remote branch has been updated since you last pulled it and only force push if there have been no changes.
  5. Finally, enter your Git credentials when prompted.

Here is an example of a force push using the --force option:

git push --force origin master

And here is an example of a force push using the --force-with-lease option:

git push --force-with-lease origin master

It is important to use caution when force pushing, as it can potentially overwrite important changes made by other contributors. Always make sure that you have a backup of your changes and communicate with your team before force pushing.

External resources:


You might also like

The latest from the blog