🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-force-checkout-in-git
How to Force Checkout in Git? - GeeksforGeeks
May 29, 2024 - This means any uncommitted changes in the current branch will be discarded, and the working directory will be updated to match the feature-new branch. ... During a merge conflict, you may want to choose between conflicting changes.
Discussions

Why does doing 'force checkout' to some previous commit destroy the file that isn't commited?
Instead we do git checkout -f 398d and all the changes get destroyed. Why it makes sense? Because that's exactly what is meant by "force", and what the -f flag does. Wouldn't it make more sense if instead we keep the uncommited text in case I switch to master branch again? If that's what you want, then you should just do git checkout 398d without the -f. It sounds like you just want -f to not exist... Why? What would the point of that be? Just don't use it if you don't want it. More on reddit.com
🌐 r/git
5
4
March 31, 2022
github - git force checkout of duplicate branch? - Stack Overflow
If I want to create a new branch, I would do: git checkout -b new-branch However, sometimes this branch already exists, eg: fatal: A branch named 'new-branch' already exists. is there an easier way More on stackoverflow.com
🌐 stackoverflow.com
How to keep changes if I wanna checkout to another branch but I don't wanna commit them yet?
When you get back to the branch where you did git stash do git stash pop to get your changes back. More on reddit.com
🌐 r/git
14
5
May 30, 2022
Pulling a branch locally without merging with my branch.

git fetch origin branch-b is probably what you want. It fetches branch b into a remote branch locally in origin/branch-b which you can do a git merge --no-commit origin/branch-b to merge in the change without committing the merge.

To repair your branch I would suggest branching off from the commit before you did the pull. You can do this by doing git checkout new-branch-name commit-hash this way you don't need to muck around with rewriting history or reverting merges.

More on reddit.com
🌐 r/git
2
1
May 22, 2020
🌐
Git
git-scm.com › docs › git-checkout
Git - git-checkout Documentation
When you run git checkout <something>, ... Git will treat <something> as a branch or commit, but you can use the double dash -- to force Git to treat the parameter as a list of files and/or directories, like this:...
🌐
Graphite
graphite.com › guides › git-force-checkout
Git force checkout - Graphite
Git force checkout refers to forcefully switching branches or restoring working tree files, potentially overwriting local changes.
🌐
Alpha Efficiency.™
alphaefficiency.com › git-force-checkout
How to Force Git Checkout | Alpha Efficiency.™
Using force checkout guarantees that the working directory corresponds to the target entity without any remnants of old work that could cause issues or errors in the future. If the version control system doesn’t allow you to check out a branch for whatever reason, you can still indicate Git to force checkout and switch branches.
Published   April 28, 2022
🌐
freeCodeCamp
freecodecamp.org › news › git-checkout-explained
Git Checkout Explained: How to Checkout, Change, or Switch a Branch in Git
December 31, 2019 - This is equivalent to running git branch with -f. You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs ...
🌐
Reddit
reddit.com › r/git › why does doing 'force checkout' to some previous commit destroy the file that isn't commited?
r/git on Reddit: Why does doing 'force checkout' to some previous commit destroy the file that isn't commited?
March 31, 2022 -

Let's say we have a text file with some text: boo. We commit that change and say it has a hash 398d. Now we make changes to that text and add 'foo' but we don't commit it yet. Instead we do git checkout -f 398d and all the changes get destroyed. Why it makes sense? Wouldn't it make more sense if instead we keep the uncommited text in case I switch to master branch again?

