How to Delete a Committed Branch


Hey kids! I have a confession to make - it never fails, whenever I push a branch and regret it (meaning I'd just like a do-over and to pretend it never happened) ... I absolutely forget the correct process!! Part of that reason is because I do it so rarely, which is probably good :)




Do I squash? No!! That merges all commits into a single commit.


Do I revert? No!! This creates a second commit that reverts the offending commit.


Do I Cherry pick? No!! That allows you to pick some changes and pull it back.


So what do I do???



gif


We need to reset.


The magic word is reset!!

So, if using Visual Studio, we need to open the history tab for the repository. Where is that? Well, we first go to our Git Changes tab



Then, we click the 3 dots (ellipses) on the top right corner. You'll get this menu that has 'Manage Branches' in it. Click the manage branches and the 'history' will open in the working window of VS.



You will see Incoming, Outgoing, and Local History. Our focus is on the Outgoing area. You should see the commit you want to 'pretend didn't happen' in this section. In my case, the commit I want to remove is 'test' in the Outgoing section below.





Once you find the offending commit (it could be in the Outgoing or the Local History, depending on if you did a Commit or a Commit and Push), you will want to right click the last change before that commit. In the screenshot above, that will be the 'Merged PR' line.




You can choose to do a Hard delete or a soft delete. The hard delete will remove the changes in your commit completely, as if you never wrote the code change. The soft delete will then restage the changes and you still have them.



Then make the changes you wanted to make before you committed the offending code, rework it, and push it!!



gif



Andddddddd we’re done! Kisses!! 😘


gif

2 views0 comments