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

Git force checkout

Greg Foster
Greg Foster
Graphite software engineer


Note

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


Git force checkout refers to forcefully switching branches or restoring working tree files, potentially overwriting local changes. This approach is used when you need to switch branches or recover files without committing the changes currently in your local working directory (the state of the repo present on your local machine).

  • Switching branches when local changes conflict with the branch: If uncommitted changes in your current branch would be overwritten by checking out another branch, without the --force flag, Git will block the action to prevent data loss.
  • Restoring files to their repository version when local modifications exist: If you want to discard local changes in specific files and revert them to the state stored in the repository.
  • Checking out remote branches when the local and remote histories diverge: This situation often requires resetting the local branch to match the remote one.

Below are detailed commands and scenarios where the git checkout --force command might be required, ensuring that you understand when and how to use it effectively.

To force checkout a branch, effectively discarding any local changes to tracked files, you can use:

Terminal
git checkout -f <branch-name>

This command will switch to the specified branch and reset the working directory to the last commit's state, disregarding any changes to tracked files.

If you encounter issues switching to the main branch because of uncommitted local changes, you can forcefully move to main using:

Terminal
git checkout -f main

This will move you to the main branch while discarding any local changes that would otherwise conflict with the main branch upon checkout.

To discard changes in a specific file and revert it to the last commit’s state, use:

Terminal
git checkout --force -- <file-path>

This command is helpful when you need to undo modifications and restore a file to its committed state without impacting other changed files in your working directory.

  • Data loss: Force checkout operations can lead to irreversible data loss for any local changes. Always ensure that you do not need the local changes before using force options.
  • Use sparingly: Over-reliance on git force checkout can indicate issues in workflow management. Consider refining your workflow to reduce the need for forceful actions.
  • Stashing: Before using force checkout, consider using git stash to save your local modifications temporarily. This allows you to restore the changes if needed after performing the force checkout.

For further reading on git checkout --force see the official Git 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