Git - Undoing Changes


Git provides various ways to undo changes based on whether they are unstaged, staged, committed, or pushed to a remote repository.

Undoing a Commit With --amend

When you forget to include certain changes or files, you can add them with the --amend option. This command allows you to add staged changes to the previous commit.

To undo a commit with the --amend option, run the following commands:

  • You make some changes and commit them.

    git commit -m "First Commit"  
    

    After committing, you notice that you forgot to stage some changes in a file before committing.

  • You can add the additional changes and stage that file using the following command:

    git add main.html
    
  • You can use the --amend option with the git commit command to include the staged changes in the previous commit. This command adds the staged changes to the most recent commit.

    git commit --amend   
    

Unstaging a Staged File

If you accidentally staged two files using git add * but you want to commit them separately, you can unstage one file while leaving the other staged.

For unstaging a staged file, use the following commands:

  • If you accidentally staged both files using the git add * command. This command stages all changes in the current directory.

    git add *  
    

    You can use git status command to get the current status of the repository including staged and unstaged changes.

    git status  
    
  • When you run the following command, it will remove the file from the staging area and revert any changes made to it.

    git reset HEAD <file_name>
    
  • Use the git status command to check the repository's status again to ensure that the desired modifications are done.

git add *

git status
The command git reset is usually safe because it does not modify the files in your working directory. But when using --hard, it can remove changes entirely.

Unmodifying a Modified File

You can unmodify a modified file, using the following commands:

  • To check the status of the current repository and see which files have uncommitted changes.

    git status
    
  • To restore the main.html file to its original state.

    git checkout -- main.html
    

    This command will remove any modifications made to the main.html file and restore it to the state of the last commit.

  • You can verify that the changes have been restored by running git status command again.

git status
It is unsafe to use git checkout -- <file> since it instantly replaces any local changes with the most recent staged or committed version of the file. Make sure you want to remove your current changes before using this command.

Undoing Changes - git restore

Git version 2.23.0 introduced a new command, git restore that can be used instead of git reset to undo the changes.

Unstaging a Staged File With git restore

You can unstage a staged file with the git restore command, using the following commands:

  • If you staged two files using the command git add *. This command stages all changes in the current directory.

    git add *  
    

    You can use git status command to get the current status of the repository including staged and unstaged changes.

    git status  
    
  • You can use git restore instead of git reset to remove the file from the staging area and revert any changes made to it.

    git restore --staged <file_name>
    
  • Use the git status command to confirm that the desired changes are done.

git add *

git status

Unmodifying a Modified File With git restore

You can unmodify a modified file with git restore, using following commands:

  • To check the status of the current repository and see which files have uncommitted changes.

    git status
    
  • To restore the main.html file to its original state.

    git restore main.html
    

    This command will remove any modifications made to the main.html file and restore it to the state of the last commit.

  • You can confirm that the changes to main.html have been reverted by running the git status command.

git status
It is unsafe to use git restore <file> since it instantly replaces any local changes with the most recent staged or committed version of the file. Make sure you want to remove your current changes before using this command.
Advertisements