Undoing Changes in Git: A Guide to Reversing Mistakes

Learn how to easily undo changes in Git and revert your local repository to a previous state. This guide will cover various methods, including using git checkout and git reset, to help you recover from errors.


Git - Fix Mistakes

Making mistakes is part of being human, and every version control system (VCS) offers features to help you correct errors within your local repository. In Git, you can easily undo modifications, allowing you to revert your local changes effectively.

Reverting Uncommitted Changes

Imagine tom accidentally modifies a file in his local repository and wishes to revert those changes. The git checkout command can be utilized to restore the original content of the file.

Check Current Directory

[tom@CentOS src]$ pwd
/home/tom/tom_repo/project/src
        
Check File Status

[tom@CentOS src]$ git status -s
M string_operations.c
        
Revert Changes

[tom@CentOS src]$ git checkout string_operations.c
        
Check Status Again

[tom@CentOS src]$ git status -s
        

Recovering Deleted Files

In another scenario, if david accidentally deletes a file from his local repository and wants to restore it, the git checkout command can help recover the deleted file as well.

Check Current Directory

[david@CentOS src]$ pwd
/home/david/top_repo/project/src
        
List Files

[david@CentOS src]$ ls -1
Makefile
string_operations.c
        
Delete a File

[david@CentOS src]$ rm string_operations.c
        
List Files Again

[david@CentOS src]$ ls -1
Makefile
        
Check File Status

[david@CentOS src]$ git status -s
D string_operations.c
        
Recover Deleted File

[david@CentOS src]$ git checkout string_operations.c
        
List Files to Confirm Recovery

[david@CentOS src]$ ls -1
Makefile
string_operations.c
        
Check File Status After Recovery

[david@CentOS src]$ git status -s
        

Removing Changes from the Staging Area

When files are added to the staging area using the git add command, they can be reverted using git checkout to remove accidental modifications.

Git maintains a HEAD pointer that always points to the latest commit. To undo changes from the staging area, you can use the git checkout command with the HEAD pointer as an additional parameter.

Check Current Directory

[david@CentOS src]$ pwd
/home/david/top_repo/project/src
        
Check Status of Unmodified File

[david@CentOS src]$ git status -s
        
Modify a File and Check Status

[david@CentOS src]$ git status -s
M string_operations.c
        
Add Modified File to Staging Area

[david@CentOS src]$ git add string_operations.c
        
Check Status After Adding to Staging Area

[david@CentOS src]$ git status -s
# Changes to be committed:
# (use "git reset HEAD ..." to unstage)
#
modified: string_operations.c
        
Revert Staged Changes

[david@CentOS src]$ git checkout HEAD -- string_operations.c
        
Check Status After Reverting

[david@CentOS src]$ git status -s 

Conclusion

With Git, you have powerful tools at your disposal to fix mistakes, whether they involve reverting uncommitted changes, recovering deleted files, or removing changes from the staging area. Understanding these commands enhances your ability to manage your code effectively.