How to undo 'git add' before commit?

7 716

1 350

I mistakenly added files to git using the command:

git add myfile.txt

I have not yet run git commit. Is there a way to undo this, so these files won't be included in the commit?


There are 48 answers so far (some deleted). Please don't add a new one unless you have some new information.

paxos1977

Posted 2008-12-07T21:57:46.503

Reputation: 59 928

10

Starting with Git v1.8.4, all the answers below that use HEAD or head can now use @ in place of HEAD instead. See this answer (last section) to learn why you can do that.

– None – 2013-07-26T02:04:14.737

2

I made a little summery which shows all ways to unstage a file: http://stackoverflow.com/questions/6919121/why-are-there-2-ways-to-unstage-a-file-in-git/16044987#16044987

– Daniel Alder – 2014-04-26T12:09:03.067

1Why not git checkout? – Erik Reppen – 2016-09-05T14:57:00.827

7@ErikReppen git checkout does not remove staged changes from the commit index. It only reverts un-staged changes to the last committed revision - which by the way is not what I want either, I want those changes, I just want them in a later commit. – paxos1977 – 2016-09-06T21:08:58.463

2If you use Eclipse, it is as simple as unchecking the files in the commit dialogue box – Hamzahfrq – 2016-11-17T12:49:17.083

This is a great resource straight from Github: How to undo (almost) anything with Git

– jasonleonhard – 2017-02-03T21:13:30.347

Before you post a new answer, consider there are already 25+ answers for this question. Make sure that your answer contributes what is not among existing answers – Sazzad Hissain Khan – 2017-06-15T15:29:32.083

I wish I could upvote this every time I have to come back here to refer to it – scubbo – 2018-02-02T22:59:46.407

git remove myfile.txt – SimonBerton – 2018-02-13T11:48:01.027

I always make this work by running git reset <file_name>. For more info be sure to take a look at this article.

– Nesha Zoric – 2018-05-07T12:23:47.043

Answers

8 684

You can undo git add before commit with

git reset <file>

which will remove it from the current index (the "about to be committed" list) without changing anything else.

You can use

git reset

without any file name to unstage all due changes. This can come in handy when there are too many files to be listed one by one in a reasonable amount of time.

In old versions of Git, the above commands are equivalent to git reset HEAD <file> and git reset HEAD respectively, and will fail if HEAD is undefined (because you haven't yet made any commits in your repo) or ambiguous (because you created a branch called HEAD, which is a stupid thing that you shouldn't do). This was changed in Git 1.8.2, though, so in modern versions of Git you can use the commands above even prior to making your first commit:

"git reset" (without options or parameters) used to error out when you do not have any commits in your history, but it now gives you an empty index (to match non-existent commit you are not even on).

genehack

Posted 2008-12-07T21:57:46.503

Reputation: 89 674

57Of course, this is not a true undo, because if the wrong git add overwrote a previous staged uncommited version, we can't recover it. I tried to clarify this in my answer below. – leonbloy – 2013-05-06T19:10:43.013

5git reset HEAD *.ext where ext is the files of the given extension you want to unadd. For me it was *.bmp & *.zip – boulder_ruby – 2013-11-26T14:25:19.790

1git reset said it undid the changes but when I proceeded to do another git status, they still showed modified – PositiveGuy – 2015-10-16T03:39:48.453

1So the opposite of "add" is "reset"? What about "remove"? – Jonny – 2016-02-17T05:53:02.827

13@Jonny, the index (aka staging area) contains all the files, not just changed files. It "starts life" (when you check out a commit or clone a repo) as a copy of all the files in the commit pointed to by HEAD. So if you remove a file from the index (git rm --cached) it means you are preparing to make a commit that deletes that file. git reset HEAD &lt;filename&gt; on the other hand will copy the file from HEAD to the index, so that the next commit won't show any changes being made to that file. – Wildcard – 2016-03-16T12:27:34.353

what if it has been committed already? – Charlie Parker – 2016-03-20T22:20:28.743

7I just discovered that there is a git reset -p just like git add -p. This is awesome! – donquixote – 2016-07-17T23:23:05.357

