WelcomeTo
WelcomeTo

Reputation: 20581

Just undo GIt's 'commit' command (without reverting files to previous commit)

I want to cancel last GIT commit, without reverting files to previous commit. I.e. I want to return to state that was one moment before committing - so I can see all new/modified files before committing. It's possible?

Upvotes: 1

Views: 81

Answers (3)

Rob Bajorek
Rob Bajorek

Reputation: 6560

You can use:

git reset HEAD^

Your last commit will be undone, but the changes in that commit will remain on the filesystem.

Upvotes: 2

poke
poke

Reputation: 387707

Two options, based on what you want to do (it’s not entirely clear). Either you want to keep the working directory as it is, but want to move the current branch back to the previous commit.

This can be easily done using git reset:

git reset HEAD~1

You can also use the --soft option to even keep the index as it is:

git reset --soft HEAD~1

If you want to keep the branch pointing to the commit it currently points to but you want to actually revert the files to the version of the commit before, then you can do that too. You can use git checkout to checkout the working directory from the previous commit without detaching into that commit:

git checkout HEAD~1 -- .

Basically by specifying a path (after the --) you are telling Git to not actually check out the commit/branch, but just to check out the state for all the files you listed where . means everything relative to the current directory (so to checkout everything, you should be in the root of your repository).

Upvotes: 1

Claudio
Claudio

Reputation: 10947

I don't know the reason why you should do this...

Anyway, a simple solution is:

  • Copy the whole directory in some other location
  • Use git reset --hard commit_nb to reset the git history to the previous commit
  • Overwrite files from the copy to your current path (paying attention to overwrite just files, without the .git/ directory)

Upvotes: 0

Related Questions