Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to prevent accidental overwrite after git pull due to editor not updating automatically?

When a file is open in an editor (say Sublime or Atom), and the file is edited outside the editor, the editor invariably refuses to refresh the file it displays. This situation occurs rarely, as most likely one will only use a single tool to edit the file in a particular time frame. This is obviously not an issue when the file is read only. For example, when reading a system error log, the file will be updated as the system is running and may have a new error log, but the log file will not be edited so it does not cause a conflict.

However, it causes problems when the file is updated by git pull.

When a person pulls the latest updates to the repo, he might have a file open in an editor to which some changes have been made in the update. If the editor fails to refresh the file, the file is saved with the old contents, and any new changes are lost.

Sometimes, it is just annoying to reverse hunk using sourcetree, but when there are a number of files updated, that overwrite may be pushed to git server unnoticed — until an error occurs. Currently we are using git log --follow -p -- file command to locate and revert the error, but it is not possible if the overwrite is not noticed in time,forcing us to copy the lines manually. Is there a way to prevent this kind of overwrite in the first place?

like image 884
cytsunny Avatar asked Oct 28 '16 03:10

cytsunny


People also ask

Will a git pull overwrite my changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.

What command would you use to force an overwrite of your local files with the master branch?

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches.


2 Answers

When a folder is opened by an editor (mainly Sublime or Atom in our team), and the code is edited outside the editor, sometimes the content in the editor is refreshed, but sometimes it doesn't.

This is precisely what is discussed in Atom issue 3594

An Atom package like file-watcher would help mitigate this issue, by prompting to reload each file if a modification outside the editor is detected.

You have the same issue reported in this thread for SublimeText. As mentioned in that thread, the issue is even more relevant on Windows when accessing a file over a network share.
File Reloader can help but does not detect external changes.

The SublimeText thread mentions the setting (2016)

{ "always_prompt_for_file_reload": true }

But that might not help when there are changes both in the editor and in the saved file: An editor like Visual Studio Code solved this with:

If there are changes on both sides (from disk and through the editor) when ever you try to save the file using VSCode, the editor will warn you about that situation and a file comparison will allow you to decide what to do.

That is why, with SublimeText (in addition of the "always_prompt_for_file_reload" setting), you might need the FileDiff plugin.
It does allow for diffing file with Saved:

https://forum.sublimetext.com/uploads/default/original/3X/5/a/5a59f922e26fea423cc31ae5c1d744eca8205143.png

like image 169
VonC Avatar answered Sep 23 '22 13:09

VonC


Is there a way to prevent this kind of overwrite at the first place?

Yes, as your editors are failing to do the detection by interruption from the file system, then to perform them by polling the file system periodically as each 3 seconds.

When programming on Notepad++ this failure is very evident, this is why a polling plugin which periodically checks the file system for changes on the files is really necessary.

For Sublime Text and Notepad++, the plugins are:

  1. https://packagecontrol.io/packages/Auto%20Refresh
  2. https://superuser.com/a/592913/458103 (Document monitor => start to monitor)

I used them for a long time, due to program with the same file on several text editors/IDE's. Then when alternating between the editor, I most times loose my work due them do not reloading the files from the file system. However after installed the mentioned plugins, I never faced this problem again or noticed performance issues due the periodic reloading.

like image 29
user Avatar answered Sep 24 '22 13:09

user