1-p most definitely is awesome, and it's used in a lot of git commands (not just reset and add). But to answer @WeDoTDD.com and @Johnny, git reset by itself just clears whether Git "knows about" the changes; it doesn't clear the changes themselves. To do that you need to do git checkout someFile.txt (for individual files) or git reset --hard (to wipe everything clean). There's no going back from either of these commands though, so be very careful when using them. – machineghost – 2016-12-15T18:33:19.780

1but 'git reset file' removes other files that had been staged for commit too. not good. – Arthur – 2017-02-17T16:29:45.243

suppose I have not even used "git add". I have just now made changes to the code. SO obviously the file will show in red colour. What can I do in that context? I want my local file changes to go away. I want it to get in sync with the master. – Unbreakable – 2017-03-26T11:14:58.243

4You actually can recover overwriten previously staged but uncommited changes but not in a userfriendly way and not 100% secure (at least none I had found): goto .git/objects, search for files created at the time of git add you want to recover (61/3AF3... -> object id 613AF3...), then git cat-file -p &lt;object-id&gt; (might be worth it to recover several hours of work but also a lesson to commit more often...) – Peter Schneider – 2017-07-31T14:03:25.680

@Unbreakable: After clearing the index (staging area) with git reset, you can use git checkout file to checkout the latest version of the named file (or just git checkout to checkout the latest versions of all files), throwing away any changes that have been made locally. – Jonathan Leffler – 2018-04-09T05:25:35.130

1Another way to recover changes that were staged but not committed and then overwritten by e.g. another git add is via git fsck --unreachable that will list all unreachable obj, which you can then inspect by git show SHA-1_ID or git fsck --lost-found that will >Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type. See also git fsck --help – iolsmit – 2018-04-27T15:27:59.897

1 990

You want:

git rm --cached <added_file_to_undo>

Reasoning:

When I was new to this, I first tried

git reset .

(to undo my entire initial add), only to get this (not so) helpful message:

fatal: Failed to resolve 'HEAD' as a valid ref.

It turns out that this is because the HEAD ref (branch?) doesn't exist until after the first commit. That is, you'll run into the same beginner's problem as me if your workflow, like mine, was something like:

  1. cd to my great new project directory to try out Git, the new hotness
  2. git init
  3. git add .
  4. git status

    ... lots of crap scrolls by ...

    => Damn, I didn't want to add all of that.

  5. google "undo git add"

    => find Stack Overflow - yay

  6. git reset .

    => fatal: Failed to resolve 'HEAD' as a valid ref.

It further turns out that there's a bug logged against the unhelpfulness of this in the mailing list.

