git-plus icon indicating copy to clipboard operation
git-plus copied to clipboard

Undo Last Commit

Open michaeljonathanblack opened this issue 8 years ago • 7 comments

My workflow often involves committing changes to feature or bugfix branches, and it's easy to accidentally commit to the wrong branch.

It'd be nice if there was an easy command to perform the task of undoing a recent commit and unstaging those changes, all in one go.

Undo Last Commit: git reset HEAD~1

Edit: git reset HEAD@{1} is safer as it can be called multiple times and merely undoes and redoes itself.

The best implementation would actively check for a previous commit before naively running, but hey.

michaeljonathanblack avatar May 09 '16 00:05 michaeljonathanblack

+1

saidmoya12 avatar May 23 '16 15:05 saidmoya12

For now you can use the Run command to do this.

akonwi avatar Dec 09 '16 02:12 akonwi

I've just published a new version with a feature that allows users to define their own commonly used commands locally. It's an experimental feature right now and I would love if you gave it a try and provided feedback.

You can read about it here

akonwi avatar Dec 28 '16 18:12 akonwi

I will be trying it, thanks a lot!

michaeljonathanblack avatar Dec 29 '16 00:12 michaeljonathanblack

That works fantastically! Thanks again.

I updated the undo command to be safe to call multiple times (now it undoes and redoes itself infinitely):

atom.packages.onDidActivateInitialPackages (p) ->
  if gitPlus = atom.packages.getActivePackage('git-plus')?.mainModule.provideService()
    # Undo Last Commit
    # This reset targets the previous line in the git reflog, so it can be called again to undo itself.
    gitPlus.registerCommand 'atom-text-editor', 'akonwi:undo-last-commit', ->
      gitPlus.getRepo() # If there are multiple repos in the project, you will be prompted to select which to use
      .then (repo) -> gitPlus.run repo, 'reset HEAD@{1}'

Curious, why do commands require the akonwi: prefix (your user name) when being registered?

michaeljonathanblack avatar Dec 29 '16 22:12 michaeljonathanblack

The prefix can be whatever you want. It's just an identifier to keep commands unique from another package that might use the same name.

akonwi avatar Dec 30 '16 00:12 akonwi

Sweet, thanks again.

michaeljonathanblack avatar Dec 30 '16 00:12 michaeljonathanblack