0

I used to have one folder per repository and push to gitlab. Later I wanted to group all these codes into a folder (Wrapper). I have remove the .git in each of these folders

-Wrapper   // git initiated
 -Project1 // .git removed
 -Project2 // .git removed  

The problem was it pushed the following directories

teams/*
dist/*
node_modules/*
temp/*
lib/*

I have rectified the problem with these commands

git rm -r --cached .
git add .
git commit -m "Untracked files issue resolved to fix .gitignore"

and added a .gitignore file in the Wrapper root folder. The repository still says 80MB.

I have attempted the following

git filter-branch --tree-filter "rm -rf node_modules" --prune-empty HEAD
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
git push origin --force --all
git push origin --force --tags

and tried this

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch *node_modules*' \
  --prune-empty --tag-name-filter cat -- --all
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
 git push origin --force --all
 git push origin --force --tags


My repository is still 80MB. There is an option, which is to create a new project, create a new folder, put all projects in the folder, git init ... and git push will solve it, but is there chance I can keep my git history?

I checked my git log using this command,

git log --name-status >1.log

and it shows my history in adding the node_modules were removed.

$ git count-objects -vH
count: 0
size: 0 bytes
in-pack: 431
packs: 1
size-pack: 1.48 MiB
prune-packable: 0
garbage: 0
size-garbage: 0 bytes


Random I.T
  • 121
  • 1
  • 10

1 Answers1

1

In addition of "How to remove/delete a large file from commit history in Git repository?", the best practice is to use the new tool git filter-repo which replaces BFG and git filter-branch.

git filter-repo --strip-blobs-bigger-than 1M --refs 84971f1..master

See the "Filtering based on paths" section

git filter-repo --paths-from-file toBeRemove.txt --invert-paths 

Put in toBeRemove.txt:

teams/
dist/
node_modules/
temp/
lib/

Note: if you get the following error message when running the above-mentioned commands:

Error: need a version of `git` whose `diff-tree` command has the `--combined-all-paths` option`

it means you have to update git.


If the local repository is good, but the remote repository is still reporting a big size, it might be a reflog issue, where a git gc done automatically by the repository remote hosting server will fix that reported size later.

The OP confirms in the comments:

the online repo GitLab is not updating the file size.
I just did a git clone and the repo is back to 1.48MB, problem solved.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • my local repo shows 1.48MB, commit log show no record for node modules. online repo shows 80MB, cannot locate node modules, commit log show no records for node modules. I have used git push force after git gc but local repo not sync with online repo ? – Random I.T Mar 16 '20 at 05:39
  • @RandomI.T did you `git push --force` your rewritten repository? If yes, the remote repository might still have the old history in its reflog. A `git gc` done on their side will eventually get rid of the old history. – VonC Mar 16 '20 at 05:41
  • @RandomI.T I meant, GitLab will do its own `git gc` eventually, on its side. The reported size will diminish then. – VonC Mar 16 '20 at 05:42
  • I found the problem, the online repo gitlab is not updating the file size. I just did a git clone and the repo is back to 1.48MB, problem solved. Thanks VonC. – Random I.T Mar 16 '20 at 05:45
  • @RandomI.T Thank you for the feedback. I have included it in the answer for more visibility. – VonC Mar 16 '20 at 05:48