And that the correct solution was right there in the Git status output (which, yes, I glossed over as 'crap)

...
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
...

And the solution indeed is to use git rm --cached FILE.

Note the warnings elsewhere here - git rm deletes your local working copy of the file, but not if you use --cached. Here's the result of git help rm:

--cached Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left.

I proceed to use

git rm --cached .

to remove everything and start again. Didn't work though, because while add . is recursive, turns out rm needs -r to recurse. Sigh.

git rm -r --cached .

Okay, now I'm back to where I started. Next time I'm going to use -n to do a dry run and see what will be added:

git add -n .

I zipped up everything to a safe place before trusting git help rm about the --cached not destroying anything (and what if I misspelled it).

Rhubarb

Posted 2008-12-07T21:57:46.503

Reputation: 27 816

in recent versions of git (v1.7.1.1 tested ) git rm -r --cached . works fine – Tom Davies – 2011-11-22T15:23:45.753

1If you like git reset, try git reset * instead of git reset . - it un-stages all you previously staged files. – Marius Butuc – 2011-12-09T21:58:24.743

1Brilliant answer that described my workflow exactly. It really needs to be the accepted answer to be topmost as this Q is what is top of the search results for "git unstage". – Henric – 2011-12-16T09:36:51.413

1Instead of the dry run, walk through the add with git add -p – jlundqvist – 2012-04-28T22:05:18.097

Doesn't just "git reset" without . do what you want, or am I missing something? – ustun – 2009-11-11T12:21:03.013

4For me git says git reset HEAD &lt;File&gt;... – drahnr – 2012-09-12T06:50:13.967

You also could use git stash/git stash pop to avoid zipping/backing up everything – chachan – 2012-11-03T19:05:17.237

10git rm --cached <file> is actually the correct answer, if it is the initial import of <file> into the repository. If you're trying to unstage a change to the file, git reset is the correct answer. People saying that this answer is wrong are thinking of a different question. – Barry Kelly – 2013-02-28T22:14:57.907

10This will actually work, but only on the first commit, where the file didn't exist before, or where the git add command added new files, but not changes to existing files. – naught101 – 2013-04-10T02:33:36.490

1I certainly feel if this is a totally new repository, removing the .git directory and initialising again is the best way. For cases in a well-used repo where you just accidentally added a single file, git rm --cached &lt;file&gt; seems best although I get a scary delete mode 100644 file after my commit. – deed02392 – 2013-07-14T22:28:14.923

1according to git status output of git version 1.8.1.4 the correct way to unstage new files is: git reset HEAD &lt;file&gt;... – akostadinov – 2013-08-28T18:25:05.340

3just goes to show how unintuitive and convoluted git is. instead of having parallel "undo" commands, you have to find out how to undo them. Like trying to free your leg in quick sand, and then getting your arm stuck, then getting your other arm stuck... every command should be done through GUI, with dropdown menus items for the options... Think of all the UI, productivity gains we've had, but we have this mess of a retro command line interface. It's not like the git GUI programs make this any more intuitive. – ahnbizcad – 2014-05-24T10:54:36.857

2@BarryKelly no it is not the correct answer. What you actually want to do is either git reset to undo all added files or git reset &lt;file&gt; to undo a specific added file. git rm --cached may work on the surface but what actually happens is it removes it from the tracking history as well which is not what you would want to do, unless, you added that file to a gitignore file where that file shouldn't have been tracked in the first place then in that case it would be ok. – JoeMoe1984 – 2014-06-02T21:14:06.730

@JoeMoe1984 git reset doesn't work on empty repos (initial imports) – Barry Kelly – 2014-06-03T13:40:23.803

@BarryKelly I tried to edit my comment as I reread yours but I was too late. I missed that part of what you wrote ;). But yeah you're right, that would be another instance to use git rm --cached – JoeMoe1984 – 2014-06-03T17:33:45.757

When intially populating a git repository, "git reset" from the top answer doesn't work: failed to resolve HEAD. We added a single file too many to the initial commit. As suggested by this answer, in that case "git rm --cached <file>" works for me. Maybe "git rm --cached ." does more than is intended for some people, but (as suggested by git status), git rm --cached <file> works.... – rew – 2015-10-20T11:31:15.843

