Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop Git from tracking any changes to a file from this commit forward?

Tags:

git

I have a database configuration file that has default values that are unimportant. However, any changes to this file would contain sensitive information that should not be tracked in the repo.

I would like future pulls of the Git repository to include the default version but disregard any changes made by any user.

The following keeps a local configuration but pushes the delete to the repo resulting in issues for future pulls.

cat "app/dir/config.file" >> .gitignore git rm --cached app/dir/config.file 

The following does the job but does not persist past the push to the repo.

git update-index --assume-unchanged app/dir/config.file 

This seems like a common requirement for version control around sensitive information but I can't seem to find a solution.

like image 594
Ben Campbell Avatar asked Aug 16 '13 15:08

Ben Campbell


People also ask

How do I turn off track changes in git?

Using git update-index to Stop Tracking Files in Git Sometimes, we may wish to keep a file in the repository but no longer want to track its changes. We can use the git update-index command with the --skip-worktree option to achieve this.

How can I ignore files that have already been committed to the repo?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.

How do I ignore an already committed file 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.


2 Answers

As usual github has a great doc on this.

https://help.github.com/articles/ignoring-files#ignoring-versioned-files

Here's the relevant snippet:

Ignoring versioned files

Some files in a repository change often but are rarely committed. Usually, these are various local configuration files that are edited, but should never be committed upstream. Git lets you ignore those files by assuming they are unchanged.

  1. In Terminal, navigate to the location of your Git repository.
  2. Run the following command in your terminal:

git update-index --assume-unchanged path/to/file.txt

Once you mark a file like this, Git completely ignores any changes on it. It will never show up when running git status or git diff, nor will it ever be committed.

To make Git track the file again, simply run:

git update-index --no-assume-unchanged path/to/file.txt.

like image 53
Rob Avatar answered Oct 10 '22 08:10

Rob


Not sure if this is the "best" way... but what I have found to work takes a few steps.

New Laravel Example:

  1. git init
  2. Adjust .gitignore files for desired results like not losing the vendors folders.
  3. Make a copy of the files you don't want tracked (ex: .htaccess, .env, etc)
  4. git add .
  5. git commit -m "first commit"
  6. git rm --cached public /.htaccess then git rm --cached .env
  7. git commit
  8. git status should show those files as deleted.
  9. Now put the file you copied back. Since they are the same name as what you told git to remove from tracking, git will now fully ignore those files.

Now those can be edited, and you can have local and production versions. NOTE: You may have to repeat all these steps the first time on the production set-up as well. So far this has worked well for me.

like image 43
bikermusician Avatar answered Oct 10 '22 09:10

bikermusician