Git remove uncommitted changes
This article will guide you on how to undo the uncommitted changes we made to our local repository.
When working on a feature, we might start by creating new files, adding changes to existing files, and then deleting some files. Eventually, we realize that this all went wrong and need to go back to an earlier commit. What should we do?
$ echo 'Add new implementation' > feature.txt
$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ rm deprecated_feature.txt
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: deprecated_feature.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
We have several ways to accomplish this.
git checkout
Remove uncommitted changes in Git
using
This command will revert uncommitted changes to tracked files. Tracked files are files that git knows about, usually after they have been git add
added
$ git checkout .
Updated 2 paths from the index
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
nothing added to commit but untracked files present (use "git add" to track)
Please note that this will not work if the file has already git add
been added to the staging area via git checkout
.
$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ git checkout file.txt
Updated 0 paths from the index
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
In the example above, file.txt
changes to will not be reverted because the file is in the staging area.
git reset
Remove uncommitted changes in Git
using
To remove uncommitted changes in the staging area, we need to take the following steps.
-
Use
git reset
to unstage a file from the staging area. -
Use
git checkout
to undo changes.
$ git reset file.txt
Unstaged changes after reset:
M file.txt
$ git checkout file.txt
Updated 1 path from the index
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
nothing added to commit but untracked files present (use "git add" to track)
git reset
Another way to remove uncommitted changes
using is to use the --hard
and arguments HEAD
.
$ git reset --hard HEAD
HEAD is now at 1e087f5 Make some change to file.txt
$ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
nothing added to commit but untracked files present (use "git add" to track)
-
--hard
The -p option instructs Git to throw out all changes between the current state and the commit in the last argument. For this reason, this command is considered dangerous and should begit status
used after you have run Check Your Working Files. -
The alias of the latest commit
HEAD
.
Removing uncommitted changes in Git
using git stash
andgit stash
git checkout
The disadvantage of and git reset
is that they cannot remove untracked files that feature.txt
still exist after executing these commands.
Consider the first example.
$ echo 'Add new implementation' > feature.txt
$ echo 'Enhance exising feature' >> file.txt
$ git add file.txt
$ rm deprecated_feature.txt
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file.txt
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: deprecated_feature.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
feature.txt
Remove all uncommitted changes, including staged files, tracked but not staged, and untracked files. We'll use the clever method git stash
.
git stash
Allows us to save changes, but does not require it git commit
; it acts as temporary storage for uncommitted files.
After adding the changes to the temporary stash, we will tell Git about 删除
these stashes. As a result, all uncommitted changes will disappear.
$ git add .
$ git stash
Saved working directory and index state WIP on main: 16b9767 deprecated_feature.txt
$ git stash drop
Dropped refs/stash@{0} (aebeb2cbdcec917331f5793ef1238f5a525d29ec)
$ git status
On branch main
nothing to commit, working tree clean
In summary, we have several ways to remove uncommitted changes:
-
git checkout
Only useful if the file is not in the staging area. -
git reset
It is useful for changes in the staging area, but it cannot remove changes to untracked files and needs togit checkout
be used in conjunction with . -
git reset --hard HEAD
Probably shorter than the above, but potentially just as dangerous. -
git stash
Withgit add .
can delete everything, including untracked files.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Push to a specific branch in Git
Publish Date:2025/04/02 Views:53 Category:Git
-
In Git, we use branches to develop independent features directly from the main workflow of the project. Since Git is considered to be the best version control system so far, we have local and remote branches in our repository for different
Solve the Git Push Everything Up-To-Date Problem
Publish Date:2025/04/02 Views:120 Category:Git
-
Git is a free, open source version control system designed to work with projects quickly and efficiently. You can make changes to your repo and push them to master branches. This article explains how to use git push the command to resolve e
Git merge development branch into feature branch
Publish Date:2025/04/02 Views:126 Category:Git
-
Creating new branches and merging them is considered to be the most powerful tool of Git. The ability of Git to create new branches and merge them into the development code is very useful for developers working in a team environment. This f
Understanding Git conflict markers
Publish Date:2025/04/02 Views:112 Category:Git
-
In this article, we will discuss git conflict markers. Understanding Git conflict markers When pulling changes from a remote repository, you may encounter merge conflicts. Merge conflict files can sometimes be confusing. A typical merge con
Selectively merge changes from different branches in Git
Publish Date:2025/04/02 Views:116 Category:Git
-
This article will discuss merging specific changes from one branch to another. As we know, when merging branches, Git merges all files without exception. You may find yourself in a scenario where you have some commits in one branch and you
Complete the merge after resolving conflicts in Git
Publish Date:2025/04/02 Views:63 Category:Git
-
This article describes the process of completing a merge after resolving merge conflicts in Git. We will go through the merge steps, resolve the conflicts, and complete the merge. Complete the merge after resolving conflicts in Git For a si
Merge the development branch into Master in Git
Publish Date:2025/04/02 Views:167 Category:Git
-
This article provides an overview of merging a development branch into the master branch. Often we find ourselves creating a branch outside of the master branch for development. Once we are happy with the changes, we can merge them into mas
Recovering a conflicting Git merge
Publish Date:2025/04/02 Views:160 Category:Git
-
This article explains the revert command when a merge conflict occurs git merge . We will also take a quick look at how to undo a git merge that was successful and has been pushed to a remote repository. Recovering a conflicting Git merge I
Git merge-base determines the most recent common ancestor of two branches
Publish Date:2025/04/02 Views:126 Category:Git
-
This article explains how we can find the most recent common ancestor commit in Git. This comes in handy when you create a branch or merge one branch into another. Without further ado, let’s get into today’s topic. Determine the most re