Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio code: automatic commit git

Many times, I forget to commit my edits to my git. And if I closed VSCode, I can't use ctrl-Z anymore. Because, I have git set up, I thought I could use something like an automatic commit every 30sec or so.

I have seen this extension btu ti's not open source, so I don't want to use it. I also found this but my programming skills aren't good enough to implement it.

I also wonder if using git in this way is a good option, as the git folder may get really heavy quickly (my git is only local, so I can handle a couple of gigab)

So what is the best way to achieve what I want to do?

like image 572
MagTun Avatar asked Dec 06 '22 10:12

MagTun


2 Answers

2018:

I also wonder if using git in this way is a good option

No, that would pollute the history of your commits.

Using the extension "Local history" is a better approach, and does not rely on your VCS.
(and is open-source: github.com/zabel-xyz/local-history)

Every time you modify a file, a copy of the old contents is kept in the local history.
At any time, you can compare a file with any older version from the history.
It can help you out when you change or delete a file by accident.
The history can also help you out when your workspace has a catastrophic problem.


2022: No need for an extension with VSCode 1.66 (March 2022).
VSCode now includes:

Local history

Local history of files is now available from the Timeline view. Depending on the configured settings, every time you save an editor, a new entry is added to the list:

Local History

Each local history entry contains the full contents of the file at the time the entry was created and in certain cases can provide more semantic information (for example, indicate a refactoring).

From an entry you can:

  • Compare the changes to the local file or previous entry.
  • Restore the contents.
  • Delete or rename the entry.

There are new global commands to work with local history:

  • workbench.action.localHistory.create - Create a new history entry for the active file with a custom name.
  • workbench.action.localHistory.deleteAll - Delete all history entries across all files.
  • workbench.action.localHistory.restoreViaPicker - Find a history entry to restore across all files.

There are also new settings to work with local history:

  • workbench.localHistory.enabled - Enable or disable local history (default: true).
  • workbench.localHistory.maxFileSize - File size limit when creating a local history entry (default: 256 KB).
  • workbench.localHistory.maxFileEntries - Local history entries limit per file (default: 50).
  • workbench.localHistory.exclude - Glob patterns for excluding certain files from local history.
  • workbench.localHistory.mergeWindow - Interval in seconds during which the last entry in local file history is replaced with the entry that is being added (default 10s).

A new filter action in the Timeline view toolbar allows you to enable or disable individual providers:

Timeline Filter

Note: Local history entries are stored in different locations depending on your use of VS Code.

  • When opening local files, the entries are persisted in the local user data folder and
  • when opening remote files, they will be stored on the remote user data folder.
  • When no file system is available (for example, in certain cases when using VS Code for Web), entries are stored into IndexedDB.
like image 130
VonC Avatar answered Dec 08 '22 00:12

VonC


I know I'm probably an outlier, but I'm terrible at consistently committing to Git yet still use it as my #1 version control / core repo solution.

The interface and experience using Git as a single source-of-truth for your project is tough to beat.

So that said, I personally use a very simple Crontab solution to automatically push 'Auto-Commits' every 20 minutes. The comment on each automatic push is identical: 'Automatic Commit.'

This works GREAT (for me), specifically on projects where I'm the solo / primary contributor.

I prefer to use VSCode's SSH Remote option to work directly on a staging server, so setting up Git to automatically back-up the project every 20 minutes protects my project files and history.

The beauty of this solution is Git will only push a new commit if changes have been made. Also, I can still manually push new commits at any time and easily find those 'milestones,' because the auto-commits are all labeled 'Automatic Commit.'

To set up a similar workflow: (Linux only):

  • Important: set up Git Credential Storage and push a manual commit before completing the steps below. E.g., run this in your project folder where your git repo is: git config credential.helper store, then push a commit. This stores your credentials so the Cron job doesn't have to log-in. (Which it can't, because it's running in the background).
  • Then, run this command in the terminal: crontab -e
  • If prompted, select the Nano editor (user-friendly) or Vim if you prefer. Add the line below to the bottom of the file.
  • */20 * * * * cd /path/to/project/git/location && git add . && git commit -m "Automatic Commit" && git push origin master

You'll now see automatic commits in your Git repo (but ONLY if changes have been made to the project!).

Welcome to the future. Where Git is used completely the wrong way... but it feels good.

like image 34
quantdigital Avatar answered Dec 07 '22 22:12

quantdigital