Septagram
Septagram

Reputation: 9785

How can you undo the last git add?

Is it possible to unstage the last staged (not committed) change in git? Suppose there were a lot of files in the current branch, some staged, some not. At some point, some foolish programmer accidentally executed:

git add -- .

...instead of:

git checkout -- .

Can this programmer now unstage his last changes with some magical git command? Or should he have committed before experimenting in the first place?

Upvotes: 207

Views: 207753

Answers (11)

Jerry Davis
Jerry Davis

Reputation: 1

Just now, I was a little too quick on the Enter key, and accidentally added about 200 files in one directory to what I had already added (that was good). Here is what I did:

  1. git status > outputfile
  2. deleted all lines except the bad files that I didn't want
  3. used vi to change the beginning of each line to be git restore --staged<space>
  4. executed that file

problem solved.

Upvotes: 0

moorhuhn
moorhuhn

Reputation: 341

I would suggest to use:

git rm -r --cached <your_dir> or git rm --cached <your_file>

It reverts your

git add <your_dir_or_your_file>

back, while keeping the files as they are.

Upvotes: 0

ireshika piyumalie
ireshika piyumalie

Reputation: 2402

You can use

git reset

to undo the recently added local files

 git reset file_name

to undo the changes for a specific file

Upvotes: 6

BSB
BSB

Reputation: 2468

If you want to remove all added files from git for commit

git reset

If you want to remove an individual file

git rm <file>

Upvotes: 2

Faisal
Faisal

Reputation: 4767

  • 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:

    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>
    

Upvotes: 2

theGiallo
theGiallo

Reputation: 156

At date git prompts:

  1. use "git rm --cached <file>..." to unstage if files were not in the repo. It unstages the files keeping them there.
  2. use "git reset HEAD <file>..." to unstage if the files were in the repo, and you are adding them as modified. It keeps the files as they are, and unstages them.

At my knowledge you cannot undo the git add -- but you can unstage a list of files as mentioned above.

Upvotes: 4

stanm
stanm

Reputation: 3321

So the real answer to

Can this programmer now unstage his last changes with some magical git command?

is actually: No, you cannot unstage just the last git add.

That is if we interpret the question as in the following situation:

Initial file:

void foo() {

}

main() {
    foo();
}

First change followed by git add:

void foo(int bar) {
    print("$bar");
}

main() {
    foo(1337);
}

Second change followed by git add:

void foo(int bar, String baz) {
    print("$bar $baz");
}

main() {
    foo(1337, "h4x0r");
}

In this case, git reset -p will not help, since its smallest granularity is lines. git doesn't know that about the intermediate state of:

void foo(int bar) {
    print("$bar");
}

main() {
    foo(1337);
}

any more.

Upvotes: 17

Philip Oakley
Philip Oakley

Reputation: 14061

Depending on size and scale of the difficultly, you could create a scratch (temporary) branch and commit the current work there.

Then switch to and checkout your original branch, and pick the appropriate files from the scratch commit.

At least you would have a permanent record of the current and previous states to work from (until you delete that scratch branch).

Upvotes: 0

knittl
knittl

Reputation: 265151

You cannot undo the latest git add, but you can undo all adds since the last commit. git reset without a commit argument resets the index (unstages staged changes):

git reset

Upvotes: 45

thameera
thameera

Reputation: 9473

You can use git reset. This will 'unstage' all the files you've added after your last commit.

If you want to unstage only some files, use git reset -- <file 1> <file 2> <file n>.

Also it's possible to unstage some of the changes in files by using git reset -p.

Upvotes: 315

davids
davids

Reputation: 6371

You could use git reset (see docs)

Upvotes: 6

Related Questions