Curious2learn
Curious2learn

Reputation: 33628

Prevent an untracked file from being overwritten in Git

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:

  1. I don't get this message for other untracked files, why for this specific file?

  2. 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

Answers (4)

janos
janos

Reputation: 124656

Do you want to track the file or not? (Should it be version controlled by Git?)

If NO:

  1. Stash the file: git add queries.py; git stash
  2. Checkout the other branch
  3. Delete the file from the branch, add it to .gitignore and commmit
  4. Restore the file from stash: git stash pop; git reset

If YES:

  1. Add the file to Git
  2. Checkout the other branch
  3. At some point you will probably merge the two branches, and then you will have to decide which version of the file to keep when you commit the merge.

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

Schleis
Schleis

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

sjas
sjas

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

dare
dare

Reputation: 662

your 2 Q: add your files to .gitignore to ignore them from git things.

Upvotes: 0

Related Questions