Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File content in Sublime does not change on Git branch change

If a file is open in Sublime text 2, and then branch is switched which has changes to the file in the terminal, the file contents are not updated in the Sublime text 2 until the editor is selected by clicking on it.

How to auto-magically change it?

like image 268
Gaurav Agarwal Avatar asked Aug 20 '13 18:08

Gaurav Agarwal


People also ask

Does switching branches change files?

When you switch branches, files that are not tracked by Git will remain untouched. Since Git does not know about new_file.

Does git branch change files?

Git doesn't touch the file. The file in the old commit and in the new commit are different. Git updates the file from the new commit. The file in the old commit exists and in the new commit it's absent.

How do I change the branch and keep changes?

You can use the git switch - command to undo any changes you make and return to your previous branch. If you instead want to keep your changes and continue from here, you can use git switch -c <new-branch-name> to create a new branch from this point.

What happens when you switch git branches?

When you switch a branch, all files that are under control of Git will be replaced with the state of the new branch. That includes changes to files as well as additions and deletions. In your case this means that you have some files in your current 'local' branch that simply do not exist in the master.


1 Answers

Sublime Text does not have any built-in way to do this. Though perhaps a Sublime Text plugin could do it by combining the file refresher plugin in the forum thread with a Python equivalent to watchr.

This Sublime Forum thread asks how to make a file in a different tab update automatically, given that Sublime Text is already frontmost. The asker couldn't find a way to make Sublime Text do that, but as a workaround, wrote a plugin that lets them hit a hotkey to reload all open tabs.

You could try to solve this with the same principle, by making a system-wide hotkey to switch to ST2 and back. You could implement the hotkey with AutoHotkey for Windows or Quicksilver or Keyboard Maestro for Mac. It's not as good as automatic updating, but it's the next best thing. You could hit the hotkey every time you expect the file to have changed.

You could even use a tool like watchr to monitor for changes in the files you plan to edit, and automatically triggers an AutoHotkey script or AppleScript that switches to Sublime Text and back when a file is updated. It would be even better to dispense with the GUI scripting and using Sublime Text's Python interface to reload the view without switching to it, but I don't know if Sublime Text allows you to run Python in the app from a program outside the app.

like image 83
Rory O'Kane Avatar answered Sep 19 '22 19:09

Rory O'Kane