0

In a local folder with several files, I have a git repository for which branch x only includes a few of those files, and the master one includes them all.

When I try to switch from x to master, I get:

$ git checkout master
error: The following untracked working tree files would be overwritten by checkout:
[...some files...]
Aborting

EDIT: None of the listed files belong to branch x, they all belong to the master one. I'm pretty confident they haven't been modified and in any case I am ok with going back to whatever version of them is saved in the master branch.

What should I do?

Ricky Robinson
  • 21,798
  • 42
  • 129
  • 185

4 Answers4

3

You have some files (the ones listed) that have been edited, but by checking out a other branch, you'll overwrite (and lose) these edits.

You can commit these changes, or stash them.

See : http://git-scm.com/book/en/v1/Git-Tools-Stashing

  • Ok. What is strange is that these files belong to the master branch and I didn't modify them in the other branch. So, I might as well go back to the "previous" version of them, whatever that might be, without any problem. How do I force it? – Ricky Robinson Mar 31 '15 at 09:08
  • 3
    OK, it was simply `git checkout -f master` – Ricky Robinson Mar 31 '15 at 09:14
  • If you not affraid of losing it, you can " git checkout -f master " But that's strange, did this help you ? http://stackoverflow.com/questions/4858047/git-error-the-following-untracked-working-tree-files-would-be-overwritten-by-ch – Bastien Saro Chassetuillier Mar 31 '15 at 09:15
  • Yeah, that did the trick. Perhaps when I changed the name of a couple of files (not among the ones listed) I messed up with something, I don't know. – Ricky Robinson Mar 31 '15 at 09:19
  • Yeah it's easy to commit more file than expected, particularly with IDE settings files (Jet brains IDE are painful for that) :) – Bastien Saro Chassetuillier Mar 31 '15 at 09:23
2

Same problem occured my project. I cleared or commited all changed files but I still couldn't change it.

After i noticed Abort button beside Commit All button. Abort button resolved my problem.

Mehmet Topçu
  • 1
  • 1
  • 16
  • 31
1

The problem is that you have files that have not been added to the working tree (Eg: new files created after the last commit). Git is preventing you from losing those files when you want to switch branches.

In order to be able to change the branch, you can either add those files to the working tree (git add file1.out or for all: git add --all) or you can remove them (git rm file1.out ...). Then you can either commit or (if not ready) you can stash them (git stash) and when you want them back (git stash pop)

More info here and here.

Community
  • 1
  • 1
Razvan
  • 3,017
  • 1
  • 26
  • 35
0

git checkout -f branch_you_want_to_go

CAREFUL. This will revert ALL changes done on the branch. But if you are forced to merge and doesnt let you stash is an easy way when not much is done.

Do this if you want to delete your branch afterwards (probably with git branch -D branch_to_delete) or you want to start working on this brand from 0.

Mbotet
  • 170
  • 2
  • 17
  • i got this error.. :( `error: pathspec 'mybranch' did not match any file(s) known to git.` – Budi Mulyo Jan 24 '19 at 05:49
  • I use to see all branches, then double click on the branch i want to use, then right click to copy at the end of your command (in this case after the "-f " ) and at least you make sure no syntax is there. Comment me the results :) – Mbotet Jan 24 '19 at 07:17
  • sry, i think my syntax error, my console got some error, then i can't login to my console after that.. – Budi Mulyo Jan 24 '19 at 07:20
  • 1
    A syntax error was in my mind when i just commented you. The command i gave you will only affect your branches. It won't affect the console itself. If it helped you somehow remember to upvote – Mbotet Jan 24 '19 at 07:24