2

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?

NSF
  • 2,499
  • 6
  • 31
  • 55
  • possible duplicate of [Change first commit of project with Git?](http://stackoverflow.com/questions/2246208/change-first-commit-of-project-with-git) – janos Dec 27 '13 at 22:01
  • Unlike the problem in that post, the problem I have is I can do git rebase -i --root to reach the first commit but can't reset it so I'm not able to touch the files inside that commit. – NSF Dec 27 '13 at 22:11
  • Fair enough! And good luck ;-) – janos Dec 27 '13 at 22:41
  • I solved it by a quick and dirty way - make a backup of the files, go back to that commit, remove the files from git, commit the changes, return to the latest commit, restore the files. I guess there might be a way but in this particular case it doesn't really make a difference. :) – NSF Dec 27 '13 at 23:09
  • In that case it would have been easier to remove `.git` folder and the files you didn't want, and then `git init; git add .; git commit` – janos Dec 27 '13 at 23:18

2 Answers2

3

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.

akhikhl
  • 2,552
  • 18
  • 23
  • Is that gonna remove the file from the disk as well? – NSF Dec 27 '13 at 22:12
  • Um... you mean from working directory? Yes, it will. – akhikhl Dec 27 '13 at 22:16
  • Then in that case I think it's equivalent to do "git rebase -i --root", "git rm ..." and then "git rebase --continue", while what I want is to separate that file from the repository without deleting it. From the git perspective, it might not be supposed to support such operation though, as it doesn't know if later commits will contain that file and that might lead to confusion... – NSF Dec 27 '13 at 23:07
0

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.

Dom
  • 2,240
  • 16
  • 22