NSF
NSF

Reputation: 2549

How to remove some files accidentally added in the first commit?

All commits are on local. No remote things related.

I have some commits already (say AA, BB and CC) but I wanna remove some files in the first commit(AA). Usually say if I need to change something in BB I do git rebase -i BB^ and then git reset BB^ and make changes then but this time git doesn't allow me to touch anything before the first commit (i.e. I can't do rebase AA^). I tried git checkout AA but in that case I would be in detached state and still couldn't change anything in that commit apart from rewording the commit message.

How can this be solved?

Upvotes: 2

Views: 1687

Answers (2)

Dom
Dom

Reputation: 2410

I ran across this searching for an answer on how to remove data accidentally included and I would like to document what I found and ended up using.

My final resolve was found in the help files of github.com: https://help.github.com/articles/removing-sensitive-data-from-a-repository/

To summarise: Github proposes to use the BFG repo cleaner. https://rtyley.github.io/bfg-repo-cleaner/. They say: The BFG Repo-Cleaner is a faster, simpler alternative to git filter-branch for removing unwanted data. For example, to remove your file with sensitive data and leave your latest commit untouched), run:

$ bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA

To replace all text listed in passwords.txt wherever it can be found in your repository's history, run:

$ bfg --replace-text passwords.txt

See the BFG Repo-Cleaner's documentation for full usage and download instructions.

Hope this helps someone.

Upvotes: 0

akhikhl
akhikhl

Reputation: 2578

The following should solve your problem:

git filter-branch --tree-filter 'rm fileToRemove' --prune-empty --force HEAD

where fileToRemove is to be replaced with a file name or mask.

To remove a directory, do:

git filter-branch --tree-filter 'rm -fR dirToRemove' --prune-empty --force HEAD

This effectively removes the specified file/dir from ALL revisions of the given repo, as it would never exist.

As a precaution, clone your repo before the change.

Upvotes: 3

Related Questions