Top answer
1 of 4
19
Instead we do git checkout -f 398d and all the changes get destroyed. Why it makes sense? Because that's exactly what is meant by "force", and what the -f flag does. Wouldn't it make more sense if instead we keep the uncommited text in case I switch to master branch again? If that's what you want, then you should just do git checkout 398d without the -f. It sounds like you just want -f to not exist... Why? What would the point of that be? Just don't use it if you don't want it.
2 of 4
7
Force checkout is meant to forcefully switch state, which includes clobbering/discarding any changes. It's sort of like when you force push and it clobbers changes to the remote. The tool is working as designed in the scenario you describe. If you're wanting to keep uncommitted changes around without necessarily committing, consider git stash. This is the canonical way to temporarily save uncommitted changes for later, e.g. "I was on my feature branch, made a bugfix, then realized that I needed that bugfix in main/master instead". Unless I'm trying to diagnose a particularly tricky regression (using git bisect or the like) or I'm wanting to. compare behaviors between some state in the past and now, I, as a rule, will not check out previous commits. I certainly can, but it's harder to keep myself out of trouble, especially if I'm collaborating with others. I had a colleague once who was in the habit of checking out past commits, then making changes, then adding the changes to those commits. It was absolutely maddening to diagnose the wacky-ass errors he introduced into the codebase this way. My preference is always to treat past commits as immutable and sacred. So they were written, so they shall be, for ever and ever. If I need to make changes, I do so from the latest head of my current un-merged branch.
Find elsewhere
🌐
Medium
medium.com › @ucheokorojefferson › understanding-git-commands-such-as-git-checkout-git-checkout-force-git-stash-git-commit-and-git-e446c8ab3a61
Understanding Git commands such as git checkout, git checkout force, git stash, git commit, and git checkout -b. | by Jefferson Uche-Okoro | Medium
February 4, 2024 - — Example: `git checkout -f branch-name` would forcibly switch to the specified branch, losing any unsaved changes. — Differences: Unlike regular `git checkout`, `git checkout force` does not prompt for confirmation before discarding changes.
🌐
Brandon Pugh's Blog
brandonpugh.com › til: today i learned... › git branch --force
Git branch --force | Brandon Pugh's Blog
August 16, 2023 - Today I learned about the --force parameter of git branch which will take an existing branch and point it to a different commit. This is another handy alternative to git reset --hard for some common scenarios. For example, if I forgot to create a new feature branch and accidentally made some commits onto main, I can run the following: git checkout -b new-branch # create the new branch and switch to it git branch --force main origin/main # fix main back to what it should be vs what I would do before:
🌐
KodeKloud
kodekloud.com › blog › git-switch-vs-checkout
Git Switch vs. Checkout: What’s the Difference?
December 12, 2024 - If you want to force-switch to another branch called feature3 and discard any uncommitted changes in your working directory and staging area, you can run: ... It is more intuitive and clearer than git checkout since it only deals with switching branches.
🌐
LinkedIn
linkedin.com › pulse › git-checkout-f-reset-hard-commend-musaddek-ali
The "git checkout -f" and "git reset --hard" commend for Git.
May 4, 2023 - The git checkout -f command in Git is used to force switch to a different branch or commit and discard any local changes or uncommitted modifications made to the current branch. The -f option stands for "force" and is used to override Git's ...
🌐
iO Flood
ioflood.com › blog › git-force-checkout-how-to-overwrite-local-modifications
Git Force Checkout | How To Overwrite Local Modifications
November 26, 2023 - In complex code repositories with multiple branches and frequent commits, ‘force checkout’ can be a formidable tool. It enables swift switching between branches, discarding unwanted changes, and maintaining a clean working directory. However, remember to exercise caution with this command. It’s a powerful tool, but with great power comes great responsibility. As previously discussed, the ‘-f’ or ‘–force’ option can be paired with ‘git checkout’ to force Git to switch branches, discarding any uncommitted changes:
🌐
Linux Hint
linuxhint.com › force-git-checkout
How to Force Git Checkout? – Linux Hint
Furthermore, modify the file and insert an updated file into the repository with the help of the git add” command. Then, check the current status of the repository and use the “git checkout” command with the “-f” or “-force” option for switching between branches.
🌐
Refine
refine.dev › home › blog › alternatives › git switch and git checkout – how to switch branches in git
git switch and git checkout – How to switch branches in git | Refine
July 4, 2025 - Git will not allow switching branch until you do one of the following: • Use stash to locally stash your changes temporarily without commit • Force checkout, which will discard your local changes • Commit your changes, and then update ...
🌐
Medium
medium.com › @ChanakaDev › understanding-the-differences-stash-checkout-migrate-changes-and-force-checkout-fa251e032346
Understanding the Differences: Stash & Checkout, Migrate Changes, and Force Checkout | by Chanaka | Medium
July 9, 2023 - Unlike the traditional checkout command that moves the HEAD pointer to the specified commit, fore checkout discards the current branch reference entirely, providing a detached HEAD state.
🌐
Ralph's Open Source Blog
ralph.blog.imixs.com › 2014 › 10 › 20 › force-git-hard-checkout-branch
Force Git to hard checkout a branch - Ralph's Open Source Blog
I run into a situation with a git repository that I was no longer able to switch between branches. Git complains about uncommitted changes. But I was sure that my local repository should not have any changes. For force a hard checkout and pull use the following git commands. NOTE:…
🌐
Git Tower
git-tower.com › learn › git faq › git checkout & switch: how to change branches
Git Checkout & Switch: How to Change Branches | Learn Version Control with Git
1 week ago - Both work — git switch is recommended for new users because it has a single, clear purpose, while git checkout is still widely used and supports additional operations like restoring files.
🌐
Linux Kernel
kernel.org › pub › software › scm › git › docs › git-checkout.html
git-checkout(1) Manual Page
May 17, 2026 - When you run git checkout <something>, ... files. If there’s any ambiguity, Git will treat <something> as a branch or commit, but you can use the double dash -- to force ......
Top answer
1 of 1
6

