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

前端 未结 7 779
灰色年华
灰色年华 2020-12-22 16:16

I have executed the following command

git add 
git commit -m \"add the foo.java file\"

How can I delete my local commit now

相关标签:
7条回答
  • 2020-12-22 16:19

    Lets say you want to unstage changes upto n commits,

    Where commit hashes are as follows:

    • h1
    • h2 ...
    • hn
    • hn+1

    Then run the following command:
    git reset hn

    Now the HEAD will be at hn+1. Changes from h1 to hn will be unstaged.

    0 讨论(0)
  • 2020-12-22 16:21

    Use:

    git reset HEAD^
    

    That does a "mixed" reset by default, which will do what you asked; put foo.java in unstaged, removing the most recent commit.

    0 讨论(0)
  • 2020-12-22 16:27

    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.

    0 讨论(0)
  • 2020-12-22 16:29

    git reset --soft is just for that: it is like git reset --hard, but doesn't touch the files.

    0 讨论(0)
  • 2020-12-22 16:41

    For unstaging all the files in your last commit -

    git reset HEAD~

    0 讨论(0)
  • 2020-12-22 16:42

    "Reset" is the way to undo changes locally. When committing, you first select changes to include with "git add"--that's called "staging." And once the changes are staged, then you "git commit" them.

    To back out from either the staging or the commit, you "reset" the HEAD. On a branch, HEAD is a git variable that points to the most recent commit. So if you've staged but haven't committed, you "git reset HEAD." That backs up to the current HEAD by taking changes off the stage. It's shorthand for "git reset --mixed HEAD~0."

    If you've already committed, then the HEAD has already advanced, so you need to back up to the previous commit. Here you "reset HEAD~1" or "reset HEAD^1" or "reset HEAD~" or "reset HEAD^"-- all reference HEAD minus one.

    Which is the better symbol, ~ or ^? Think of the ~ tilde as a single stream -- when each commit has a single parent and it's just a series of changes in sequence, then you can reference back up the stream using the tilde, as HEAD~1, HEAD~2, HEAD~3, for parent, grandparent, great-grandparent, etc. (technically it's finding the first parent in earlier generations).

    When there's a merge, then commits have more than one parent. That's when the ^ caret comes into play--you can remember because it shows the branches coming together. Using the caret, HEAD^1 would be the first parent and HEAD^2 would be the second parent of a single commit--mother and father, for example.

    So if you're just going back one hop on a single-parent commit, then HEAD~ and HEAD^ are equivalent--you can use either one.

    Also, the reset can be --soft, --mixed, or --hard. A soft reset just backs out the commit--it resets the HEAD, but it doesn't check out the files from the earlier commit, so all changes in the working directory are preserved. And --soft reset doesn't even clear the stage (also known as the index), so all the files that were staged will still be on stage.

    A --mixed reset (the default) also does not check out the files from the earlier commit, so all changes are preserved, but the stage is cleared. That's why a simple "git reset HEAD" will clear off the stage.

    A --hard reset resets the HEAD, and it clears the stage, but it also checks out all the files from the earlier commit and so it overwrites any changes.

    If you've pushed the commit to a remote repository, then reset doesn't work so well. You can reset locally, but when you try to push to the remote, git will see that your local HEAD is behind the HEAD in the remote branch and will refuse to push. You may be able to force the push, but git really does not like doing that.

    Alternatively, you can stash your changes if you want to keep them, check out the earlier commit, un-stash the changes, stage them, create a new commit, and then push that.

    0 讨论(0)
提交回复
热议问题