Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore files locally without adding it in .gitignore

Tags:

git

github

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 ?

like image 562
Anurag Singh Bisht Avatar asked Aug 18 '17 09:08

Anurag Singh Bisht


People also ask

How do I ignore certain files in Git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a .gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

How do I ignore a file in Git desktop?

To ignore files in your repository with GitHub Desktop go to the Repository menu and select Repository Settings… With the Repository Settings pop-up open, click the Ignored Files tab. Here you will be able to add file names, directory names, or patterns for Git to ignore in your repository.

Can I have local Gitignore?

A local . gitignore file is usually placed in the root directory of a project. You can also create a global . gitignore file and any entries in that file will be ignored in all of your Git repositories.


1 Answers

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.

like image 199
zigarn Avatar answered Sep 30 '22 21:09

zigarn