Ace
Ace

Reputation: 1601

Git Ignore not working

Here is my scenario. I was on branch A, where I made some changes to certain file on my local working copy.

/launcher/file.esb

I basically want the following:

  1. Ignore the changes made to this file completely. I accomplished this using assume-unchanged
  2. Untrack this file from version control, which I tried to accomplish using .gitignore.

  3. Preserve the changes to this file on my local working folder as I checkout different branches. A->Develop->B->C etc...

It is #3 that I am not able to accomplish. As soon as I checkout a new branch and do a pull, the old changes are overwritten on the file.

Why is this happening? Shouldnt inclusion of this file path into .gitignore effectively take out this file completely out of reach of the version control tentacles?

How can I achieve #3?

This question is unlike the one mentioned in the "possible duplicate" link. Heres why:

Here's basically what I want in lay words. "Put this one particular file of mine in essentially a 'shell', that neither gets overwritten during ANY git operations nor gets taken into account if changed". Its basically excluding any form of version control synchronization between that file and staging/remote. This local file and the version on staging/remote must exist with mutual and total disconnection.

Upvotes: 0

Views: 4137

Answers (2)

axiac
axiac

Reputation: 72226

Untrack this file from version control, which I tried to accomplish using .gitignore.

You tried and you failed. .gitignore is read only when you update the index, i.e. you prepare the next commit. If a file was already committed, adding it to .gitignore doesn't help telling Git to stop tracking the file.

You have to also remove the from the index (using git rm --cached) and commit the change. Only then Git stops tracking the file. Adding its name to .gitignore only tells it to stop listing it in the Untracked files section on the output of git status.

However, if you checkout a commit that was created before you removed the file from the repo, the commit contains the file and Git will try to check it out too.

Upvotes: 1

Mad Wombat
Mad Wombat

Reputation: 15105

This is happening because the file is already part of the repository. So when you switch branches you get the version of this file from the branch you are checking out. The gitignore file only prevents git from seeing files that are not a part of the repository. This is done, so git doesn't include certain files in commands like git add . or git commit -a. And assume-unchanged option tricks git into not seeing the changes, but that means that Git feels free to overwrite the file on checkouts, since it knows the file to be no different from the committed version.

If what you want is to remove this file from git control and manage the changes to it yourself, you can use git rm --cached /launcher/file.esb and commit the change. This will remove this file from the index and the file will no longer be tracked by git. The --cached option prevents git from actually removing the file from your working directory. You might have to do this separately for each branch though, since removing file from one branch doesn't remove it from other branches. To do this you might want to undo assume-unchanged and then use git stash command to temporarily stash your local changes.

Upvotes: 4

Related Questions