Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't switch branches - "Error: Untracked working tree files..." should I do git add?

Tags:

git

I have this on my .gitignore file:

/nbproject/
/app/runtime/
/app/runtime/application.log*
/app/runtime/error.log*
/app/config/localdev.php*
.DS_Store

1) I have checkout to master branch and then, checkout again, back to dev branch.

2) Once I got back to dev branch, I lost all /nbproject/ files and all /app/config/localdev.php as well !!! And perhaps the others, but since they are autogenerated by php framework uppon runtime, I can't really tell!

3) I then switch back to the master branch and I got those files there.

4) I've copy those files somewhere else, and I've switched to dev branch again.

5) I've place those missing files back inside /nbproject/ folder.

Now, if I try to switch to the Master branch, I'm getting this:

Error: The following untracked working tree files would be overwritten by checkout:     
    nbproject/private/config.properties      
    nbproject/private/private.properties     
    nbproject/private/private.xml    
    nbproject/project.properties     
    nbproject/project.xml 

    Please move or remove them before you can switch branches.  

    Aborting

I'm aware that I should perhaps do a git add . to move those files on the repository, what I don't understand is:

Why is git throwing this message IF I had nbproject/ ignored well before those git checkout commands being issued?

Question:

The point is to ignore those files. Both on master and dev. My question is: how can I fix this in order to:

a) First: get those files (the ones on gitignore) back to dev ?

b) Second: make steps to avoid this conflict again.

like image 880
MEM Avatar asked Aug 31 '12 16:08

MEM


People also ask

Can I switch branch with untracked files?

Normally when you switch branches the untracked files are brought along with you without any notice. That's why they're called "untracked". However, you say that the untracked files would be overwritten. This implies that you have created an untracked file in the new branch which already exists in the master branch.

What happens to the files when you switch branches?

Files belonging to the old branch but missing from the new one are simply deleted (because they already exist in the object store, so Git can just re-extract them if you switch back). When some files are identical between branches, git checkout doesn't touch them at all (no deletion/re-extraction).


2 Answers

You can track them in the other branch. Or, you can add --force to the checkout command to zap the files with what ever master has. Or, you can git clean -xdf before checking out master to zap the files from the working dir.

like image 93
Adam Dymitruk Avatar answered Sep 29 '22 08:09

Adam Dymitruk


Steps taken to solve the problem. Credits go to: jszakmeister

1) I have cloned the project to somewhere else.

2) I've checkout master on that clone.

3) Placed the gitignore into the master as it should have been there from the beginning: git show dev:.gitignore > .gitignore

4) I've removed the files from the tree by doing git rm "all files that where on gitignore one by one"

5) I've added the .gitignore file to the repo.

6) commit it

7) switch back to the original repo and did:

8) git fetch /path/to/fix-project master:master

9) git checkout master, and I got: Switched to branch 'master' Your branch is ahead of 'hub/master' by 1 commit. this is my remote master branch.

10) Pushed to remote master.

11) checkout dev again

like image 37
MEM Avatar answered Sep 29 '22 09:09

MEM