Why does git checkout only work for some branches?

Here is the result of two checks: why does the second not work? 'git status' indicates that some files have been changed, but I'm sure I have not touched these 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) 
+4
source share
3 answers

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

For example, a branch may contain the file CHANGES.txt. Since git does not want to overwrite the file that you have in your workspace, if you give this error, to allow the backup of files that you have locally. You can:

  • Move these files somewhere safe
  • If you are sure that you do not need these files, you can perform the -f check to switch to the branch (this will overwrite any conflicting files)

Stashing does not work for files that are not tracked in the current branch. You can use git diff to determine which files are at 0.20, but not at 0.21. For instance:

 git diff --name-only branch-0.20 
+5
source

This can happen if there is a filter in place, automatically changing the contents of these files during scanning. How in:

 core.autocrlf=true 

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

For example, if the eol style was changed automatically, you would modify the files in the working tree.
And that would be enough to prevent another check with shared modified files.

You can save the changes as Kit suggests , but I would recommend first understanding why these changes occur in the first place.

+3
source
 git stash git checkout branch-0.20 git stash apply 

try higher

0
source

All Articles