_kud's tech blog

$ git contrib

A new command for my git worflow.

You probably don't know me so much but I love when something is perfect, shiny, well designed. Yes, some people could say I'm a maniac. Anyway. 😜

So why we're here today. We are here today because I want to talk about how to make a good fork on GitHub (or whatever but my command only works on GitHub yet) to contribute to a project and not to have an alternative of this one.

Indeed, when you fork, you could have two ideas behind:

  • Because you want to make your own version of this project

  • Because you want to contribute by making some PRs to this project where you aren't not a maintainer (yet?)

This is the second case we're interested.

If you fork only because you want to contribute, in my opinion, you should notify any visitor that it is not a project they can use.

And as developer, you won't care so much about your master branch, you won't update it, you won't use it to make your PR, you only will only submit feature branches, one by one.

So. How to do this?

We'll make the command step-by-step.

First of all, we fork a project via this great cli command:

1#! /usr/bin/env zsh
3gh repo fork $1 --clone

Fine. Now we want to create the master branch as an orphan one where we notify the user it is not a real project, just a contribution.

1#! /usr/bin/env zsh
3gh repo fork $1 --clone
7git co -b tmp &&
8git branch -D master &&
9git checkout --orphan master &&
10git branch -D tmp &&
11git rm -rf . &&
12rm '.gitignore' &> /dev/null
13echo "**Repository used only for submitting PRs. 🙌**" > README.md &&
14git add README.md &&
15git commit -a -m "Initial Commit" &&
16git push -f origin master

As you can read, we create a temporary branch, we delete master, we recreate master as orphan, we remove any tracked files, and we create a commit with only a README saying what we want to notify. And finally we force push it.

This is what we've got:

Now if anyone goes to your fork project, they know it is not an extended one, just your base to contribute.

The final script. We just need a way to get to name of the project to open it in the shell.

1#! /usr/bin/env zsh
3# ex: git contrib git@github.com:gajus/create-index.git
9gh repo fork $1 --clone &&
10cd $REPO &&
12git co -b tmp &&
13git branch -D master &&
14git checkout --orphan master &&
15git branch -D tmp &&
16git rm -rf . &&
17rm '.gitignore' &> /dev/null
18echo "**Repository used only for submitting PRs. 🙌**" > README.md &&
19git add README.md &&
20git commit -a -m "Initial Commit" &&
21git push -f origin master

And how to use it:

$ git contrib git@github.com:gajus/create-index.git

You'll get a new project called there create-index as folder with those settings for remote:

1# $ git remote -v
2origin git@github.com:kud/create-index.git (fetch)
3origin git@github.com:kud/create-index.git (push)
4upstream git@github.com:gajus/create-index.git (fetch)
5upstream git@github.com:gajus/create-index.git (push)

So clean! 🙌🏻

Now we're good and can contribute to lots of projecs, oh yeaaaaaah.

Want to see all my git commands? This is on my environment manager project.