Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unstage my files again after making a local commit?

Tags:

git

I have executed the following command

git add <foo.java> git commit -m "add the foo.java file" 

How can I delete my local commit now and unstage foo.java?

If I type git reset --hard, I found that it reverts my modified foo.java to the original one.

like image 998
TheOneTeam Avatar asked Jul 13 '11 17:07

TheOneTeam


People also ask

How do I Unstage files after a commit?

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.

How do I Unstage files after git add?

To undo git add before a commit, run git reset <file> or git reset to unstage all changes.


1 Answers

git reset --soft HEAD~1 should do what you want. After this, you'll have the first changes in the index (visible with git diff --cached), and your newest changes not staged. git status will then look like this:

# On branch master # Changes to be committed: #   (use "git reset HEAD <file>..." to unstage) # #       modified:   foo.java # # Changes not staged for commit: #   (use "git add <file>..." to update what will be committed) #   (use "git checkout -- <file>..." to discard changes in working directory) # #       modified:   foo.java # 

You can then do git add foo.java and commit both changes at once.

like image 158
Antti Avatar answered Oct 09 '22 19:10

Antti