47

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
NSGod
  • 22,699
  • 3
  • 58
  • 66
highBandWidth
  • 16,751
  • 20
  • 84
  • 131

9 Answers9

74

Option 1

git checkout -f gh-pages

Option 2

git reset --hard     # beware: don't make that a habit
git checkout gh-pages
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 3
    Option 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. – Shailen Feb 21 '13 at 11:13
  • 2
    @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). – sehe Feb 21 '13 at 13:00
  • Option 2 did not work for me, it just repeats the error. Git is a total nightmare. – Owl Sep 07 '16 at 14:21
  • 1
    I've never seen the error "Git is a total nightmare". Does it say anything else? Is it time to ask your own question, @Owl? – sehe Sep 07 '16 at 14:22
  • git 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. – zerocog Nov 01 '16 at 20:12
12

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.

Wtower
  • 18,848
  • 11
  • 103
  • 80
6

You can ignore all uncommitted changes.

git reset --hard HEAD

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
3

If you have unstaged file, try:

git checkout -- .

Or

git checkout -- filename
maoyang
  • 1,067
  • 1
  • 11
  • 11
  • This 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. – Owl Sep 12 '16 at 07:56
3

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

Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • I 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. – Owl Sep 07 '16 at 14:23
  • @Owl I think it would be best to create a new question for your problem, including the output of `git status`. – Mark Longair Sep 10 '16 at 08:02
0

git add -A git stash

this will clear all the changes you made (Only those that are not commited)

Yoweli Kachala
  • 423
  • 6
  • 13
0

You can discard changes you made in a specific file:

git checkout somefile.txt

And then jump smoothly to the branch:

git checkout gh-pages
medik
  • 1,174
  • 12
  • 17
0

Blockquote Option 2 did not work for me, it just repeats the error.

The same for me. After git reset --hard + git checkout I was still getting the same error.

I have fix it by adding --force on second command, right after hard reset.

git reset --hard
git checkout -f <branch name>
Stefan x
  • 21
  • 6
0

To ignore but NOT remove the changes

OP asks to ignore changes - but does not ask to remove them. Sounds like OP want changes to still be around. So this answer is for those folks in a similar situation who don't want to lose their changes.

Given the limitations encountered already, plus given you want to keep those changes, one option is to put them in another branch

Regardless of where you have either uncommitted or staged changes, you can always make a new branch with

git checkout -b new_branch

and then git add and git commit as necessary. and now you can switch to other branches at will, knowing these changes have been saved.

Like the OP I don't like to use stash and pop (and cherry pick) but isolating these changes by putting them into a new branch I can then merge and rebase to and from works well for me. Using branches for the solution is easy for me

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497