Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore file from git commit but protect it from clean

Tags:

git

gitignore

In my work tree I have a mixture of files in .gitignore:

  1. Build outputs
  2. Sensitive files that shouldn't be part of the repo (e.g. passwords, connection details for local servers)

If I want to rebuild my project from scratch, I can do git clean -x to remove the build outputs in (1). But this also removes the sensitive files in (2).

Is there a way to mark a file so it's not affected by git clean while still being ignored by git commit?

like image 321
Tim Robinson Avatar asked Apr 28 '17 12:04

Tim Robinson


People also ask

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.

Does Git clean remove files in Gitignore?

git clean -X will remove any files matching the patterns in . gitignore . The -n at the end causes it to not actually remove files, only to dry run, reporting files that would have been removed. Add -d to also remove ignored directories.

How do I force Git to ignore a file?

You can create a .gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the .gitignore file in to your repository.

Does Gitignore remove files already committed?

Let's say you have already added/committed some files to your Git repository and you then add them to your . gitignore file; these files will still be present in your repository index.


1 Answers

The documentation for git-clean says that -x:

-x

Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options.

So if you want to keep, say, keys.pem, you would call

git clean -fdxe keys.pem

You can also create alias for the command.

like image 53
Jan Hudec Avatar answered Sep 23 '22 15:09

Jan Hudec