tags:

views:

320

answers:

2

I noticed a moment ago that my .gitconfig -file was public at my repo. It contains my secret tokens.

I git-add -u the file and committed. I am not sure whether this command removes the file from the previous commits too.

I want to be sure and search the file in my previous commits such that there is no such a file in my history.

How can you search the file in previous commits?


I run

git filter-branch --tree-filter 'rm .gitconfig' master     # Thanks to Greg!

I get

Rewrite 84dabfa2ea195ce9aad0309216858b302150017c (1/25)rm: .gitconfig: No such file or directory
tree filter failed: rm .gitconfig

The error message suggests me that I do not have the file at my commit history.

Is there any way to search the file in my commit history such that I do not need to run the removal to see that I do not have the file in my commit history?

+4  A: 

To remove a file from all past Git history, you will need to rewrite all past commits using something like git filter-branch. However, doing this makes your new tree unmergeable with all past trees, so do this with care.

Greg Hewgill
I would like to play with the Git -files in /tmp. --- I played unsuccessfully with them after I copied .git to /tmp. --- This suggests me that I need to copy all files to /tmp with .git. --- @ How would you make a 2nd copy such that you can test the command?
Masi
You can clone a repository with "git clone". Anything you do to the cloned repository does not affect the original repository (unless you push your changes, of course).
Greg Hewgill
+4  A: 

Short answer:

git filter-branch --tree-filter 'rm -f .gitconfig' master

Note the -f.

Long answer:

rm would return a non-zero return code when file does not exist. filter-branch think this is an error and stop the rewrite. Adding -f avoid this.

Note:

There is an alternative command:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch .gitconfig' master

which is MUCH faster. This command is documented in the EXAMPLE section of the man page.

J-16 SDiZ
@J-16: Both your commands give me the same error message as in my question. This suggests me that I have removed the file by accident by some command already. --- Do you know how you can search .gitconfig in Git's history such that I can be sure that there is no such file?
Masi
just `git log .gitconfig` should show the history. NOTE: .git/config (and anything under .git/) is *NOT* versioned, only .gitconfig.
J-16 SDiZ
@J-16: I get the info that the commits 57d6 and 0d97 contain my secret tokens. (1) How can I see these commits? (2) How can I remove these .gitconfig files from these commits?
Masi
@J-16: Your last command gives me the following warning although I have .gitconfig -files in my history: Cannot rewrite branch(es) with a dirty working directory.
Masi
"dirty working directory" means you have uncommitted works.. Run `git status` and commit (or reset) them.
J-16 SDiZ