<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Git Commands on Daniel Zhang</title><link>https://zhangdapeng.dev/tags/git-commands/</link><description>Recent content in Git Commands on Daniel Zhang</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Fri, 04 Dec 2026 14:20:00 +0800</lastBuildDate><atom:link href="https://zhangdapeng.dev/tags/git-commands/index.xml" rel="self" type="application/rss+xml"/><item><title>How to Undo Git Add: Remove Files from the Staging Area</title><link>https://zhangdapeng.dev/p/undo-git-add-remove-files-from-staging-area/</link><pubDate>Fri, 04 Dec 2026 14:20:00 +0800</pubDate><guid>https://zhangdapeng.dev/p/undo-git-add-remove-files-from-staging-area/</guid><description>&lt;img src="https://zhangdapeng.dev/p/undo-git-add-remove-files-from-staging-area/cover.png" alt="Featured image of post How to Undo Git Add: Remove Files from the Staging Area" /&gt;&lt;p&gt;When working with Git, you may accidentally add files to the staging area by running commands such as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git add .
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This command stages all new and modified files in the current directory and its subdirectories. Sometimes, you may accidentally include files that should not be committed, such as log files, environment configurations, dependencies, or temporary build artifacts.&lt;/p&gt;
&lt;p&gt;Fortunately, you can undo &lt;code&gt;git add&lt;/code&gt; without deleting your local files.&lt;/p&gt;
&lt;p&gt;However, the correct command depends on whether your repository already has at least one commit.&lt;/p&gt;
&lt;h2 id="1-check-the-current-git-status"&gt;1. Check the Current Git Status
&lt;/h2&gt;&lt;p&gt;Before making changes, check which files are currently staged:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git status
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You may see output similar to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Changes to be committed:
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; new file: config.local
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; new file: package-lock.json
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; modified: README.md
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;These files are currently in the staging area and will be included in the next commit.&lt;/p&gt;
&lt;h2 id="2-undo-git-add-for-a-single-file"&gt;2. Undo Git Add for a Single File
&lt;/h2&gt;&lt;p&gt;If you only want to remove one file from the staging area, use:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git restore --staged &amp;lt;filename&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git restore --staged config.local
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This removes &lt;code&gt;config.local&lt;/code&gt; from the staging area but keeps the file on your local filesystem.&lt;/p&gt;
&lt;p&gt;For older Git versions, you can use:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git reset HEAD &amp;lt;filename&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Both commands serve the same purpose in this situation.&lt;/p&gt;
&lt;h2 id="3-undo-git-add-for-all-staged-files"&gt;3. Undo Git Add for All Staged Files
&lt;/h2&gt;&lt;p&gt;If you accidentally ran &lt;code&gt;git add .&lt;/code&gt; and want to unstage everything, use:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git restore --staged .
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Alternatively:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git reset
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Both commands remove files from the staging area without deleting their local contents or discarding your working directory changes.&lt;/p&gt;
&lt;p&gt;Afterward, run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git status
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The files should now appear under &lt;code&gt;Changes not staged for commit&lt;/code&gt; or &lt;code&gt;Untracked files&lt;/code&gt;, depending on their previous state.&lt;/p&gt;
&lt;h2 id="4-what-if-the-repository-has-no-initial-commit"&gt;4. What If the Repository Has No Initial Commit?
&lt;/h2&gt;&lt;p&gt;There is an important exception.&lt;/p&gt;
&lt;p&gt;If you have just initialized a Git repository:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git init
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git add .
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But have not created your first commit, running the following command may produce an error:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git reset HEAD
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;fatal: ambiguous argument &amp;#39;HEAD&amp;#39;: unknown revision or path not in the working tree.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This happens because &lt;code&gt;HEAD&lt;/code&gt; does not yet point to an existing commit.&lt;/p&gt;
&lt;p&gt;In this situation, use &lt;code&gt;git rm --cached&lt;/code&gt; instead.&lt;/p&gt;
&lt;h3 id="remove-a-single-file"&gt;Remove a Single File
&lt;/h3&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git rm --cached &amp;lt;filename&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git rm --cached config.local
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="remove-an-entire-directory"&gt;Remove an Entire Directory
&lt;/h3&gt;&lt;p&gt;If you accidentally staged a directory such as &lt;code&gt;node_modules&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git rm -r --cached node_modules/
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="remove-all-staged-files"&gt;Remove All Staged Files
&lt;/h3&gt;&lt;p&gt;To clear the entire staging area in a repository without an initial commit:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git rm -r --cached .
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;--cached&lt;/code&gt; option is important because it tells Git to remove files from the index without deleting the actual files from your working directory.&lt;/p&gt;
&lt;p&gt;After running the command, check the result:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git status
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The files should appear as untracked files again.&lt;/p&gt;
&lt;h2 id="5-prevent-accidentally-staging-unwanted-files"&gt;5. Prevent Accidentally Staging Unwanted Files
&lt;/h2&gt;&lt;p&gt;If you frequently encounter accidentally staged files, consider configuring a &lt;code&gt;.gitignore&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;For example, a typical &lt;code&gt;.gitignore&lt;/code&gt; might contain:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code class="language-gitignore" data-lang="gitignore"&gt;# Dependencies
node_modules/
vendor/

