Undo the last local commit in Git

The git reset command is used to undo the last local commit in Git.The git reset will undo the recently added commit to the previous commit stage in local. There are three options for the git reset command, default, soft and hard reset. The soft reset will reverse the recent commit without removing any recent changes. The hard reset will reverse the recent commit and changes. The default reset will reverse the recent commit without removing any recent changes and removes from git staging area.

In some cases, you have committed some files that you don’t want to push to your Git repository. You may want to make additional changes before you commit. If you make additional changes without deleting the previous commit, both changes will be pushed to your git repository. The recent local commit should be removed before the additional change is made.

The undo of the last local git commit is done in two ways. If you don’t want to see the recent changes again, lose the recent changes, want to start from the previous commit, use the hard-reset option. The hard option will be reset to the previous commit and all recent changes will be lost. If you want to reset to the previous commit and do not want to lose the recent changes, the soft option will be used. You can continue with the changes you made in the local area before.



Stage 1 – Initial Stage

There is no file available in the current directory at the initial stage. The status of the git will show the current status. The git log command shows that 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

To reset the current commit, we need to create a commit first. A new file is added to the current directory at this stage. The new file will be committed to create a recent local commit. The text file test1.txt will be created in the example below. After the file is created, the git status and git log command 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

In this stage, the file is added in git staging area. Git add command add the file in staging area and indexed.

$ 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

A new commit is being created at this stage. The file is added to git using the git add command. The git commit command creates a new commit in local. The git status and git log command reflect the new commit 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


Solution 1

The easiest way to undo the last commit is to use the git reset command with the soft reset option. The soft reset option will reset the last recent commit without removing any recent changes. The commit will be deleted. The changes made at the local level will remain in the current directory. The HEAD~1 shows that reverting to one previous commit in the HEAD.

$ git reset --soft HEAD~1

Reset with the soft reset option will reverse the changes in stage 4 and reset to stage 3. The git status and git log command will be shown 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 recent commit and remove the file from the staging area. The git reset command will reverse the git commit and git add command. The commit will be deleted. The changes made at the local level will remain in the current directory. The HEAD~1 shows that reverting to one previous commit in the HEAD.

$ git reset HEAD~1

Reset with the soft reset option will reverse the changes in stage 4, 3 and reset to stage 2. The git status and git log command will be shown 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 git reset with the hard reset option removes the recent commit and moves to the previous commit. Recent changes will be deleted and the files will be stored as in the previous commit. The local changes in the hard reset will be deleted. The HEAD~1 shows that reverting to one previous commit in the HEAD.

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

Reset with the soft reset option will reverse the changes in stage 4, 3, 2, and reset to stage 1. The git status and git log command will be shown 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



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *