4

Here is the result of two checkouts: why the second is failing? 'git status' shows some files have been modified, but I am sure I haven't touched those files.

praveensripati@MyMini:~/Hadoop/Git/hadoop-common$ git checkout branch-0.21  
Switched to branch 'branch-0.21'  

praveensripati@MyMini:~/Hadoop/Git/hadoop-common$ git checkout branch-0.20  
error: The following untracked working tree files would be overwritten by checkout:  
    CHANGES.txt  
    LICENSE.txt  
    README.txt  
    bin/hadoop  
    bin/hadoop-daemon.sh  
    bin/hadoop-daemons.sh  
Please move or remove them before you can switch branches.  
Aborting

praveensripati@MyMini:~/Hadoop/Git/hadoop-common$ git status
# On branch trunk
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   CHANGES.txt
#   LICENSE.txt
#   README.txt
#   bin/
#   build.xml
#   conf/
#   lib/
#   site/
#   src/
nothing added to commit but untracked files present (use "git add" to track)
Zoe
  • 27,060
  • 21
  • 118
  • 148
Praveen Sripati
  • 32,799
  • 16
  • 80
  • 117
  • What does `git status` say? Does it report any changes or any untracked files? – Jan Hudec Sep 26 '11 at 08:25
  • Looks like git is prone to flaky network connections. I had this and other problems when the network connection is proper. Cloned the repository again and the problem got solved. – Praveen Sripati Sep 27 '11 at 06:54
  • It cannot possibly be network connection. Git first downloads everything, verifies the checksums and than starts checking out. It could be that something went slightly wrong during the checkout though. – Jan Hudec Sep 27 '11 at 07:36

3 Answers3

5

This is occurring because some or all of the files that are not being tracked on your current branch are being tracked by the branch you want to change to.

For example the branch may contain a CHANGES.txt. Because git does not want to overwrite the file you have in your workspace if is giving your this error to allow you to backup the files you have locally. You can either:

  1. Move these files somewhere safe
  2. If you are sure you don't need these files, you can perform a checkout -f to switch to the branch (this will overwrite any files that conflict)

Stashing does not work for files that are not tracked on the current branch. You can use git diff to work out which files are on the 0.20 but not on 0.21. For example:

git diff --name-only branch-0.20
andygavin
  • 2,784
  • 22
  • 32
  • 1
    +1 This is the right answer, I think. Just one hopefully helpful note: if you're using a very recent version of git (1.7.7) you can get stash to also store untracked files by doing `git stash --include-untracked` – Mark Longair Sep 26 '11 at 11:44
3

That can happen if there is a filter in place, automatically changing the content of those files on checkout. As in:

core.autocrlf=true

(See Why should I use core.autocrlf=true in Git?)

For instance, if the eol style is changed automatically, you would have modified files in your working tree.
And that would be enough to prevent another checkout with common modified files.

You can stash the changes, as Kit suggests, but I would recommend understanding first why those changes happen in the first place.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • good answer, want to know more that what is 'eol' stand for? if set core.autocrlf to false, it will not modify automatically, right? – TheOneTeam Sep 26 '11 at 08:15
  • @KitHo: if `core.autocrlf` is set to false, it won't change automatically the end of line style... except if you have a gitattribute set (http://stackoverflow.com/questions/1206406/dealing-with-files-that-git-refuses-to-reset/1206441#1206441). But if you don't, try setting `core.autocrlf` and see if the issue persists. – VonC Sep 26 '11 at 09:38
  • `For instance, if the eol style is changed automatically, you would have modified files in your working tree. And that would be enough to prevent another checkout with common modified files.` Could you explain me in detail? If the files are modified why I am able to checkout branch-0.21 and not branch-0.20? ------- How could I see the difference between the modified file and the file in Git (to see the eol change)? – Praveen Sripati Sep 26 '11 at 10:17
  • git stash - No local changes to save; git stash apply - No stash found; git status - shows me a list of file. Still not able to checkout branch-0.20 :) – Praveen Sripati Sep 26 '11 at 10:27
  • @PraveenSripati If the first checkout is done on a clean working tree, it will succeed, but will changes the files eol in the process, leaving a non-clean working tree. Meaning the second checkout (which needs unmodified files to be able to change them) will fail. What is the result of a `git status` after your first checkout? – VonC Sep 26 '11 at 10:46
  • @VonC: I don't think this is about line endings - the error and status show that those files are untracked rather than modified. – Mark Longair Sep 26 '11 at 11:46
  • @Mark: sure, I didn't had the git status output at the time. I just thought interesting to mention that tricky situation where a first checkout works (but changes files) and the second would not. – VonC Sep 26 '11 at 12:32
0
git stash 
git checkout branch-0.20
git stash apply

try above

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158