Reputation: 33628
Say I have two branches, master
and b2
. In the specific directory where the git repository is, there are many files not tracked by git and some that are tracked by git. I did some work in branch b2
, and now I tried to switch back to master
using git checkout master
.
I get an error saying:
The following untracked working tree files would be overwritten by checkout: queries.py
I have two questions:
I don't get this message for other untracked files, why for this specific file?
I want this file to remain the state it currently is across the different branches (that is, I do not want to overwrite or delete it). How can I do that?
I looked at other questions that mentioned this error. The answers suggested using git clean
saying it would remove the untracked file, but I don't want to remove it.
Upvotes: 2
Views: 3006
Reputation: 124656
Do you want to track the file or not? (Should it be version controlled by Git?)
If NO:
git add queries.py; git stash
.gitignore
and commmitgit stash pop; git reset
If YES:
You can choose either way, but you have to decide. It's not normal to track a file in some branches and ignore in others. Either track everywhere or ignore everywhere.
Upvotes: 5
Reputation: 43720
That file has been added in one of your branches and is not present in others. So when you are moving from the branch where it is not tracked to the one that has the file being tracked, you get this error.
When you moved from the branch that had this file being tracked, git made the file untracked when it was updating the state of the code. Files that are not tracked on the new branch are not removed they remain and become "new" un-tracked files. Then moving back, git thinks that you are trying to overwrite this file as it exists in the file system.
Start tracking the file in that branch and you won't have the problem.
You can also add the file, do git stash
, change branches and git stash drop
This makes the file seem like it is being tracked. Stashing will prevent any changes from being overridden and keep changes from being tracked. When you change branches the file will be created in the state it is on that branch. Dropping the stash is optional if the file is in a state that you want to retain don't do this step.
Upvotes: 3
Reputation: 19687
To make sure the file is kept versioned, but cannot be changed, make it write-protected.
That it is present in all branches, you have to readd it where it is missing.
Adding it to the .gitignore
makes it invisible to git, it will not be tracked further. But all working data that is needed should be present in the repository. Any other approach is bad style which will get you sooner or later.
Upvotes: 0
Reputation: 662
your 2 Q: add your files to .gitignore to ignore them from git things.
Upvotes: 0