Deleting unwanted forks from GitHub

Photo by Yancy Min on Unsplash

At some point in my life I thought it would be a good idea to start forking git repos in GitHub instead of just starring them. I guess it started with a fear the repos might not exist in some point in the future and that I would like to have a copy for reference. But really how many times do I ever go back to stored bookmarks, or saved copies of things. Guess I am a digital hoarder.

Deleting forks you no longer want from GitHub is a real PITA.

Enter this repo https://github.com/yangshun/delete-github-forks to save the day!

Setup a GitHub token to be used by delete-github-forks

Go to https://github.com/settings/tokens/new and create a new token. I used delete-github-forks as the Note field.

Set the public_repo and delete_repo permissions for the token.

Generate the token and then make sure you copy the token and store it securely (I use 1Password).

Configure delete-github-forks

Clone the repo.

$ cd [some directory you want the repo to be stored]
$ git clone https://github.com/yangshun/delete-github-forks.git
$ cd delete-github-forks

Install dependencies.

$ npm install

Configure the credentials to be used against GitHub.

$ cp src/config.json.example src/config.json
$ vim src/config.json   [I like TextMate, so normally I am lazy and just use the mate command]

Specify your GitHub username and the token you generated earlier.

Fetch the list of forks

$ cd src
$ node fetch-repos.js

...
Forked repos found: 50
^^ Geez!

This creates a file called repos.json and you need to remove all the forks that you actually want to keep.

It is worth reiterating from the repo’s documentation:

The repositories that remain inside repos.json will be deleted on the next command. It is an irreversible operation. Use with great caution!.

See the last section on how I did a quick verification on each fork to see if I wanted to keep it or not.

Deleting the unwanted forks

See the warning above. Make sure you only keep the forks in repos.json that you actually want to delete.

Delete the forks.

$ node delete-repos.js

Amazing in less than 2 seconds this deleted 68 forks I was no longer needing!

Miscellaneous

I wanted to visit the GitHub page for each of the forks. So I thought I would open all the tabs in bulk from the command line

$ sed -e 's|"andrejacobs|open "https://github.com/andrejacobs|' repos.json | sed -e 's/",/"/' | awk '{$1=$1};1' | bash

Breakdown

Replace “andrejacobs with: open “https://github.com/andrejacobs

sed -e 's|"andrejacobs|open "https://github.com/andrejacobs|'

Replace “, with: “

sed -e 's/",/"/'

Trim whitespace

awk '{$1=$1};1'

This is by far not the most optimized or best way but it did open 50+ tabs in my browser.

The process was pretty simple. I check what the fork is about and even visit the repo it was forked from to see if it still exists etc. and then made an informed decision if I wanted to keep my fork or not. Also now was a good time to start starring repos I cared about and NOT fork them.