0

I have many large binary files with source code under git version control.

I want to only push the source files to github for repo initialization.

So I used git rm -r --cached to remove the large binary files from the index.

After checking git ls-tree -r --names-only mybranch, I confirmed there are only text source files in the index.

When I then pushed to github, git checked 3 thousand files to transfer, and started pushing about 1GB content to remote github. But those source files together occupy merely 53KB.

From the logs I found that the binary files in historic commits were being pushed to remote.

Is there an option that only pushes my current index? like :

git push --depth 1

Thanks in advance for any advice.

Chiron
  • 974
  • 1
  • 8
  • 20
  • 1
    You have to clean the files from the older commits, use [BFG Repo-Cleaner](https://rtyley.github.io/bfg-repo-cleaner/) – user000001 Apr 14 '18 at 15:55
  • 1
    Did you commit the changes after executing git rm -r --cached? If you didn't commit the changes, that may cause the binary files pushed to github repo. And Has your problem been solved? If yes, you can accept the answer which helps. – Marina Liu Apr 16 '18 at 06:46
  • @MarinaLiu-MSFT Thank you. I did commit the changes before pushing. I really appreciate the excellent ideas provided by phd and code_fodder, and I've been actively watching this problem. But I think the solutions we currently are either ugly in practice or evasive, which I think may not provide adequate as well as feasible advice for others. Thank you again for your reminding and suggestion! – Chiron Apr 17 '18 at 08:49
  • 1
    @Chiron Can you still reproduce the issue? If not, then the only way is removing the binary files from your github repo (as below answers suggest) instead of analyzing. – Marina Liu Apr 18 '18 at 07:56
  • @MarinaLiu-MSFT I've been experiencing and fighting this issue for the past several days. I have many projects with large bins to export. I'm getting near to giving up. – Chiron Apr 19 '18 at 07:45
  • 1
    @Chiron If you have environment it test, please execute these commands and check the result: 1. `git rm -r --cached filenames` 2. `touch .gitignore` and specify the binary file names in it 3. `git status` to check if the binary files are ignored 4. `git add .` 5. `git commit -m 'ignore the binary files'` 6. `git status` check if working tree is clean 7. `git push`. – Marina Liu Apr 19 '18 at 07:52
  • @MarinaLiu-MSFT Thank you. I tried your advice. The binary files are successfully ignored. But historic bins still present in pushing commits. I think I'll have to write a script and do the dirty job – Chiron Apr 21 '18 at 02:23

2 Answers2

2

You have a few options here.

A simple approach, If you are happy to re-initialise your git repo and "start-fresh", is to just delete your .git folder and re initialise it:

  • rm -rf .git
  • git init
  • git add ...
  • git commit -m"..."

Only add the files you want (not the binaries) - infact add the binaries to the .gitignore file, e.g. if they are all called x.out, y.out, z.out then you can add the following to your .gitignore:

  • *.out

Or if they are all in some output folder you can add the following to your .gitignore:

  • outputfolder/

If you want to re-add your remote then you can do the following:

git remote add origin <remote-url>

update

If you need over-write your remote then you can use:

git push -f origin master

code_fodder
  • 15,263
  • 17
  • 90
  • 167
2

binary files in historic commits were being pushed to remote.

Yep, that's how git works.

Is there an option that only pushes my current index?

Nop, that's not how git works. Git doesn't work with files, git pulls/pushes/works with direct acyclic graphs of commits (and accompanying objects — trees and blobs).

If you don't want to push large files you have to remove them from the entire history using either git filter-branch or BFG Repo-Cleaner.

If you want to drop the entire history you can create a new orphan branch, create commit from the index and push the new branch. Old branches could be removed then, and the new branch could be renamed to master.

phd
  • 82,685
  • 13
  • 120
  • 165