Mastering Git: How to Undo git add and Revert Commits with in very easy steps

rajneesh

rajneesh

5 min read

  • git
git undo and revert commit

Are you looking for a method by which you can undo git add or roll back any commit completely.

After reading this blog post you can easily undo git add and roll back commit.

Git is a very essential tool for developers as it provides a super power to manage code, version control very efficiently. even seasoned or professional developers sometimes they also mistakes, such as accidentally adding files to the staging area or committing changes prematurely. Fortunately, Git provides simple commands to fix that mistakes. in this blog post i will explain you how you can undo add or revert the commit with simples practical examples.

Understanding Git Staging Area

git staging area is one of the important part between your working directory and the repository. It allows you to prepare changes before committing them. When can use git add, to move changes from your working directory to the staging area. to stage all or single file with changes we can use this commands:

git add . // for all file 
git add <file_name> // for spacific file

by this we an easily organizing and reviewing changes, but sometimes, files might be added by mistake.

Using git reset to Undo git add

What is git reset?

Git reset command is used to undo the changes made in your working directory and go back to a specific commit while discarding all the commits made after that using hard option and we can also use reset soft option by which commit is done without discarding. Or you can undo files from staging area to working directory.

Step-by-Step Guide

  1. Check the status: check the staging area Use the command:

    git status

    Example output:

    On branch main
    Your branch is up to date with 'origin/main'.
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   index.html
            new file:   script.js
            new file:   style.css

  2. Undo the add: To unstage the specific file, use:

    git reset HEAD index.html // replace your file name that one you want to unstage with index.html file

    Example output:

    Unstaged changes after reset:
    M       index.html

  3. Undo all adds: If you want to unstage all files, simply run:

    git reset

    Example output:

    Unstaged changes after reset:
    M       index.html

Verify changes: Finally, check the status again to confirm the files are no longer staged:

git status

Example output:

On branch main
Your branch is up to date with 'origin/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:   index.html

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        script.js
        style.css

no changes added to commit (use "git add" and/or "git commit -a")

Alternative Method: git restore --staged

What is git restore?

Introduced in Git 2.23, git restore provides more clear and easy syntax as comparison to git reset command. it make more easier for new developers.

Step-by-Step Guide

  1. Check the status: Begin by seeing which files are staged:

    git status

    Example output:

    On branch main
    Your branch is up to date with 'origin/main'.
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            modified:   index.html
            new file:   script.js
            new file:   style.css

  2. Undo the add: To unstage a specific file, use:

    git restore --staged index.html // replace your file name that one you want to unstage with index.html file

  3. Undo all adds: To unstage all files, run:

    git restore --staged .
  4. Verify changes: confirm the changes by checking the status again:

git status

Example output:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   script.js
        new file:   style.css

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:   index.html

Undoing Changes After Commit

Sometimes, you may realize that you have mistakenly commit the wrong code or file. git provide a command by which you can undo that commits.

Using git reset to Undo a Commit

git reset can also undo commits 😎.

  • Check the commit history: Use git log to see recent commits.

    git log --oneline

    Example output:

    4d7d122 (HEAD -> main, origin/main) add:hello coders
    3ec037c upt:hello world
    da56090 add:hello

  • Undo the last commit: If you want to undo the most recent commit and unstage the changes, use:

    git reset --soft HEAD~1
    //or you can use by commit id
    git reset --soft da56090 //this one is recommanded

    Example output:

    Unstaged changes after reset:
    M   index.html
  • Undo the last commit and discard changes: If you want to undo the commit and discard the changes completely, use:

    git reset --hard HEAD~1
    //or you can use by commit id
    git reset --hard da56090 //this one is recommanded

    Example output:

    HEAD is now at da56090 add:hello

Verify changes: confirm the changes by checking by the git log --oneline again:

git log --oneline

Example output:

da56090 (HEAD -> main) add:hello

Using git revert to Undo a Commit

f you have already pushed the commit to a remote repository, it's better to use git revert to maintain a clear commit history.

  1. Revert the last commit: Create a new commit that undoes the changes made by the previous commit.

    git revert HEAD

    Example output:

    [main d9e3f6g] Revert "Added example.txt and test.py"
     2 files changed, 0 insertions(+), 0 deletions(-)
     delete mode 100644 example.txt
     delete mode 100644 test.py
    

Revert a specific commit: You can also revert a specific commit by referencing its hash.

git revert 1a2b3c4

[main h1i2j3k] Revert "Added example.txt and test.py"
 2 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 example.txt
 delete mode 100644 test.py

Common Mistakes and Troubleshooting

While using git reset or git restore, you might encounter some common issues:

  • Files not unstaged: Double-check the file names and paths. They should exactly match those listed by git status.

  • Unintended changes in the working directory: Use the --soft flag with git reset to avoid modifying the working directory.

Conclusion

in this blog post, i have covered both the commands to undo add for staging area and revert the commit. it is most common problem while working with git if you are pro or beginner, this commands will help. while reverting the old commit with hard option please make sure your action because it discarding all the commits made after that. if you think this post is helpful for you. you can share this post with your fellow developers.

rajneesh

rajneesh

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.