How to Undo Changes in Git

John Wachira Feb 02, 2024
  1. Use the git log Command to Check History of Commits in Git
  2. Use the git clean Command to Undo Changes in Git
  3. Use the git revert Command to Undo Changes in Git
  4. Use the git reset Command to Undo Changes in Git
How to Undo Changes in Git

This tutorial covers the various commands to undo changes to your local and remote repositories. These commands include:

  1. git clean
  2. git reset
  3. git revert

Use the git log Command to Check History of Commits in Git

Let us first see how we check the history of our commits. We check this using the git log command.

pc@JOHN MINGW64 ~/Git (main)
$ git log
commit 195e5c362975354d62ebc469da2e3cd276c7da03 (HEAD -> main)
Author: John <wachirajohnie11@gmail.com>
Date:   Mon Feb 21 12:11:12 2022 +0300
commit 7b19db4b35c1ca15e5ecb8df1f805d44aad62e5f
Author: John <wachirajohnie11@gmail.com>
Date:   Mon Feb 21 10:09:31 2022 +0300
   first commit

The command only displays the history of your working branch. To check for all branches, use the command below.

git log --branches=*

Use the git checkout command to view a specific commit, as shown below.

git checkout

Now you can access files without tampering with the current state of your project. Use the git checkout main command to return to the current project workspace.

Use the git clean Command to Undo Changes in Git

We use the git clean command to undo changes on untracked files. You cannot undo a git clean command.

  1. The git clean -n option acts as a tryout command. It only displays the untracked files but does not delete them.

    Example:

    pc@JOHN MINGW64 ~/Git (main)
    $ git clean -n
    Would remove Hello world.txt
    Would remove Test1.txt
    
  2. The git clean --force command deletes all untracked files and folders in your working directory. You can use the .gitignore command to throw exceptions.

    Example:

    pc@JOHN MINGW64 ~/Git (main)
    $ git clean --force
    Removing Hello world.txt
    Removing Test1.txt
    

    Let’s check if the files are in our directory.

    pc@JOHN MINGW64 ~/Git (main)
    $ git status
    On branch main
    nothing to commit, working tree clean
    

    The output shows a clean branch. The command deleted the files successfully.

Use the git revert Command to Undo Changes in Git

The git revert command acts on the changes made by a commit and overwrites it. We use it to go back to a point and make changes.

Let’s use the command to change our first commit.

git revert first commit
#[first commit [7b19db4] Revert 'first commit'
#1 file changed, 1 deletion(-)

You have to pass a commit reference.

Use the git reset Command to Undo Changes in Git

The last command we will cover is the git reset. This command operates on the HEAD.

  1. The git reset --hard option changes the specified commit. However, any changes in the staging index and your working directory match the changes. All staged commits are lost in the process.

    Example:

    $ git reset --hard
    HEAD is now at 78129a6 Revert "$first commit"
    

    The command executes a hard reset to the HEAD.

  2. The git reset --mixed command saves the changes from the staging index to your working directory.

    Example:

    $ git reset --mixed
    Unstaged changes after reset:
    M       .bash_history
    M       text.txt.txt
    M       text.txt.txt.bak
    

    The command has saved the undone changes to our working directory from the output. Let’s check the status of our branch.

    $ git status
    On branch main
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
    		modified:   .bash_history
    		modified:   text.txt.txt
    		modified:   text.txt.txt.bak
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    		.bash_history.bak
    no changes added to commit (use "git add" and/or "git commit -a")
    
  3. The git reset --soft command only changes the commit history. The default pointer is always the HEAD.

    Example:

    pc@JOHN MINGW64 ~/Git (main)
    $ git reset --soft
    pc@JOHN MINGW64 ~/Git (main)
    $ git status
    On branch main
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
    		modified:   .bash_history
    		modified:   text.txt.txt
    		modified:   text.txt.txt.bak
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    		.bash_history.bak
    no changes added to commit (use "git add" and/or "git commit -a")
    

    From the output above, our working directory and staging index are untouched. Use the hash of your commit to reset a specific commit.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Reset