0

I have mistakenly did git add . Now I have multiple files in staging area. I know I can use git reset to unstage files but this would take most of my time.

I am looking for a solution that would save my time. Now I want to know that

  • Is there a possible way to undo last git add . command fired on git. So that I don't have to do git reset file multiple times.

  • If the above way is not possible then is there a way I could do reset multiple files using a single command.

2

2 Answers 2

1

Did you have staged files you want to keep staged, or are you willing to have all files go back to completely unstaged?

The latter is very easy: use git reset with no additional arguments, as galath commented. This does a --mixed reset:

  • move the current branch to the named commit, but with no named commit, that means move the current branch to HEAD, and HEAD is the current commit, so we move the current commit to itself, which does nothing; then
  • replace files in the index with their versions in the named commit (which, again, we didn't name, so that's the HEAD commit).

(These two steps are the --soft and --mixed parts; without --hard, git reset skips the third step of wiping out work-tree changes. Obviously you don't want those wiped out, so you should not use --hard.)

The former is harder. To get a list of all staged files, use:

git diff --cached --name-only

If you want to selectively re-set most, but not all, of these files, you can write those file names to a file (preferably outside your work-tree) such as /tmp/list, then edit the file to remove the names you want to keep staged. Then:

git reset -- $(cat /tmp/list)

or:

xargs git reset -- < /tmp/list

to run git reset -- on each path name. This assumes there is no white-space in the file names; if there is, use additional shell trickery to break only at newlines (assuming no newlines in the path names).

If you had carefully staged some but not all of some files using git add -p, it is usually possible to recover these using git fsck --lost-found and poking through all the resulting "dangling blobs", but that's rarely worth the effort.

1
  • Thanks for the answer. Commented Mar 11, 2017 at 9:41
0

It appears this should work: git reset -p <file_name>

Not the answer you're looking for? Browse other questions tagged or ask your own question.