1Why not just delete the hidden .git folder lol.. and do git init again... (assuming you're making the git repo for the FIRST time and have not done any commits) – Kevin Lee – 2016-04-05T14:16:48.207

10Hah. I followed this same process. Except I gave up and said rm -rf .git, git init because I didn't trust git rm --cached to keep my working copy. It says a little for how git is still overly complex in some places. git unstage should just be a stock standard command, I don't care if I can add it as an alias. – Adrian Macneil – 2011-03-29T03:45:18.290

1If the files have not yet in index, use git rm --cache &lt;filename&gt;, if the files already in index, but you don't want them to be in this commit, use git reset &lt;filename&gt; – Ding-Yi Chen – 2016-10-20T01:46:10.103

Did this and it deleted all my other existing files - unchanged - from the git backup. Re-adding them makes everything larger and destroys the proper history. Git is totally an unfinished project. – SamGoody – 2011-06-01T09:09:18.517

This may have been applicable at the time of writing but seems to work now. – Thomas Weller – 2017-06-08T10:20:25.723

This is also a wrong answer. I don't know why people post answers, and even less why they upvote wrong answers.... – Vladimir Despotovic – 2018-09-27T13:22:37.597

492

If you type:

git status

git will tell you what is staged, etc, including instructions on how to unstage:

use "git reset HEAD <file>..." to unstage

I find git does a pretty good job of nudging me to do the right thing in situations like this.

Note: Recent git versions (1.8.4.x) have changed this message:

(use "git rm --cached <file>..." to unstage)

Paul Beckingham

Posted 2008-12-07T21:57:46.503

Reputation: 11 184

13The message will be different depending on whether the added file was already being tracked (the add only saved a new version to the cache - here it will show your message). Elsewhere, if the file was not previously staged, it will display use "git rm --cached &lt;file&gt;..." to unstage – leonbloy – 2013-05-06T18:25:26.480

Great! The git reset HEAD &lt;file&gt; one is the only one that will work in case you want to unstage a file delete – skerit – 2018-02-24T00:25:25.980

My git version 2.14.3 says git reset HEAD to unstage. – SilverWolf – 2018-04-23T19:16:53.103

226

To clarify: git add moves changes from the current working directory to the staging area (index).

This process is called staging. So the most natural command to stage the changes (changed files) is the obvious one:

git stage

git add is just an easier to type alias for git stage

Pity there is no git unstage nor git unadd commands. The relevant one is harder to guess or remember, but is pretty obvious:

git reset HEAD --

We can easily create an alias for this:

git config --global alias.unadd 'reset HEAD --'
git config --global alias.unstage 'reset HEAD --'

And finally, we have new commands:

git add file1
git stage file2
git unadd file2
git unstage file1

Personally I use even shorter aliases:

git a #for staging
git u #for unstaging

takeshin

Posted 2008-12-07T21:57:46.503

Reputation: 29 300

2"moves"? This would indicate it has gone from the working directory. That's not the case. – Thomas Weller – 2017-06-08T09:18:33.273

3Why is it obvious? – Lenar Hoyt – 2017-06-23T18:25:52.650

Actually, git stage is the alias for git add, which is the historic command, both on Git and other SCM. It has been added in december 2008 with commit 11920d28da in the "Git's git repository", if I can say. – Obsidian – 2018-09-12T18:13:20.837

146

An addition to the accepted answer, if your mistakenly added file was huge, you'll probably notice that, even after removing it from the index with 'git reset', it still seems to occupy space in the .git directory. This is nothing to be worried about, the file is indeed still in the repository, but only as a "loose object", it will not be copied to other repositories (via clone, push), and the space will be eventually reclaimed - though perhaps not very soon. If you are anxious, you can run:

git gc --prune=now

Update (what follows is my attempt to clear some confusion that can arise from the most up-voted answers):

So, which is the real undo of git add?

git reset HEAD <file> ?

or

git rm --cached <file>?

Strictly speaking, and if I'm not mistaken: none.

git add cannot be undone - safely, in general.

Let's recall first what git add <file> actually does:

  1. If <file> was not previously tracked, git add adds it to the cache, with its current content.

  2. If <file> was already tracked, git add saves the current content (snapshot, version) to the cache. In GIT, this action is still called add, (not mere update it), because two different versions (snapshots) of a file are regarded as two different items: hence, we are indeed adding a new item to the cache, to be eventually commited later.

In light of this, the question is slightly ambiguous:

I mistakenly added files using the command...

The OP's scenario seems to be the first one (untracked file), we want the "undo" to remove the file (not just the current contents) from the tracked items. If this is the case, then it's ok to run git rm --cached <file>.

And we could also run git reset HEAD <file>. This is in general preferable, because it works in both scenarios: it also does the undo when we wrongly added a version of an already tracked item.

But there are two caveats.

First: There is (as pointed out in the answer) only one scenario in which git reset HEAD doesn't work, but git rm --cached does: a new repository (no commits). But, really, this a practically irrelevant case.

Second: Be aware that git reset HEAD can't magically recover the previously cached file contents, it just resyncs it from the HEAD. If our misguided git add overwrote a previous staged uncommitted version, we can't recover it. That's why, strictly speaking, we cannot undo [*].

Example:

$ git init
$ echo "version 1" > file.txt
$ git add file.txt   # first add  of file.txt
$ git commit -m 'first commit'
$ echo "version 2" > file.txt
$ git add  file.txt   # stage (don't commit) "version 2" of file.txt
$ git diff --cached file.txt
-version 1
+version 2
$ echo "version 3" > file.txt   
$ git diff  file.txt
-version 2
+version 3
$ git add  file.txt    # oops we didn't mean this
$ git reset HEAD file.txt  # undo ?
$ git diff --cached file.txt  # no dif, of course. stage == HEAD
$ git diff file.txt   # we have lost irrevocably "version 2"
-version 1
+version 3

Of course, this is not very critical if we just follow the usual lazy workflow of doing 'git add' only for adding new files (case 1), and we update new contents via the commit, git commit -a command.


* (Edit: the above is practically correct, but still there can be some slightly hackish/convoluted ways for recovering changes that were staged but not committed and then overwritten - see the comments by Johannes Matokic and iolsmit)

leonbloy

Posted 2008-12-07T21:57:46.503

Reputation: 52 293

3Strictly speaking there is a way to recover an already staged file that was replaced with git add. As you mention git add creates an git object for that file that will become a loose object not only when removing the file completely but also when being overwritten with new content. But there is no command to automatically recover it. Instead the file has to be identified and extracted manually or with tools written only for this case (libgit2 will allow this). But this will only pay out if the file is very important and big and could not be rebuild by editing the previous version. – Johannes Matokic – 2017-12-06T13:07:37.320

2To correct myself: Once the loose object file is found (use meta-data like creation date/time) git cat-file could be used to recover its content. – Johannes Matokic – 2017-12-06T13:22:17.027

1Another way to recover changes that were staged but not committed and then overwritten by e.g. another git add is via git fsck --unreachable that will list all unreachable obj, which you can then inspect by git show SHA-1_ID or git fsck --lost-found that will >Write dangling objects into .git/lost-found/commit/ or .git/lost-found/other/, depending on type. See also git fsck --help – iolsmit – 2018-04-27T15:29:46.953

89

git rm --cached . -r

will "un-add" everything you've added from your current directory recursively

braitsch

Posted 2008-12-07T21:57:46.503

Reputation: 9 159

3I wasn't looking to un-add everything, just ONE specific file. – paxos1977 – 2009-12-09T22:35:27.763

3Also helpful if you don't have any previous commits. In absence of previous commit, git reset HEAD &lt;file&gt; would say fatal: Failed to resolve 'HEAD' as a valid ref. – Priya Ranjan Singh – 2013-06-02T03:46:13.853

5No, this adds a deletion of everything in your current directory. Very different to just unstaging changes. – Mark Amery – 2015-10-30T01:33:56.640

83

Run

git gui

and remove all the files manually or by selecting all of them and clicking on the unstage from commit button.

Khaja Minhajuddin

Posted 2008-12-07T21:57:46.503

Reputation: 5 078

1Yes I understand that. I only wanted to implicitly suggest that your indicate that on your answer like "You can use git-gui...." :) – Alexander Suraphel – 2014-08-01T16:11:05.947

