标签云

微信群

扫码加入我们

WeChat QR Code

This question already has an answer here:

How can I uncommit my last commit in git?

Is it

git reset --hard HEAD

or

git reset --hard HEAD^

?


For git I find that the man page is often the best reference (or alternatively, git help reset)

2018年06月19日13分57秒

Please check here stackoverflow.com/a/49130829/7178104

2018年06月19日13分57秒

richard please choose a correct answer, whichever worked alright for you. Makes life easier for many!

2018年06月19日13分57秒

inger: True enough, I suppose, but in my mind, the question the OP meant to ask was "what do HEAD and HEAD^ mean?"

2018年06月19日13分57秒

perfect, and thanks for mentioning --soft, that's just what I wanted to do

2018年06月19日13分57秒

Jefromi: every answer in this question is totally wrong to emphasize --hard, the --soft, is necessary for it to be "uncommit last commit", a --hard will not only uncommit but also destroy your commit. I nearly destroyed a whole day's of work since I didn't recheck what --hard meant assuming 70+ answer wouldn't be wrong. Fortunately reflog saved my day, but it wouldn't have to be that way.

2018年06月20日13分57秒

Jefromi could you to switch the order you mention --hard and --soft? To uncommit (i.e. just no commit, not discard the changes) is --soft and "lazy" SO users might just see and run the first part, potentially losing a lot of hard work!

2018年06月20日13分57秒

jameshfisher It's the answer to the original question ("which of these two is it?") and there was a giant warning right underneath it. I'm editing, but... if you see a question that looks from the title like what you want, and you just blindly run the first command you see in the first answer, this is going to keep happening to you.

2018年06月19日13分57秒

git reset --soft HEAD^ More? More? fatal: ambiguous argument 'HEAD ': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]' What the heck? Why does nothing ever just work in git? I hate it.

2018年06月19日13分57秒

VioletGiraffe you are probably on windows and need to do git reset --soft "HEAD^"

2018年06月19日13分57秒

charsi: It was indeed on Windows. Thanks for the tip.

2018年06月20日13分57秒

Great answer, what I do is create in ~/.gitconfig add [alias] uncommit = reset --soft HEAD^ .... Then I can just write git uncommit and it will soft reset last commit

2018年06月19日13分57秒

Nice tip WoodyDRN

2018年06月19日13分57秒

You don't have to quote the carat with the Git bash from msysgit.

2018年06月19日13分57秒

tried this and just lost days of work here. read below for the soft version that will retain your work locally.

2018年06月19日13分57秒

Note that this not only undoes the act of committing, but also throws away your changes.

2018年06月19日13分57秒

I've just learned without ruining anything, that subsequent git reset --soft HEAD~ keeps rollin' back without an intervening commit. Fortunately, I push to a bare repository and recovered from that. Good to learn these things the non-destructive, recoverable way.

1970年01月01日00分03秒

octopusgrabbus That has to be the most useful comment I've ever seen on Git (beautiful undo) please put that as an answer. That really deserves praise.

2018年06月20日13分57秒

Is it works if you pushed to origin?

2018年06月19日13分57秒

What is that in Be careful with that? Is it git reset? I would agree.

2018年06月20日13分57秒

The rebase command. You can destroy the commit history with it.

2018年06月19日13分57秒