If you made a commit to your git local repository and you want to undo it, you should make use of the git reset command.
There are three flags or options you may use depending upon what your use case is.We have done two commits:
$ touch File1.txt File2.txt $ git add File1.txt $ git commit -m "Commit 1" $ git add File2.txt $ git commit -m "Commit 2" $ git log --oneline 341ab47 (HEAD -> main) Commit 2 3291fc9 Commit 1
1. Undo the Last Commit completely with a hard reset
If you do not want the files that were committed in the last commit to your local repository make use of the --hard option.
$ git reset --hard HEAD^1 HEAD is now at 3291fc9 Commit 1
Note this case you will lose the files that were committed as the modifications you made to the existing file.
$ ls File1.txt $ git status On branch main nothing to commit, working tree clean
2. Undo Last Commit with a soft reset
If you want to undo your last commit, but want to retain the files that were committed in the last commit make use of the --soft flag.
$ git reset --soft HEAD^1 $ ls File1.txt File2.txt $ git status On branch main Changes to be committed: (use "git restore --staged
..." to unstage) new file: File2.txt
Note that changes you made or the new files added will remain in the staged area.
3. Undo Last Commit with a mixed reset
If you want to undo your last commit, but want to retain the files that were committed in the last commit in the working area make use of the --mixed flag.
$ git reset --mixed HEAD^1 $ ls File1.txt File2.txt $ git status On branch main Untracked files: (use "git add
..." to include in what will be committed) File2.txt nothing was added to commit but untracked files are present (use "git add" to track)
As you can see the new file that was committed is now moved to the working area and is untracked.
Facing issues? Have Questions? Post them here! I am happy to answer!
- Fix [oh-my-zsh] Cant update: not a git repository
- [fix] fatal: pathspec index.html did not match any files error
- How to ignore files in git using .gitignore file
- Git Fix: fatal: refusing to merge unrelated histories Error
- Install GitHub Command Line Tool on Mac
- How to change directory in Git bash
- Get List of all local branches git command
- fix fatal: --local can only be used inside a git repository error
- [git] fatal: your current branch 'main' does not have any commits yet
- Git Revision Questions Before the Interview
- Change the default git branch name from master to main
- git command to remove/unstage files from staging area
- Remove git config at Local, Global or System Levels?
- [fix] fatal: this operation must be run in a work tree in git
- Clone a particular remote brach using git clone command
- How do I get a list of all branches in Git?
- How to check your installed version of Git
- Step-by-Step: How to delete a git branch from local as well as remote origin
- Git Commit - Author identity unknown, Please tell me who you are email
- fatal: Unable to create '/c/git_repo/.git/index.lock': File exists. If no other git process is currently running, this probably means a git process crashed in this repository earlier.
- How to Rename a Git Local Branch Using the --move Option
- Perform an Empty Commit in Git without anything in Staging Area
- [Fixed] Git Clone firstname.lastname@example.org: Permission denied (publickey) Could not read from remote repository Fatal error
- GitHub: How to Search Code
- Rename git branch on Local and GitHub Remove using Command
- Change Terminal Cursor Type in Mac (MacOS Shell) - MacOS
- Maven Central Repository: URL and Details - Java
- Android: Maps and Places using Maps SDK - Android
- Android: programmatically turn Bluetooth on or off using Java code - Android
- Trigger Flow on selected Listitem from SharePoint view - create button with JSON column formatting - SharePoint
- Bash command to wait for seconds - Bash
- JBoss stuck loading JBAS015899: AS 7.1.1.Final Brontes starting - Java
- Change CSS Background Opacity with Examples - CSS