Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - remove and exclude configuration file

2 weeks ago, I commited config of my application which had my password, it's not very useful. How can I remove the file from the commit history and make sure it doesn't get re-commited?

I want to remove the file from all commits tree because it contains my passwords.

like image 462
Andrey Kuznetsov Avatar asked Dec 07 '09 19:12

Andrey Kuznetsov


People also ask

How do I ignore a specific 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.

Should I Gitignore config files?

gitignore file allows you to ignore files in a Git repository. You may not want to include all the files in your local copy of a project in your commits. For instance, you may not want to commit compiled code, or system logs, or config files. To ignore files, you can specify which ones you want to ignore in .

How do I remove a file from a git track?

Removing Files To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command does that, and also removes the file from your working directory so you don't see it as an untracked file the next time around.

How do you exclude a file from a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


2 Answers

You can

git rm myConfigFile
echo myConfigFile > .gitignore
git add .gitignore
git commit -m "from now on, no more myConfigFile"

The other extreme approach (dangerous especially if you have already pushed your repo to a remote one) would be to entirely remove that file from the history of said repo:

git filter-branch --index-filter 'git update-index --remove myConfigFile' HEAD

(to use with care, and with a backup first)

The question How do I remove sensitive files from git’s history has more on that sensitive topic.

The problems with this process are twofold:

  1. If your repo has already be cloned, you can never guarantee the confidential information will be really "gone" from every other repo.
  2. When others try pull down your latest changes after this, they'll get a message indicating that the the changes can't be applied because it's not a fast-forward.
    To fix this, they'll have to either delete their existing repository and re-clone it, or follow the instructions under "RECOVERING FROM UPSTREAM REBASE" in the git-rebase manpage.
    In both case, your confidential information will not be "quietly" replaced or erased...
like image 165
VonC Avatar answered Nov 13 '22 19:11

VonC


cp my-config config.tmp
git rm my-config
git commit -m 'removed config'
mv config.tmp my-config
echo my-config >> .gitignore
git add .gitignore
git commit -m 'ignore config'
like image 20
Jonathan Feinberg Avatar answered Nov 13 '22 18:11

Jonathan Feinberg