1It says, "git-gui: command not found". I'm not sure if this works. – Parinda Rajapaksha – 2017-09-13T04:19:18.730

78

Git has commands for every action imaginable, but needs extensive knowledge to get things right and because of that it is counter-intuitive at best...

What you did before:

  • Changed a file and used git add ., or git add <file>.

What you want:

  • Remove the file from the index, but keep it versioned and left with uncommitted changes in working copy:

    git reset head <file>
    
  • Reset the file to the last state from HEAD, undoing changes and removing them from the index:

    # Think `svn revert <file>` IIRC.
    git reset HEAD <file>
    git checkout <file>
    
    # If you have a `<branch>` named like `<file>`, use:
    git checkout -- <file>
    

    This is needed since git reset --hard HEAD won't work with single files.

  • Remove <file> from index and versioning, keeping the un-versioned file with changes in working copy:

    git rm --cached <file>
    
  • Remove <file> from working copy and versioning completely:

    git rm <file>
    

sjas

Posted 2008-12-07T21:57:46.503

Reputation: 10 434

1I can't under stand the difference of 'git reset head <file>' and 'git rm --cached <file>. Could you explain it? – jeswang – 2013-08-14T00:39:07.537

5@jeswang files are either 'known' to git (changes in them are being tracked.), or they are not 'versioned'. reset head undoes your current changes, but the file is still being monitored by git. rm --cached takes the file out of versioning, so git no longer checks it for changes (and also removes eventually indexed present changes, told to git by the prior add), but the changed file will be kept in your working copy, that is in you file folder on the HDD. – sjas – 2013-08-15T15:09:40.277

2The difference is git reset HEAD &lt;file&gt; is temporary - the command will be applied to the next commit only, but git rm --cached &lt;file&gt; will unstage untill it gets added again with git add &lt;file&gt;. Also, git rm --cached &lt;file&gt; means if you push that branch to the remote, anyone pulling the branch will get the file ACTUALLY deleted from their folder. – DrewT – 2014-08-10T19:54:43.343

69

The question is not clearly posed. The reason is that git add has two meanings:

  1. adding a new file to the staging area, then undo with git rm --cached file.
  2. adding a modified file to the staging area, then undo with git reset HEAD file.

if in doubt, use

git reset HEAD file

Because it does the expected thing in both cases.

Warning: if you do git rm --cached file on a file that was modified (a file that existed before in the repository), then the file will be removed on git commit! It will still exist in your file system, but if anybody else pulls your commit, the file will be deleted from their work tree.

git status will tell you if the file was a new file or modified:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   my_new_file.txt
    modified:   my_modified_file.txt

Michael_Scharf

Posted 2008-12-07T21:57:46.503

Reputation: 19 455

4+1. An extraordinary number of highly-upvoted answers and comments on this page are just flat-out wrong about the behaviour of git rm --cached somefile. I hope this answer makes its way up the page to a prominent position where it can protect newbies from being misled by all the false claims. – Mark Amery – 2015-10-30T23:44:32.300

69

Undo a file which already added is quite easy using git, for resetting myfile.txt which already added, use:

git reset HEAD myfile.txt

Explain:

After you staged unwanted file(s), to undo, you can do git reset, Head is head of your file in local and the last parameter is the name of your file.

I create the steps in the image below in more details for you, including all steps which may happen in these cases:

git reset HEAD

Alireza

Posted 2008-12-07T21:57:46.503

Reputation: 43 514

58

If you're on your initial commit and you can't use git reset, just declare "Git bankruptcy" and delete the .git folder and start over

Paul Betts

Posted 2008-12-07T21:57:46.503

Reputation: 64 018

