Chronicles Of A Developer | Removing A Commit From Master

Rehtse Studio
4 min readOct 28, 2022

How bug(💩)s / human oops help us to grow as Developers!

Backstory

The lead developer of your group gives you a new task, you tackle the job in record time. You push your changes and you are pleased with yourself, later to notice you push to the “master” branch instead of your working branch.

“What do I do?”, you say to yourself.

What Can You Do?

Regardless if you’re using GitHub or Bitbucket, removing a commit from this service is not straightforward. Nothing in Bitbucket or GitHub will allow us to remove a commit. But by using our local repo we have a chance.

We are going to use GitHub Desktop as our middleman and GIT for this fix. we don't necessarily need GitHub Desktop but the UI is very easy to get the necessary information that we need.

Scenario

In this Unity project, there are two branches: One is the master (or main, depending on your repo’s settings) branch that has the stable product/game, and the second branch is called power_up_implementation.

GitHub Desktop
GitHub

On Unity, I created a new folder called Scripts and a new script called PowerUp; these newly created files were supposed to be made and pushed to the power_up_implementation branch. But by mistake, I commit and push to master

Files were committed and pushed to master
Files were committed and pushed to master

Safe Our Work

Even if we have committed a sinful act to push to master we need to acquire our implementation and add it to where is supposed to be. We go to the power_up_implementation branch, this is where GitHub Desktop comes in handy, got to Branch, and select Update from master.

And just like that, we have saved our work, now let's fix our oops 💩

Getting The commit ID

Before pushing to any branch you need to do a commit, those commits have an ID that we can use in this situation or others that we may encounter in the future.

We need to get the commit ID of the commit labeled as “Initial commit from PC”, that's when our master was before adding the power-up implementation.

There are different ways we can do this, using GitHub Desktop we can view our commit ID to the top right, if we use GIT and we are in the master branch we simply type git log and it will give us all the commits and their ID.

Copy that ID.

Using the repos, Bitbucket has a clear way to get the ID, with GitHub I have no clue what I am seeing🤣.

Fixing Our Oops

To remove the unnecessary commit from the master branch in the repo, we will use just GIT for the following steps.

On GIT, in the master branch, we are going to type git reset — hard #commit ID#

Once we run the command, GIT will tell us where we are on the branch, if we go back using GitHub Desktop you're going to see that our mistake commit is gone. Using the above command our branch is how it is supposed to be on that commit and all other changes that we have done forward have been erased/removed.

Once again, using GIT, we type git push — force origin master, this command will force what we have on our local repo to the repo on GitHub / Bitbucket.

And just like that, we have removed our mistake from the master 😊.

Please be careful when using this type of command since any work you do not save or put on another branch will be gone.

🖥TO BE CONTINUED🖥

--

--

Rehtse Studio

Game Developer sharing content around Unity, programing and tips and tricks in game dev.