How to Undo Changes in Git
-
Use the
git log
Command to Check History of Commits in Git -
Use the
git clean
Command to Undo Changes in Git -
Use the
git revert
Command to Undo Changes in Git -
Use the
git reset
Command to Undo Changes in Git
This tutorial covers the various commands to undo changes to your local and remote repositories. These commands include:
git clean
git reset
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.
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.
-
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
-
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
.
-
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
. -
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")
-
The
git reset --soft
command only changes the commit history. The default pointer is always theHEAD
.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.
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.
LinkedInRelated Article - Git Reset
- Difference Between the Git Reset, Revert, and Checkout Commands
- How to Make the Development Branch Identical to the Master Branch
- How to Remove Local Git Changes
- How to Revert a Git Merge With Conflicts
- Difference Between Git RM --Cached and Git Reset File
- How to Revert a Git Repository by Commit ID