Data report"State of code review 2024" is now liveRead the full report

How to Git force push

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git force push command allows users to overwrite the commit history on a remote repository.

This command should be used with caution, as it can permanently erase commits, potentially leading to loss of work for you or your collaborators.

This guide provides an in-depth look at how to use git push --force, including detailed examples.

The git force push command is typically used in scenarios where you need to update the commit history on a remote repository to match your local repository, even if it means discarding changes that are on the remote but not in your local repository.

For example, force pushing is useful if you wish to squash multiple commits that were already pushed into one larger commit, or amend existing commits with new changes like fixing a typo.

The command syntax is:

Terminal
git push --force <remote-name> <branch-name>

Or, for short:

Terminal
git push -f <remote-name> <branch-name>

Where <remote-name> is usually origin (the default name Git gives to the remote you cloned from), and <branch-name> is the name of the branch you want to force push.

Suppose you've rewritten the commit history of your local feature-branch using git rebase and want to update the remote branch. You can do this by running:

Terminal
git push -f origin feature-branch

You should then see something like:

Terminal
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
To https://github.com/yourusername/yourproject.git
+ 4a2b3cd...7c8d9ef feature-branch -> feature-branch (forced update)

This output indicates that the push has succeeded, forcibly updating the remote feature-branch with your local changes.

If you want to ensure you don't overwrite others' work use the --force-with-lease flag:

Terminal
git push --force-with-lease origin feature-branch

If someone else has updated the branch since you last pulled, the push will be rejected, and you'll see:

Terminal
! [rejected] feature-branch -> feature-branch (stale info)
error: failed to push some refs to 'https://github.com/yourusername/yourproject.git'
hint: Updates were rejected because the remote contains work that you do not have locally. This is usually caused by another repository pushing to the same ref. You may want to first integrate the remote changes
(e.g., 'git pull ...') before pushing again.

Following the instructions, run git pull to integrate the most recent remote changes then try your force push again.

To force push changes from one branch to another (e.g., from feature-branch to main), you'd check out to the branch you want to push into and then force push the other branch's commits:

Terminal
git checkout main
git push -f origin feature-branch:main

This command overwrites the main branch's history with that of feature-branch.

  • Force Push not working: If the force push doesn't seem to work, ensure you have the necessary permissions to the remote repository and that the remote branch hasn't been protected against force pushes. It is very common for repositories to forbid force pushing to the main branch.

When using the git force push command you should always proceed with extreme caution due to its potential to rewrite history. Always communicate with your team when performing force pushes on shared branches to prevent data loss. Where possible, prefer --force-with-lease to ensure you're not overwriting others' work.

For further reading on force pushing in Git see the official documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2