开发者

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 开发者_如何学Gobecause it contains my passwords.


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...


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'


Add the file name to a .gitignore file.

Link


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