Switching a branch after aborting current changes in git
Solution 1
Option 1
git checkout -f gh-pages
Option 2
git reset --hard # beware: don't make that a habit
git checkout gh-pages
Solution 2
Just for the sake of completeness, and for those who landed here by searching: Although the OP asks specifically for a solution without stashing, it is worth mentioning that stash is indeed a very nice option:
The command saves your local modifications away and reverts the working directory to match the HEAD commit.
So you can simply
git stash
This is like resetting to HEAD. When being absolutely positively certain that indeed those uncommitted changes are worthless, simply
git stash drop
You can even have multiple stashes etc. as mentioned in the documentation link above.
I would recommend this practice since it puts one in the habit to double-think before resetting without a significant cost.
Solution 3
You can ignore all uncommitted changes.
git reset --hard HEAD
Solution 4
If you have unstaged file, try:
git checkout -- .
Or
git checkout -- filename
Solution 5
If you're really sure that you want to throw away your uncommitted changes (i.e. those that are staged as well as those in your working tree) you can do:
git reset --hard
In general, stashing is often safer
Related videos on Youtube
highBandWidth
Updated on August 29, 2020Comments
-
highBandWidth over 3 years
I cloned a git repo and then started playing around in its master branch. After a while, I want to ignore the changes I just made (without committing them), and switch to a different branch. However, it stops me from switching because there are uncommitted changes. How do I ignore them without stashing them either? This is what happens:
$ git checkout gh-pages error: Your local changes to the following files would be overwritten by checkout: somefile.txt Please, commit your changes or stash them before you can switch branches. Aborting
-
Shailen about 11 yearsOption 1 didn't work. Git was a goliath of a reluctant problem solver. I was forced to make git reset --hard .. a habit. The holy grail of git command that fixes everything.
-
sehe about 11 years@shailenTJ You should be a poet :) And rest assured, I use git reset --hard often. But the thing is, it's a powerful weapon and you should not use it without giving it proper thought, as it will lose anything that wasn't committed (or at least added to the index once, and that would be tricky to recover).
-
Owl over 7 yearsOption 2 did not work for me, it just repeats the error. Git is a total nightmare.
-
sehe over 7 yearsI've never seen the error "Git is a total nightmare". Does it say anything else? Is it time to ask your own question, @Owl?
-
Owl over 7 yearsI REALLY want to throw away my changes, it wont let me. I've tried git reset --hard, i've tried stashing and dropping the stash. Helllllp. Save me from this awful versioning software.
-
Mark Longair over 7 years@Owl I think it would be best to create a new question for your problem, including the output of
git status
. -
Owl over 7 yearsThis fixed my problem, thank you. It seems that this issue is either solved by a git checkout -- . or git reset --hard. One of those should fix it.
-
zerocog over 7 yearsgit checkout -f gh-pages worked well for me. I had some lingering CRLF files that were fixed in another branch. stash and stash drop still did not let me checkout a different branch. I noticed my CRLF files were still waiting when I returned to the branch after the -f.