Help my Fork is Broken!

Oops, I just made the mistake of checking into the master branch of my fork of JUnit on GitHub. This would make it tricky for me to create PRs. Let’s fix it.

Wow – that’s a stale copy and my commit is a problem…

Let’s look at the commits:

Yes. My commit is a blot on the landscape here. If only I could get the juicy goodness of commit d92e034 into a branch off the head of the real master, maybe the owner of the origin might accept my PR!

First things first. Let’s make sure I have my commit somewhere useful. If we create a branch and push it, then I can always go back to the tree which includes my change on top of the commit where I first made it.

# from my working folder - create a branch
# which can hold onto my commit
git checkout -b branch_with_my_work

# store this branch up on github
git push --set-upstream origin branch_with_my_work

So my commit is safe, no matter what I do… which is good because I’m about to rewind the close. On the master branch on my fork, I’m going to reset back to where I deviated from the upstream original. In this case, the commit before I started messing stuff up in my copy is 4d0d182.

# go back to my master
git checkout master

# rewind my master to where 
# I should have started
git reset 4d0d182 --hard

# force this version of master
# into my fork on github
git push -f

My fork is now at the place it was when I took it from the original. It’s just behind:

So, now we need to get it up to date.

The repository’s original/upstream is JUnit’s own repo. The address of that can be found from the clone button:

What we need to do is set an upstream within my local copy of the repo, which we can use to update my master.

git remote add upstream

With this upstream in place, I should be able to update my master.

# on the master branch
git checkout master

# with the latest changes from the upstream 
# "known" to your local git
git fetch upstream

# then rebase your master against the upstream
git rebase upstream/master

# and then push those changes up to github
git push

This is like sorcery – look at my repo now:

That’s massively up to date (it was commits from 2016 before this).

All that’s left is for me to get my changes onto a new branch. I have a few options. I could try rebasing the branch I made at the start of this article against my new master. If things have gotten messy, it might just be easier to rescue commit d92e034 like this:

# create a new branch
git checkout -b a_new_attempt_at_my_branch

# pull my special commit from its 
# hiding place into this branch
git cherry-pick d92e034

# now I can keep developing, 
# push this branch, make a PR etc etc.

There are, of course, myriad other ways of doing this. This one works well!

One comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s