git smart-push for OSS contributor

I contribute to many OSS projects, and what I normally did is:

  1. Clone the repository (rails/rails)
  2. Fork the repository (juanitofatas/rails) (with hub fork)
  3. Create a feature branch (doc/patch-1)
  4. change, add, commit
  5. git push forked-remote feature-branch-name

git push

By default git push will push to the remote you cloned with in step 1. I usually clone the source because it is easier to keep up with the upstream.

So when you git push, it will try to push to the remote that you cloned (normally don’t have access to the source repository, you’re contributing!).

So I will often type this instead:

git push juanitofatas doc/patch-1

And this is tedious, introducing git smart-push (hahaha):

git smart-push

git smart-push

What this script does is to push the current branch you’re on, to the remote that you forked (looking by your Git username). You can also pass in Git’s force option (I recommend --force-with-lease, can also use -f, --force).

Use git smart-push

To use this script, get it to any place in the $PATH, grant it execution right (chmod +x), then you can use it.

Add an alias

$ git config --global alias.sp smart-push
$ git sp # `git sp' now aliased to `smart-push'

Happy Contributing 🎉