79

How do I discard all git commits that haven't been pushed and then force git to not commit anything in the data/ directory?

I'm trying to force overwrite all changes to a project stored on github from my c9.io project. I accidentally git added some mongodb data files. I tried to add the data/ directory to .gitignore. I pulled from github to get the updated .gitignore, however when I try and git push, these stubborn files in data/ are still staged for commit. I also tried git reset, git rebase, git clean and this where I changed dmpfile.sql to pi/data/node-login.1.

Please help I've been at this for hours and I'm really frustrated

Counting objects: 15, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 150.22 KiB | 92 KiB/s, done.
Total 13 (delta 6), reused 0 (delta 0)
remote: warning: File pi/data/local.0 is 64.00 MB; this is larger than GitHub's recommended maximum file size of 50 MB
remote: warning: File pi/data/node-login.0 is 64.00 MB; this is larger than GitHub's recommended maximum file size of 50 MB
remote: error: GH001: Large files detected.
remote: error: Trace: e6ade98208c08b634ed28aefea36dfbb
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File pi/data/node-login.1 is 128.00 MB; this exceeds GitHub's file size limit of 100 MB
To git@github.com:bobbyg603/goddard.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@github.com:bobbyg603/goddard.git'

.gitignore:

lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

data/

pids
logs
results

npm-debug.log
node_modules

.project
.settings

Thanks, Bobby

bobbyg603
  • 3,536
  • 2
  • 19
  • 30

5 Answers5

130

Thanks to Chris I was able to fix this by running the following:

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch pi/data/node-login.0'

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch pi/data/node-login.1'

git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch pi/data/local.0'

Community
  • 1
  • 1
bobbyg603
  • 3,536
  • 2
  • 19
  • 30
  • 2
    Be careful, it rewrites all the history. – Utopik Jan 21 '15 at 19:28
  • 1
    Or use [The BFG Repo Cleaner](https://rtyley.github.io/bfg-repo-cleaner/). I've done it both ways, and BFG is faster but takes a bit more care to set up, whereas git filter-branch is a nice one-liner. – bradw2k May 05 '15 at 23:25
  • This didn't move tags across and now everything is duplicated. – Daniel Ryan Jun 26 '15 at 02:12
  • 1
    I tried so many solutions and only this one worked. Thank you so much! – Bolun Zhang Jan 13 '16 at 04:00
  • 2
    To expand on Sheraz's comment, in Windows make sure to have double quotes instead of single quotes, and if your path and/or file has spaces in the name, surround it by single quotes. So `git filter-branch -f --index-filter "git rm --cached --ignore-unmatch 'path/with/spaces/in/file name.txt'"`. See https://stackoverflow.com/a/33741936/6169534 . Also make sure you're in the right directory! – conor Sep 07 '17 at 06:34
  • 1
    Guys , Is there affect on remote file ? – Dhaval Vaghela Oct 05 '17 at 08:26
  • @Camit1dk I think when you push this to origin it completely deletes that file from history. Be careful! – bobbyg603 Oct 25 '17 at 06:43
  • Getting `Your branch and 'origin/master' have diverged` after running this. Sigh, why is git so complicated.. – gargoylebident Oct 16 '20 at 16:48
  • Make sure to take care of all the unstaged changes otherwise you'll get `Cannot rewrite branches: You have unstaged changes.`. You can for example commit those and then the code will run. – gevra Aug 17 '21 at 11:44
72
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME" -- --all

replace FOLDERNAME with the file or folder you wish to remove from the given git repository.

Gank
  • 4,507
  • 4
  • 49
  • 45
  • I got another error about a dirty working directory. Fixed it by making sure my constantly updating jupyter checkpoints were ignored and my gitignore was committed then this worked. – Reen Aug 17 '18 at 21:27
  • It gives me error while pushing .. "error: failed to push some refs to https//..." and gives "hint: Updates were rejected because the tip of your current branch is behind" . But when i pulled it gave lot of conflicts. – Ammar Mujeeb Jul 07 '19 at 05:28
20

This worked for me:

git rm --cached name_of_a_giant_file
git rm --cached name_of_another_giant_file
git commit --amend -CHEAD
git push

Source: Github Help: Working with large files

Augusto
  • 2,125
  • 18
  • 27
biscuit314
  • 2,384
  • 2
  • 21
  • 29
  • 2
    Nothing happening. After applying this though it reduced total number of file but but after showing process 99% it stuck again. Any suggestion what I'm missing ? – CoDe Mar 08 '17 at 07:58
  • 2
    @biscuit314 Please update the command: git commit --amend -CHEAD – Anish Jul 06 '17 at 00:53
7
git filter-branch --tree-filter 'rm -rf path/whaever' HEAD

This is a little bit simpler and it is a way to just remove and rewrite it from the history. That's the result

Rewrite 5ac3045254d33fc5bb2b9fb05dccbdbb986b1885 (61/62) (24 seconds passed, remaining 0 predicted)
Ref 'refs/heads/features_X' was rewritten

Now, I can push

ackuser
  • 5,681
  • 5
  • 40
  • 48
2

I've used BFG Repo Cleanner simple and fast, works like a charm

jthill
  • 55,082
  • 5
  • 77
  • 137
Victor Laerte
  • 6,446
  • 13
  • 53
  • 102