Make Selenium Easy

Git Tutorial 28 – How to undo all local commits in a branch to reset to remote branch

Introduction

As a part of GIT Tutorials End To End, we will learn to revert all local commits from a branch to match the content as a remote branch.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

Revert all commits

We clone an existing repo to add files or make changes. These changes are committed before pushing. Suppose you have made all required changes and committed but have yet to push to the remote branch. Now you get to know that those changes are not required anymore.

To start on another feature you need to discard all changes you made so far after cloning the repo. In short, your local branch is needed to be synced with the remote branch i.e. like a freshly cloned repo. There are multiple ways you can achieve it –

  1. You can clone the remote repository again.
  2. You can create a new local branch from a remote branch.
  3. You can revert all commits.

In this post, we will learn about option 3rd. We will revert all commits using the git reset command.

We will clone a public git repo from here.

Let’s add some files and do multiple commits.

Above you can see that I have created two commits and have not pushed them. It makes my branch ahead of origin/main by 2 commits.

Now you just want to revert all these two commits as you don’t require it anymore. Instead of recloning, we can use the git reset command. Please understand git stash will not work here.

To revert all commit i.e. resetting your branch status at a HEAD commit of a remote branch, use the below command –

git reset –hard origin/main

We are passing remote branch name i.e. origin/main to instruct git to reset the local branch to the head of origin/main. We will explore the option “–hard” later.

Observe the log of command above. It says “HEAD is now at 235839a added FileC“. This command removed both new commits and reset the head of the local branch to the head of origin/main. It is now the same as a newly cloned repo.

You can run the git log command to see that the latest two commits were removed.

Please note above command will not remove the untracked file. That is obvious as git does not bother about files not tracked by it. If you want to remove untracked files you need to use the git clean command.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

Git Tutorial 28 – How to undo all local commits in a branch to reset to remote branch

by Amod Mahajan time to read: 2 min
0
%d bloggers like this: