简体   繁体   中英

How to undo the last commit in git, but keep my changes as unstaged?

I've made a commit, but now it is kind of difficult to see what I all changed. I can off course do a git diff, but I'd rather undo the last commit and keep all my changes in tact so that my IDE (PyCharm) simply shows me which files have been changed.

So is there a way to undo the last commit (really remove it) but still keep my changes in tact? All tips are welcome!

I would leave off the --soft in the other two answers and go with a simple git reset @^ (or git reset HEAD^ in older versions of git), which will default to git reset --mixed @^ . The difference is that a soft reset leaves the files staged for commit, which is not what it sounds like you want to do. If you really want to undo the commit, you should also probably unstage the changes, which is what the default does. I find this much more useful in the general case than a soft reset, which is probably why mixed is the default.

This sounds like a job for git reset --soft , whose help ( git help reset ) says this:

--soft
    Does not touch the index file nor the working tree at all
    (but resets the head to <commit>, just like all modes do).
    This leaves all your changed files "Changes to be
    committed", as git status would put it.

So the commit is undone, but the files still look the way they did before. Nothing is staged to the index.

As Peter says, I'd go with git reset --soft which should leave both your index, and working directory in tact.

In your case, if you want to go back 1 commit, you can use git reset --soft HEAD~ to point the branch at the parent commit of the current HEAD; your index and working directory will still contain your altered files.

A handy article about reset: http://git-scm.com/blog/2011/07/11/reset.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM