Bringing a Git branch up-to-date with its origin - an atomic hack
So, you want to contribute some code from your repo to the original project? But Github is showing other commits you don’t want to include, and you don’t know how to remove them…
Method #1 - do things properly
Learn Git, understand the project’s workflow, set up your local repositories correctly, set up the remotes, issue the correct git commands. This method is not the subject of this post… :-)
Method #2 - the ugly, but practical, hack
“But I just want it to let me do a simple PR! I don’t have time to learn all that…“
Ok, so here’s a nice little atomic procedure for you. Atomic doesn’t mean it explodes (hopefully), it means it’s self-contained, does its job without any previous set up, and leaves no local traces behind it. It’s fire and forget.
What this does is create a new directory, start a local clone, set everything up, bring a branch on your forked repo up to date with the origin repo, and delete everything local in the end. After that, you can easily and safely do your PR.
Image taken from here, then edited by me.
See, when you’re on the online GitHub site, you only care about the top part of this scheme. You don’t want any work setting up the lower block, your local install, and learning about Push and Pull, when all you need to do is bring a branch up to date with SalesAgility (origin with upstream). So what the script does is create the lower part automatically, do it for you, and remove it afterwards.
So, here’s the script, complete with reassuring comments:
Make sure you edit those two variables at the beginning (user
and fork
), to fill them with appropriate values referencing your Github repo.
Of course, with a proper git set up you would avoid the long delay of fetching the entire project every time.
This is for Linux but it works great in Windows 10 if you install the Linux Bash Shell from Microsoft.