0

I want to free some space on my machine and my git history is getting quite large. I have about 20 projects where I have the full history pushed to GitHub, so it would be enough for me to only keep the latest commit on my machine.

My two questions are:

  1. How can I remove all commits but the latest one from a project, but only locally? I want to keep all history on GitHub and also push new commits in the future.

  2. How would a command line script in Linux look like that goes over my project folders and repeats the action of only keeping the latest commit for each project. Here is what my folder structure with my projects looks like:

GWDG+mschmidh@UG-UFBW100-C393 MINGW64 /p/r/projects
$ ls
be-a-mapper/
biogeochem-processes/
climathur/
coding/
coding-examples/

Many thanks for your help!

ms-soil
  • 13
  • 2
  • Does this answer your question? [Using git to get just the latest revision](https://stackoverflow.com/questions/1209999/using-git-to-get-just-the-latest-revision) – Malik Jul 12 '21 at 10:07
  • To clarify, I want to avoid cloning again. Can I do what I noted just locally. This way, maybe it can be done with several projects in a command line script? – ms-soil Jul 12 '21 at 10:24
  • https://stackoverflow.com/search?q=%5Bgit%5D+keep+only+latest+commit – phd Jul 12 '21 at 11:49

1 Answers1

1

What you are searching for is git clone --depth 1

--depth

Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless --no-single-branch

is given to fetch the histories near the tips of all branches. If you want to clone submodules shallowly, also pass --shallow-submodules.

So just do git clone --depth 1 https://github.com/mypersonal/repo and your .git folder should be smaller.

Malik
  • 878
  • 2
  • 9
  • 23
  • Ok, thanks, that helps. I can now `rm -rf` my whole project and then get only the latest commit back from GitHub with `git clone --depth 1`. But can it be done with just modifying the local repo and for many folders at once? – ms-soil Jul 12 '21 at 10:18
  • DONE! I incorporated your solution, @Malik, into a bash script: line 1: `#!/bin/bash`, line 2: `cd ~/r/projects/`, line 3: `rm -rf $1`, line 4: `git clone --depth 1 "https://github.com/my-repo/$1.git"`. This first goes to the folder, removes the project of your choice, then clones only the last commit from GitHub back to local. The script can be saved as `keep-only-last-commit.sh` and be run, e.g. in Git Bash using `./keep-only-last-commit.sh coding-examples`. Thanks again! – ms-soil Jul 12 '21 at 11:31