1

I have a pretty basic work environment but does not manage to work with Git as desired.

Here is how we can describe it : two branches A and B with two files each in their working trees, the first file is toMerge and the second is toKeepSpecific.

I have the same .gitignore in the two branches saying to ignore the toKeepSpecific file. The toKeepSpecific file has never been pushed so in the remote repository, the two branches only contain toMerge.

The problem I have is that when I'm on branch B and try to checkout branch A, I have the following message : "The following untracked working tree file would be overwritten by checkout". I thought the .gitignore would take care of that but apparently not...

What's the most appropriate git environment for this kind of needs ?

Thanks !

2
  • Which untracked file is it complaining about? Also, .gitignore is not supposed to manage files that git is tracking. Commented May 19, 2011 at 10:59
  • Well it's the toKeepSpecific file that raises the complaint... Commented May 19, 2011 at 11:01

1 Answer 1

2

git ignore tells git not to track contents of files by specific names.

Edit This only prevents git from showing files as untracked, or automatically tracking them after git add. It does not apply to files that are currently tracked. You need to remove them manually from the branch where you wanted them ignored in the first place.

IOW: git ignore does not impact existing revisions. Tracked files continue to be tracked, until you remove them


Regardless of that, it is never ok to overwrite local files without warning, even when they are not (currently) tracked. This is precisely to prevent loss of data on switching branches, of course.

Many git commands accept the --force- option to just go ahead anyway, so if you know what you're doing, by all means, use that.

If you are using a git workflow that doesn't allow you to use --force options, then I suggest you employ the git clean -x command to clean up untracked (i.e. the ignored) files before doing the branch switch

Sign up to request clarification or add additional context in comments.

5 Comments

I don't want to use --force because I don't want the file to be overwritten, I want to keep the current local version...
checkout does a checkout. You cannot checkout a version while keeping some files unaltered. You'd first have to make sure that the blocking files are no longer in the revision you are trying to checkout. You can try to do the reverse: git checkout revision -- notignored1/ notignored2/ but this changes semantics: this will not update HEAD to the checkout revision, but rather overwrite the named local files while staying at the same HEAD as before.
Added clarification to the answer
Thanks ! But the thing is : the blocking files ARE NOT in the revision I'm trying to checkout. I basically just want to checkout all the files except those that not only are ignored, but are not in any branch (files only locally present in working tree). But I see the idea of doing the reverse...I just hope there is a more natural solution...
"The following untracked working tree file would be overwritten by checkout" contradicts your assertion that these files are not in the revision you are trying to check out. I'd personally double check that (e.g. git ls-tree -r "branchA:path/to/ignored.file")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.