9

I have a github repository, which I checked out locally.

  • I changed my environment variables in environment.local-dev.ts file. (It usually points to localhost, but as I don't have local api repository installed, I updated file to point to my colleagues local api)
  • Now, the changed configuration is specific to my need. So, I don't want to commit these changes to the Github repository

I searched for two options :

  • To add file in .gitignore. But the problem is, I will still see .gitignore, which I might commit accidently.
  • To add it in .git/info/exclude. I did this, but I am still seeing the changed files when I do git status

The Github repository, I am working on is already created by someone else. So cannot change the initial configuration. Also other people are working on the same repository so cannot commit changes which might affter other people negatively.

So, the problem is to find a way to not commit files which are being tracked by git. Is there a way to do this ?

Anurag Singh Bisht
  • 2,683
  • 4
  • 20
  • 26
  • 1
    If the file is already tracked by Git, adding its name to `.gitignore` or `.git/info/exclude` doesn't have any effect. Git consults these files only when it adds to the repository files that it doesn't already track. You also have to `git rm --cached` the file you want to untrack and then commit. – axiac Aug 18 '17 at 09:54
  • Be careful, with `git rm` (after push) you will remove the file also from github and probably this is not what you want! – Francesco Aug 18 '17 at 10:13
  • Possible duplicate of [Git - preventing a particular commit from ever being pushed](https://stackoverflow.com/questions/45484019/git-preventing-a-particular-commit-from-ever-being-pushed) – Francesco Aug 18 '17 at 10:16
  • I don't think this is a duplicate of that question. My problem is very different. As my files are already being tracked by git and available in github. – Anurag Singh Bisht Aug 18 '17 at 10:17
  • @axiac - `git rm --cached` as pointed out by Francesco, will remove it from github repsository also. I dont want this. I have a temporary change in my local due to my `local backend` not working properly. For time being I want to use `local ip ` of my colleague but I don't want to do any changes in the github repo. – Anurag Singh Bisht Aug 18 '17 at 10:19
  • 1
    Either you want to track the file or you want to ignore it. There is no other option. In fact, you have a different problem. You committed configuration files and this is a common mistake. Read [this answer](https://stackoverflow.com/a/38999817/4265352) on a similar question; it tells you how to correctly store the configuration files in a VCS. – axiac Aug 18 '17 at 10:35
  • Yes I know what I want to do is not conventional. I searched about it and couldn't find the answer, therefore I asked the question. It's a common problem which I think people must face. The repository is already created by someone else. I don't have control what has been committed. Now the problem is if I want to change configuration for my system but don't want to commit it so that other contributors don't get affected. I know the answer of this question might be "Sorry Cannot do this". But I needed to ask to know if that's the case or not. – Anurag Singh Bisht Aug 18 '17 at 11:05

3 Answers3

11

EDIT: as pointed by @Philippe skip-worktree would be more appropriate than assume-unchanged. In this answer, everything is the same but you just switch the option.

.gitignore and .git/info/exclude are only to exclude untracked files.

To ignore local modifications of tracked files, you can use git update-index --assume-unchanged FILES....
But there is a risk to forget modifications that need to be committed in the file.
To get the modification visible again: git update-index --no-assume-unchanged FILES....

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

$ git update-index --assume-unchanged file

$ git status
On branch master
nothing to commit, working tree clean

$ git ls-files -v
h file

$ git update-index --no-assume-unchanged file

$ git ls-files -v
H file

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file

no changes added to commit (use "git add" and/or "git commit -a")

You can define some aliases:

[alias]
    forget = update-index --assume-unchanged
    unforget = update-index --no-assume-unchanged
    forgotten = ! git ls-files -v | grep ^[a-z]

A better way would be to have an local ignored configuration overwritting file.

zigarn
  • 10,892
  • 2
  • 31
  • 45
5

Try this:

git update-index --skip-worktree environment.local-dev.ts

For further reading, see git update-index documentation and other related questions, like Git - Difference Between 'assume-unchanged' and 'skip-worktree'.

Mika
  • 1,256
  • 13
  • 18
2

Contrary to what said other answers, that not the assume-unchanged feature (that is reserved for big files that is costly to check) that you should use but skip-worktree.

See https://stackoverflow.com/a/13631525/717372

Philippe
  • 28,207
  • 6
  • 54
  • 78