Monday, February 18, 2013

Remove a file completely in GIT

GIT is a great version control tool. I was a CVS and SVN users but I think GIT is even more powerful. Perhaps I can write something about GIT but now I want to share an experience: How to remove a file completely from GIT.

There is an internal db in GIT. Once your add and commit codes into the repository, the info will be store and the great thing is that you can check out the file at any version. However, I did an stupid thing - I hard coded some personal information (eg password) in a file and commit it to the repository. It won't be a problem if you are the only developer of the project.  But if you need to share the code by cloning the repository to the others, your information can be retrieved, even you delete it in filesystem.

So I did some quick search and find a solution: just runt the following command to remove the history and file:

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch " HEAD

Make sure you make a copy to some places out of the GIT repository. Then you can copy the file back and add it as a "fresh new" file.

PS. As a better practice, don't hard coded any thing that cannot be shared in a repository !!

No comments: