So you’ve forked a repository from someone and they happen to be using git. This is great and with git (and GitHub) this is so easy and so simple. Heck, on GitHub, they want you to press that fork button. And this is all wonderful except for two things.
- You can’t search a fork on GitHub.
- Merging back into your fork is confusing.
The first issue drives me nuts.
That it says “currently” gives me some hope, but it’s one of the most annoying aspects of a fork on GitHub.
The second issue is bigger than just GitHub.
Sometimes when you fork, you never want to go back, and that’s sensible. You’ve decided to go a different way. That’s how most of us view a fork, after all, because we’re used to repositories being silo’d and stand alone (like with SVN). But with git, you can actually send your fork repo as a pull request to the original for them to merge in your changes. And the reverse is also true, so if you and another dev have a fundamental difference on something you can’t hack with an add-on, you have options that don’t involve reading every line of code and copy/pasting.
Yes, I did that before.
Thankfully you won’t have to. You can follow three steps to do this.
Add the Upstream
Technically you should do this any time you make a fork, but if you use GitHub you probably forgot. After all, GitHub has that nice ‘Pull Request’ button for you, which takes care of it. They want you to cross contribute and, bless them, they make it quite easy to do so.
Instead, you’ll want to manually tell your repository that yes, Virginia, there is an upstream. This is the parent repository and it’s one command:
git remote add upstream ORIGINALREPO
On GitHub it looks like this:
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Simple. Done.
Fetch the Upstream
Now you want to fetch the upstream repository so your clone of the repository has the code it will need to merge.
git fetch upstream
Yeah, it’s that simple. It’s pretty much making a branch and fetching its changes. At this point, you’ve not made any changes to your own code.
Merge with Upstream
This works best on master to master, but I’ll bet you can also set a branch and merge that way.
git merge upstream/master -m "Merging from upstream"
If there aren’t any commit differences, it fastforwards. Otherwise you get a merge done safely, your changes stick.
But I don’t use Git on the CLI!
According to Hermes Pique, you can do it this way:
- Open your fork on GitHub.
- Click on Pull Requests.
- Click on New Pull Request. By default, GitHub will compare the original with your fork, and there shouldn’t be anything to compare if you didn’t make any changes.
- Click on switching the base. Now GitHub will compare your fork with the original, and you should see all the latest changes.
- Click on Click to create a pull request for this comparison and assign a predictable name to your pull request (e.g., Update from original).
- Click on Send pull request.
- Scroll down and click Merge pull request and finally Confirm merge. If your fork didn’t have any changes, you will be able to merge it automatically.
Me? I like the CLI.