The best way to undo the last local commit in Git is to use git reset command with options like —soft, —hard, which will undo the most recent git local commit with or without keeping the local file modifications. The git reset command restores local to the previous commit point, reversing the recently added commit. The git reset command remove the last commit and undo local commit changes.

There are two ways to reverse the last local git commit. Use the hard-reset option if you don’t want to see the recent changes again, want to lose the recent changes, or want to resume from the previous commit. All recent changes will be removed if you use the hard reset option. The soft option will be used if you want to go back to a previous commit without losing your latest changes. You’ll keep making updates to the code you’ve been working on in the local git repository.

You may have committed some files to your Git repository that you do not want to push. Before you commit, you might want to make a few more modifications. If you make new changes without removing the previous commit, both updates will be pushed to the git repository. The latest local commit should be deleted before making the additional modification.



Stage 1 – Initial Stage

There are no files available in the current directory at this stage. The current git status will be shown in the git status command. As per the git log command, there is only one commit.

$ls
$
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
$ git log --oneline        
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit


Stage 2 – Adding a file

We must first create a commit in order to reset the recent local commit. At this stage, a new file is added to the current directory. To create a recent local commit, the new file will be committed. In the example below, the text file test1.txt will be created. After the file is created, the git status and git log commands will be shown as below.

$ cat>>test1.txt
test file
^C

$ ls
test1.txt
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test1.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git log --oneline        
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit


Stage 3 – add to git

The file is now added to the git staging area. The git add command indexes and adds the file to the staging area.

$ git add test1.txt
$ git status             
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   test1.txt
$ git log --oneline      
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit


Stage 4 – Commit the file

At this stage, a new commit is being created. The git add command is used to add the file to git. The git commit command adds a new commit to the local repository. The new commit is reflected in the git status and git log commands, as shown below.

$ git commit -m "adding test1.txt file"
[master ad88c9f] adding test1.txt file
 1 file changed, 1 insertion(+)
 create mode 100644 test1.txt
$ git status       
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git log --oneline                    
ad88c9f (HEAD -> master) adding test1.txt file
055feb2 (origin/master, origin/HEAD) Initial commit


Git Reset Types

The git reset command has three options: default, soft, and hard reset. The soft reset will erase the most recent commit, but the changes will remain in the local repository. The hard reset will erase all recent changes and revert the commit. The default reset would undo the most recent commit without removing any modifications, and it will also delete the file from the git staging area.



Solution 1

The easiest way to undo the recent commit is to use the git reset command with the soft reset option. The soft reset option will remove the last recent commit without removing any recent changes. The commit will be removed from the local git repository. The changes you made locally will stay in the current local git repository. The HEAD~1 shows that the HEAD is reverting to a previous commit.

$ git reset --soft HEAD~1

The soft reset option will undo the changes made in stage 4 and return you to stage 3. The git status and git log command will show the output as shown in stage 3.

$ git status             
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   test1.txt
$ git log --oneline      
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit


Solution 2

The git reset without any option will remove the most recent commit as well as the file from the staging area. The git commit and git add commands will be reversed if you use the git reset command. The commit will be removed from the local repository The changes you made locally will stay in the current directory. The HEAD1 shows that the HEAD is reverting to a previous commit.

$ git reset HEAD~1

The soft reset option will undo the changes made in stage 4, 3 and return you to stage 2. The git status and git log command will show the output as shown in stage 2.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	test1.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git log --oneline        
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit


Solution 3

The hard reset option in git reset removes the most recent commit and returns to the previous commit. Recent changes will be removed, and the files will be stored in the same way they were before the previous commit. The hard reset’s local changes will be wiped away. The HEAD1 shows that the HEAD is reverting to a previous commit.

$ git reset --hard HEAD~1              
HEAD is now at 055feb2 Initial commit

The soft reset option will undo the changes made in stage 4, 3, 2 and return you to stage 1. The git status and git log command will show the output as shown in stage 1.

$ git status                           
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
$ git log --oneline                    
055feb2 (HEAD -> master, origin/master, origin/HEAD) Initial commit



Leave a Reply