Sublime Forum

Revert Hunk


One feature that “Sourcetree” (software I was using before Sublime Merge) had that I miss is to quickly revert hunks from the UI when looking at a commit or ranges of commit.

It would be similar to discarding a whole file or a hunk in the working directory mode (or staging):

It is one of the few features that I think are missing from Sublime Merge, or it’s there and I don’t know how to do it yet.



If I understand what you’re asking here, I think this sort of thing is possible as a part of a rebase operation on a commit. If you right click on a commit you can choose Edit Commit > Edit Commit Contents (also available from the “three dots” menu at the top right of the commit message display area.

When you choose that option the working directory gets set back to the state prior to that commit being created, so you could change what is considered part of that commit such as above, and then finish the rebase.

I’ve never used SourceTree myself but I would imagine that it would be doing something similar behind the scenes but just wrapped in a visually different workflow; Merge on the other hand adheres fairly closely to how you’d use git if you were doing actions manually.



How about this, if I wanted to revert one whole file, not a hunk, and let’s assume I don’t want to re-write history (which, even though I haven’t tried, it’s what your alternative is proposing right?)

One way of doing it with git commands would be to run:

>>> git revert -n <hash> 
>>> git reset HEAD .                           # Unstage the changes (since git stages all)
>>> git add <file you want to revert>  # Only get the file you wanted to revert
>>> git checkout -- [base of repo]       # Discard all the other un-staged changes, so that only the file you wanted to revert is kept

So at least for whole files, it can be done with commands alone, although, what I have above does not work if your working directory/staging area is dirty. I don’t know if there is a perfect solution at that.

What do you think?



Ahh sorry, I was thinking a little too literally there. A rebase would indeed rewrite the history.

One of the items in the context menu for commits is Revert Commit, which does an implicit git revert --no-commit on the commit that you use it on. Once you do that you can use the buttons on the right (i.e. the ones from your image above) to unstage/discard as needed and commit the results back. So that may do what you want.

1 Like