8

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.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70

4 Answers4

19

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...
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1
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'
Jonathan Feinberg
  • 44,698
  • 7
  • 80
  • 103
0

Use ignore: http://github.com/guides/ignore-for-git

Adrian Toman
  • 11,316
  • 5
  • 48
  • 62
Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
0

Add the file name to a .gitignore file.

Link

pmlarocque
  • 1,704
  • 1
  • 18
  • 26