I have a file open in Sublime.
Now I want to discard its current changes. Is there a fast way to do this with sublime or sublime merge?
I don’t want to have to scroll through my working directory changes and find the file that way.
I have a file open in Sublime.
Now I want to discard its current changes. Is there a fast way to do this with sublime or sublime merge?
I don’t want to have to scroll through my working directory changes and find the file that way.
File: Revert reverts unsaved changes within the ST editor, not within your git repository. I’m not aware of a built-in method to do this, but it is easily achieved through a plugin in ST like https://github.com/timbrel/GitSavvy and in Merge you can easily add a menu item by following the docs (though at that point you might as well just press “Discard”, which is also a context menu entry in the “Contents” tab).
Thanks FichteFoll.
The problem with just discarding from Sublime Merge, is that there is not a nice efficient way to get from the open file in sublime text, to discarding in sublime merge.
In addition, in sublime merge there is a not a good way to parse or filter my unstaged changes.
So if I have 200 changed files…it can be sort of annoying to manually search through the files to find the one I want to discard. This is a recurring theme for me. I have switched from Git Extensions, to sublime merge, and my git productivity has plummeted
With Sublime Merge installed, can’t you just right click the file in Sublime Text and select File History...
The file history view does not have an option to discard working directory changes.
I personally use the snapshot feature in the package Sublimerge Pro (not to be confused with Sublime Merge) if I want to be able to revert back to a certain point without going through Git, I think it also has a feature to automatically take that snapshot when you open a file.
If you’re using the built in git integration (i.e. mini_diff
is true
or "auto"
in your user preferences) then you should be able to use that functionality to put the file back to the original state. The Edit > Text > Revert Modification
command will revert all of the modifications covered by the current selection state back, so if you select everything in the file and then use that command, everything should revert.
For example, given this User/complete_revert.sublime-macro
macro file:
[
{ "command": "select_all" },
{ "command": "revert_modification" },
{
"command": "move",
"args":
{
"by": "characters",
"forward": false
}
}
]
Invoking the macro from the Tools > Macros
menu would revert all of the changes in the current file and leave the cursor at the first line for you. You can also bind it to a key if you like:
// Revert the current file back to the state it was in when the file
// was first opened or to the latest git commit (depending on the
// file). Requires that the mini_diff setting be enabled and (for
// git) that git_diff_target be set to "head".
{
"keys": ["ctrl+k", "ctrl+k", "ctrl+z"],
"command": "run_macro_file",
"args": {
"file": "res://Packages/User/complete_revert.sublime-macro"
},
},
Similarly you could add it to the command palette or augment the menu, if you’d rather go that way.
This will also work for changes to files that aren’t tracked by git, though in that case File > Revert
would do the same thing anyway.
For this to work, the git_diff_target
setting needs to be set to head
and not index
; otherwise any partially staged changes will not be reverted because the incremental diff functionality doesn’t know that they exist.
I tried playing a bit with using the set_setting
command to flip the state of the setting, but it doesn’t have any effect unless you do it manually. I would imagine that’s because Sublime needs time to notice that the setting changed so that it can update the state of the incremental diff. For that I think you’d need a simple plugin to do this that imposes a delay.
Note also that reverting the file content doesn’t do anything to what git thinks is happening in the file. So for example even when the diff target is set to "head"
, if you stage some lines and then use this to revert the file, the content changes but Git still thinks that things are staged.
That shouldn’t be too much of a problem if your workflow is to stage things prior to commiting them, though. When in doubt you can always do a quick Goto > Next Modification
to make sure that there aren’t any.