What you want here is git checkout -B name. If name does not name an existing branch, this creates the new branch, pointing to the current commit, as if by regular git checkout -b.

If name does name an existing branch, what happens instead is that Git forcibly re-points the branch name to the current commit. This is a lot like a git reset --soft. A branch name is really just a human-readable name for some Git hash ID, and a soft reset changes the hash ID attached to the branch name, without touching the index or work-tree. In the same way, git checkout -B will change the ID attached to this name, without touching the index or work-tree.

(The main answer ends here, the rest is just an aside on all the various success vs failure modes.)

There is a big difference between -B and --force

There's an important couple of distinctions to make here.

The general form of this particular kind of checkout is:

git checkout [-b | -B] name [target-commit-specifier]

such as, e.g., git checkout -b newbr a234567.

The difference between -b and -B at this point is obvious: if the name part names a branch that already exists, git checkout -b fails immediately. Git doesn't have to try the checkout: the name exists; error. If the name doesn't exist, or you used -B, though, there is more to try.

If you leave out the target-specifier, as we are doing above and in the original question, a whole class of problems just goes away. This form, without a target commit, means: Leave the current commit in place. Don't touch the index or work-tree at all. Doing nothing is easy, and never fails, so this part never fails.

When you use the form git checkout -B name target-commit-specifier, though, the command tries to git checkout the target commit a234567. This step requires updating the index and/or work-tree, and this may fail with, e.g.:

error: The following ... files would be overwritten ...

In this case, the git checkout -b or git checkout -B is aborted and no branch is created or re-set. (See also Checkout another branch when there are uncommitted changes on the current branch.)

The --force option tells Git that if this checkout step is about to fail, Git should go ahead and do the checkout anyway, overwriting or removing files that are not safely saved away in the repository. This can make all kinds of changes to the index and/or work-tree, and any file-data that are not permanently, safely stored away in a commit could be lost here. So --force remains dangerous (in that it can lose file data), but is not related to -b vs -B: it only affects this step, of moving from your current commit, whatever it is, to this other target commit.

If Git gets this far—the target commit is now installed as the current commit, with the index and work-tree changed; or the target commit is the current commit, so the index and work-tree are left alone—then at this point the whole thing is destined to succeed. We've already checked whether the branch name exists for -b. All Git has to do now is write the hash ID of the current commit into the branch name (so that name points to a234567, for instance) and write ref: refs/heads/name into .git/HEAD, so that you're now on branch name.

🌐
RTEE Tech
blog.rteetech.com › home › development › git force checkout: safely switch branches & discard changes
Git Force Checkout: Safely Switch Branches & Discard Changes
June 5, 2025 - Using the `-f` flag resolves this issue by disregarding local changes. When you need to discard modifications in a specific file and want it to match the committed version from the repository, `git force checkout` does the trick.