
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
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.