Monthly Archives: April 2023

Git is awesome

I just recently fat-fingered a branch-deletion of a remote branch. But luckily git has you covered should you do that. Let me tell you the story…

I don’t know why on a lot of keyboards the letters D and F are right next to each other (well, I know but that’s a different story). So far that never was an issue.

But! If you start sloppy typing and while hitting the F key you also hit the D that usually just means you will have to use the backspace and delete a character you didn’t want.

Unless you are on the CLI and hit ENTER immedately after …

What happened:

I was working on a branch and commited some stuff to it. As I already had a PR open for it on github I pushed the change.

Of course the CI found a minor thing in the code. I was casting something that was unnecessary. So I removed that cast in the code and commited that as well to my local branch. As it was a really minor change that I should have done with the previous commit already I decided to do a git commit --amend --no-edit . Just add that to the previous commit and be done.

That now replaced the last commit with a new one and I had to force-push that to the remote branch.

And now I fat fingered.

Instead of git push origin branchname -f I typed git push origin branchname -df

And the -d means: Delete that branch on the remote server.

I mean it’s not that much of a loss. I could have just used git push origin branchname again and be done.

But with deleting the remote branch I also closed the PR. And just pushing the branch again would require me to create a new branch instead of being able to reopen the old one. Why? because the old PR was associated with the old commit-hash. But I now had a new commit-hash.

So how could I fix that?

git reflog to the rescue! While still on the local branch I issued this:

git reflog
4fcb8ff4d (HEAD -> branchName, origin/branchName) HEAD@{0}: commit (amend): Commit Message
0cc696b53 HEAD@{1}: commit: Commit Message
458011b1b HEAD@{2}: rebase (continue) (finish): returning to refs/heads/branchName

So the great thing here is that we not only have the log of our last commits (which would only show one entry for the Commit Message` commit. But we have both commit hashes. How cool is that!

That allowed me now to do git push origin 0cc696b53:branchName which pushed the commit 0cc696b53 to the server and named the resulting branch branchName . That caused GitHub to realize that the branch is still existing and allowed me to reopen the PullRequest.

So now we are almost in the same situation as before my fat fingered stupidity.

The only thing left to do now is to actually push (and not delete) the branch to the server.

And a git push origin branchName -f (no -d) later the branch is updated and the PullRequest knows about the update and CI is up and running.

Thank you git for saving my back!