How to remove local git history after a commit?
Asked Answered
C

3

11

I would like to switch from Dropbox to the open source Sparkleshare. It uses git for the syncing and versioning. If say I had a 1GB file I deleted in my folder, it stays within the history of the local .git folder. But I would like to have this kind of heavy data on the server and not the client. How can I commit my repository and delete the local one with git? Many thanks!

Chrystal answered 22/3, 2012 at 9:11 Comment(1)
Maybe this will help: https://mcmap.net/q/23015/-moving-a-git-directoryForespeak
I
30

Solution removing the history

  1. git fetch --depth=1 to prune the old commits. This makes the old commits and their objects unreachable.
  2. git reflog expire --expire-unreachable=now --all. To expire all old commits and their objects
  3. git gc --aggressive --prune=all to remove the old objects

Specific solution just to remove the large file in the local history

  • GitHub offer's Git Large File Storage, which stores large files always on the server and fetches them only on checkouts. Very easy to setup and use - in case your repository is on GitHub, bitbucket, or gitlab.
  • git-annex - allows storing big files anywhere

Discussion: How do Git LFS and git-annex differ?

Inessential answered 8/5, 2016 at 12:26 Comment(0)
C
3

Based on koppor's answer, I created this single line bash command to clear the local git history from all repositories which are in the same base folder:

for d in */; do echo $d && cd "$d" && git fetch --depth=1 && git reflog expire --expire-unreachable=now --all && git gc --aggressive --prune=all; cd ..; done

Note: you can replace */ by any specific folder criteria if you wish, such as *mobile*/ .

Windows cmd version (untested):

FOR /D %d IN (*) DO (echo $d && cd "$d" && git fetch --depth=1 && git reflog expire --expire-unreachable=now --all && git gc --aggressive --prune=all & cd ..)

This has saved me a few gigabytes !

Cineraria answered 12/11, 2021 at 15:55 Comment(0)
S
0

Add the below commands in cmd or sh file and should help in clearing the history git fetch --depth=1 to prune the old commits. This makes the old commits and their objects unreachable. git reflog expire --expire-unreachable=now --all. To expire all old commits and their objects git gc --aggressive --prune=all to remove the old objects

Sharkey answered 18/4, 2023 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.