3@ChrisJohnsen comment is spot on. Sometimes, you want to commit all files except one: git add -A &amp;&amp; git rm --cached EXCLUDEFILE &amp;&amp; git commit -m 'awesome commit' (This also works when there's no previous commits, re Failed to resolve 'HEAD' problem) – Barry – 2013-03-29T04:20:41.283

5One tip is to copy your .git/config file if you have added remote origin, before deleting the folder. – Tiago – 2010-03-08T23:15:52.933

54

As per many of the other answers you can use git reset

BUT:

I found this great little post that actually adds the Git command (well an alias) for git unadd: see git unadd for details or..

Simply,

git config --global alias.unadd "reset HEAD"

Now you can

git unadd foo.txt bar.txt

electblake

Posted 2008-12-07T21:57:46.503

Reputation: 1 539

47

git remove or git rm can be used for this, with the --cached flag. Try:

git help rm

gnud

Posted 2008-12-07T21:57:46.503

Reputation: 60 023

6Isn't this going to remove the file altogether? – Willa – 2015-08-26T05:29:10.037

41

Use git add -i to remove just-added files from your upcoming commit. Example:

Adding the file you didn't want:

$ git add foo
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   foo
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
# [...]#

Going into interactive add to undo your add (the commands typed at git here are "r" (revert), "1" (first entry in the list revert shows), 'return' to drop out of revert mode, and "q" (quit):

$ git add -i
           staged     unstaged path
  1:        +1/-0      nothing foo

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> r
           staged     unstaged path
  1:        +1/-0      nothing [f]oo
Revert>> 1
           staged     unstaged path
* 1:        +1/-0      nothing [f]oo
Revert>> 
note: foo is untracked now.
reverted one path

*** Commands ***
  1: [s]tatus     2: [u]pdate     3: [r]evert     4: [a]dd untracked
  5: [p]atch      6: [d]iff       7: [q]uit       8: [h]elp
What now> q
Bye.
$

That's it! Here's your proof, showing that "foo" is back on the untracked list:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
# [...]
#       foo
nothing added to commit but untracked files present (use "git add" to track)
$

Alex North-Keys

Posted 2008-12-07T21:57:46.503

Reputation: 3 041

35

Here's a way to avoid this vexing problem when you start a new project:

  • Create the main directory for your new project.
  • Run git init.
  • Now create a .gitignore file (even if it's empty).
  • Commit your .gitignore file.

Git makes it really hard to do git reset if you don't have any commits. If you create a tiny initial commit just for the sake of having one, after that you can git add -A and git reset as many times as you want in order to get everything right.

Another advantage of this method is that if you run into line-ending troubles later and need to refresh all your files, it's easy:

  • Check out that initial commit. This will remove all your files.
  • Then check out your most recent commit again. This will retrieve fresh copies of your files, using your current line-ending settings.

Ryan Lundy

Posted 2008-12-07T21:57:46.503

Reputation: 153 812

1Confirmed! Tried a git reset after a git add . and git was complaining about corrupt HEAD. Following your advice, I could git add & reset back and forth with no problems :) – Kounavi – 2012-10-03T21:32:16.720

1The second part works, but it is a bit clumsy. How line endings are handled, depends on autocrlf value... This won't work in every project, depending the settings. – sjas – 2013-03-29T11:26:58.743

1This answer was reasonable at the time it was posted, but is now obsolete; git reset somefile and git reset both work prior to making the first commit, now. This has been the case since several Git releases back. – Mark Amery – 2015-10-30T23:38:32.840

@MarkAmery, you may be right (it'd be cool if you posted a source for your assertion), but there's still value in starting your repo with a clean commit or two. – Ryan Lundy – 2015-10-31T20:01:14.793

32

Maybe Git has evolved since you posted your question.

$> git --version
git version 1.6.2.1

Now, you can try:

git reset HEAD .

This should be what you are looking for.

Kokotte23

Posted 2008-12-07T21:57:46.503

Reputation: 337

2Sure, but then you have the followup question of how one should unadd one of two (or more) files added. The "git reset" manual does mention that "git reset <paths>" is the opposite of "git add <paths>", however. – Alex North-Keys – 2013-05-15T13:36:21.690

32

Note that if you fail to specify a revision then you have to include a separator. Example from my console:

git reset <path_to_file>
fatal: ambiguous argument '<path_to_file>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

git reset -- <path_to_file>
Unstaged changes after reset:
M   <path_to_file>

(git version 1.7.5.4)

powlo

Posted 2008-12-07T21:57:46.503

Reputation: 1 195

2I tried git reset &lt;path&gt; and it works just fine without a separator. I'm also using git 1.9.0. Maybe it doesn't work in older versions? – None – 2014-04-05T05:32:25.710

29

To remove new files from the staging area (and only in case of a new file), as suggested above:

git rm --cached FILE

Use rm --cached only for new files accidentally added.

Ran

Posted 2008-12-07T21:57:46.503

Reputation: 4 742

2Mind that the --cached is a really important part here. – takeshin – 2013-04-12T12:21:46.690

-1; no, this doesn't un-stage the file, it stages a deletion of the file (without actually deleting it from your work tree). – Mark Amery – 2015-10-30T23:42:05.460

23

To reset every file in a particular folder (and its subfolders), you can use the following command:

git reset *

Zorayr

Posted 2008-12-07T21:57:46.503

Reputation: 14 703

3Actually, this does not reset every file because * uses shell expansion and it ignores dotfiles (and dot-directories). – Luc – 2014-05-04T23:20:01.893

You can run git status to see anything remaining and reset it manually i.e. git reset file. – Zorayr – 2014-05-07T15:23:55.677

23

use the * command to handle multiple files at a time

git reset HEAD *.prj
git reset HEAD *.bmp
git reset HEAD *gdb*

etc

boulder_ruby

Posted 2008-12-07T21:57:46.503

Reputation: 26 043

2Mind that will usually not include dotfiles or 'dot-directories' unless you explicitly specify `.or.*.prj` – Luc – 2014-05-04T23:21:06.620

21

Just type git reset it will revert back and it is like you never typed git add . since your last commit. Make sure you have committed before.

Donovan

Posted 2008-12-07T21:57:46.503

Reputation: 235

1Won't work if there's no last commit. – meowsqueak – 2012-04-11T22:07:03.760

As it happens, there was a last commit... but I was specifically asking about removing a single file from the commit, not every file from the commit. – paxos1977 – 2013-01-31T16:21:37.823

16

Suppose I create a new file newFile.txt.

enter image description here

Suppose I add the file accidently, git add newFile.txt

enter image description here

Now I want to undo this add, before commit, git reset newFile.txt

enter image description here

Vidura Mudalige

Posted 2008-12-07T21:57:46.503

Reputation: 545

14

For a specific file:

  • git reset my_file.txt
  • git checkout my_file.txt

For all added files:

  • git reset .
  • git checkout .

Note: checkout changes the code in the files and moves to the last updated (committed) state. reset doesn't change the codes; it just resets the header.

Hasib Kamal

Posted 2008-12-07T21:57:46.503

Reputation: 670

12

This command will unstash your changes:

git reset HEAD filename.txt

You can also use

git add -p 

to add parts of files.

wallerjake

Posted 2008-12-07T21:57:46.503

Reputation: 2 991

12

To undo git add use

git reset filename

Anirudh Sood

Posted 2008-12-07T21:57:46.503

Reputation: 1 132

11

I'm surprised that no one mention interactive mode:

git add -i

choose option 3 to un add files. In my case i often want to add more than one file, with interactive mode you can use numbers like this to add files. This will take all but 4: 1,2,3,5

To choose a sequence just type 1-5 to take all from 1 to 5.

Git staging files

Jonathan

Posted 2008-12-07T21:57:46.503

Reputation: 169

"I'm surprised that no one mention interactive mode" - they did: http://stackoverflow.com/a/10209776/1709587 – Mark Amery – 2015-10-30T23:52:13.930

8

git reset filename.txt

Will remove a file named filename.txt from the current index, the "about to be committed" area, without changing anything else.

Rahul Sinha

Posted 2008-12-07T21:57:46.503

Reputation: 531

8

git add myfile.txt # this will add your file into to be committed list

Quite opposite to this command is,

git reset HEAD myfile.txt  # this will undo it. 

so, you will be in previous state. specified will be again in untracked list (previous state).

it will reset your head with that specified file. so, if your head doesn't have it means, it will simply reset it

Mohideen ibn Mohammed

Posted 2008-12-07T21:57:46.503

Reputation: 6 869

8

git reset filename.txt  

Will remove a file named filename.txt from the current index, the "about to be committed" area, without changing anything else.

Joseph Mathew

Posted 2008-12-07T21:57:46.503

Reputation: 759

7

In SourceTree you can do this easily via the gui. You can check which command sourcetree uses to unstage a file.

I created a new file and added it to git. Then I unstaged it using the SourceTree gui. This is the result:

Unstaging files [08/12/15 10:43]
git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree reset -q -- path/to/file/filename.java

SourceTree uses reset to unstage new files.

miva2

Posted 2008-12-07T21:57:46.503

Reputation: 1 712

6

One of the most intuitive solutions is using SourceTree.

You can just drag and drop files from staged and unstaged enter image description here

Marcin Szymczak

Posted 2008-12-07T21:57:46.503

Reputation: 6 673

-6

git stash && git stash pop

Will erace the staging.

ohadgk

Posted 2008-12-07T21:57:46.503

Reputation: 1 145

-9

The command git reset --hard HEAD should work. The one thing to note is that you need to changed directory (cd) back into your normal working directory. Otherwise if you run the command from the directory you mistakenly did the git add . .... you will not be able to revert out and instead get the errors mentioned in other posts regard "unknown revision or path not in the working tree".

dan.true

Posted 2008-12-07T21:57:46.503

Reputation: 25

23WARNING: this won't just unstage files from you index, it will completely erase their changes from your working copy too! I would not recommend that anyone use a hard reset just to unstage files...not unless they're fans of completely losing their work. – None – 2014-07-20T07:55:09.790