Resetting a Git Repository Branch

Resetting a Git Repository Branch

Introduction

I have a problem. Well, I have several, but this one is that I keep pushing changes into my main branches instead of creating a feature branch. I don’t usually do this, I am fairly disciplined, but whenever I am on calls with people and I am distracted, I frequently make a tiny change on the screen to show people what needs to be done and then later i check it in on that branch.

This leaves my main branch with changes that I don’t want.

The Answer

To fix the situation, assuming I don’t want the code I checked in, I need to reset the main branch back to that on the server. This is a simple git command: –

 git reset --hard origin/main

‘main’ should be replaced with whatever root, develop, main branchin name you use.

This will lose the changes in the checkins you did – so, if you need them, copy those changes out to another branch before you do this.

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.