Home Git Reset: Reset Your Changes Back to the Original State
Post
Cancel

Git Reset: Reset Your Changes Back to the Original State

Git Reset: Reset Your Changes Back to the Original State

Version control systems come in handy when you make mistakes or want to start over. One of their many features is the ability to reset changes. With the reset, you can reset your branch back to its original state with a commit SHA id or to another branch.

Git has three reset options; soft reset, mixed reset, and hard reset.

Before We Start

Before we start, let’s clone a repository, check out a new branch, and commit some changes. So, we can reset them with all three options and see the difference between them.

I will be using my GitRevertReset repository for this post. It has a sample DotNet 6 Web API application created by the command below.

1
dotnet new webapi -o GitRevertReset

If you follow along, below are the command to run to prepare the repo.

1
2
3
git clone https://github.com/musaugurlu/GitResetRevert.git
cd GitResetRevert
git checkout -b feature/newApi

Then, make some changes to the project. I have added a new API to the controller. If you follow along, add the below code to the WeatherForecastController.cs file under the Controllers folder.

1
2
3
4
5
6
7
8
9
10
[HttpPost(Name = "PostWeatherForecast")]
public IActionResult Post()
{
    return Ok(new WeatherForecast
    {
        Date = DateTime.UtcNow,
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    });
}

Then, commit your changes.

1
git commit -a -m "Added Post API"

if you run git status on your terminal, you will end up with the screen below.

status Git Repository

Soft Reset

For Git to track your changes, you need to add files to Git’s track with the command git add <file name> (or git add -A for all the files). When you commit your changes, Git adds the changes in the tracked files into the local repository, and then you synch the local repository with the remote one with the git push command.

Soft reset resets your branch to a given branch or a commit SHA id and keeps the files tracked. So that you can update your changes, add only the updated files to git track, and commit again.

The command:

1
git reset --soft <branch name or Commit short/long SHA id>

Note: If you reset your branch to a remote branch, add origin/ at the beginning of your branch so that Git can know it is at origin (the remote location).

Git Reset Soft Reset

GitLog Before Reset Git Log Before Soft Reset

Git Log After Reset Git Log After Soft Reset

Mixed Reset

A mixed reset does almost the same effect as a soft reset with only one difference: It removes the file from git tracking. If you want to commit your files again, whether or not you updated them, you have to add them to git tracking again with the command git add

The command:

1
git reset --mixed <branch name or Commit short/long SHA id>

Note: If you reset your branch to a remote branch, add origin/ at the beginning of your branch so that Git can know it is at origin (the remote location).

Mixed Reset Mixed Reset

Git Log After Mixed Reset Git Log After Mixed Reset

Hard Reset

Unlike the mixed and soft reset, Hard reset resets the branch to the given branch or commits SHA id and removes the files that do not exist in the target branch.

The command:

1
git reset --hard <branch name or Commit short/long SHA id>

Note: If you reset your branch to a remote branch, add origin/ at the beginning of your branch so that Git can know it is at origin (the remote location).

Hard Reset Hard Reset

Git Log After Hard Reset Git Log After Hard Reset

This post is licensed under CC BY 4.0 by the author.

Working with Multiple Subscriptions, AKS Clusters, and K8s Namespaces in Azure CLI

Git Revert: Life Saver of the False Committers