# Environment files
.env
.env.local

# Logs
*.log
logs/

# Build artifacts
dist/
build/

# IDE settings
.idea/
.vscode/

# Operating system files
.DS_Store
Thumbs.db
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Git will ignore matching untracked files when you run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git add .
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;code&gt;.gitignore&lt;/code&gt; does not automatically remove files that are already tracked or staged. If a file has already been added to Git, you may need to remove it from the index separately.&lt;/p&gt;
&lt;h2 id="6-what-if-you-have-already-committed"&gt;6. What If You Have Already Committed?
&lt;/h2&gt;&lt;p&gt;If you have already created a commit but have not pushed it, you can undo the latest commit while keeping all changes staged:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git reset --soft HEAD~1
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then remove unwanted files from the staging area:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git restore --staged &amp;lt;filename&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git reset --soft HEAD~1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;git restore --staged config.local
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Your changes will remain in the working directory.&lt;/p&gt;
&lt;p&gt;Be careful when using &lt;code&gt;git reset&lt;/code&gt; on commits that have already been pushed to a shared remote repository. Rewriting shared history can affect other contributors. In that situation, &lt;code&gt;git revert&lt;/code&gt; is generally a safer option for undoing a commit.&lt;/p&gt;
&lt;h2 id="7-quick-reference"&gt;7. Quick Reference
&lt;/h2&gt;&lt;p&gt;Here is a summary of the most commonly used commands:&lt;/p&gt;
&lt;table&gt;
	&lt;thead&gt;
			&lt;tr&gt;
					&lt;th&gt;Scenario&lt;/th&gt;
					&lt;th&gt;Command&lt;/th&gt;
			&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;
			&lt;tr&gt;
					&lt;td&gt;Unstage one file&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git restore --staged &amp;lt;file&amp;gt;&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Unstage all files&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git restore --staged .&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Alternative to unstage all&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git reset&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Unstage one file without an initial commit&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git rm --cached &amp;lt;file&amp;gt;&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Unstage a directory without an initial commit&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git rm -r --cached &amp;lt;directory&amp;gt;&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Clear staging area without an initial commit&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git rm -r --cached .&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Undo the latest commit but keep changes staged&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git reset --soft HEAD~1&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
					&lt;td&gt;Check Git status&lt;/td&gt;
					&lt;td&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/td&gt;
			&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;Accidentally running &lt;code&gt;git add .&lt;/code&gt; is usually not a serious problem. Git provides several commands to remove files from the staging area without deleting your local changes.&lt;/p&gt;
&lt;p&gt;The most important thing to remember is the difference between repositories with and without an initial commit:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Repository with an existing commit:&lt;/strong&gt; Use &lt;code&gt;git restore --staged&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Repository without an initial commit:&lt;/strong&gt; Use &lt;code&gt;git rm --cached&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Already committed but not pushed:&lt;/strong&gt; Consider &lt;code&gt;git reset --soft HEAD~1&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Before committing, always run &lt;code&gt;git status&lt;/code&gt; to verify which files are staged. This simple habit can help prevent accidentally committing sensitive configuration files, dependencies, or unnecessary build artifacts.&lt;/p&gt;</description></item></channel></rss>