Hi programmers,

I work from two computers: a desktop and laptop. I often interrupt my work on one computer and continue on the other, where I don’t have access to uncommitted progress on the first computer. Frustrating!

Potential solution: using git to auto save progress.

I’m posting this to get feedback. Maybe I’m missing something and this is over complicated?

Here is how it could work:

Creating and managing the separate branch

Alias git commands (such as git checkout), such that I am always on a branch called “[branch]-autosave” where [branch] is the branch I intend to be on, and the autosave branch always branches from it. If the branch doesn’t exist, it is always created.

handling commits

Whenever I commit, the auto save branch would be squashed and merged with the underlying branch.

autosave functionality

I use neovim as my editor, but this could work for other editors.

I will write an editor hook that will always pull the latest from the autosave branch before opening a file.

Another hook will always commit and push to origin upon the file being saved from the editor.

This way, when I get on any of my devices, it will sync the changes pushed from the other device automatically.

Please share your thoughts.

  • MajorHavoc@programming.dev
    link
    fedilink
    arrow-up
    11
    ·
    4 months ago

    I set that up, once. It went poorly for me. Git behaves much better, for me, when used thoughtfully and manually.

    What I now do instead, is work on certain projects on an SSH accessible host. This gives the same benefits of having my last state easily accesses, without causing noise in my development tools such as git.

    • Danitos@reddthat.com
      link
      fedilink
      arrow-up
      3
      ·
      4 months ago

      If working on Linux, combine SSH with tmux (and the attach/detach commands) and you have a very solid workflow. Learning tmux has been one of the best tools of the year for me.

  • xmunk@sh.itjust.works
    link
    fedilink
    arrow-up
    8
    ·
    4 months ago

    Git doesn’t need to have a single pull source. It’s probably worth just configuring the visibility on each machine so you can do peer pulls.

    I don’t hate the idea of autocommitting in that scenario, though.

  • Kissaki@programming.dev
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    4 months ago

    I would consider three four approaches.

    1. Commit and push manually and deliberately

    I commit changes early and often anyway. I also push regularly, seeing the remote as a safe and remote (as in backup) baseline and reference state.

    The question would be: Do I switch when I’m still exploring things in the workspace, without committing when switching or moving away from it, and I would want those on the other PC? Then this would not be enough.

    2. Auto-push all local git references into a separate space on the git remote

    Git branches are refs, commit pointers, just like other refs are. And they can be put under arbitrary paths. refs/heads/ holds branches. I can replicate and regularly update all my branches under refs/pcreplica/laptop/*. And then on the other PC, list or fetch those, individually, or all of them, regularly automatically, or manually.

    git push origin refs/heads/*:refs/pcreplica/laptop/*
    git ls-remote
    git fetch origin refs/pcreplica/laptop/*:refs/laptop/*
    

    3. Auto-push the/a local branch like you suggested

    my concern here would be; is only one branch enough? is only the current branch enough?

    4. Remoting into the other system

    Are the systems both online? Can I remote into / connect into it when need be?

  • GetOffMyLan@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    4 months ago

    Honestly I would just commit your in progress work then on the other machine check it out and reset to the previous commit.

    Then you have your in progress work on the new machine with no random commits.

    You could set up an alias that does commit with message “switching machines” and pushes.

    Similar have one that pulls and resets.

    • matcha_addict@lemy.lolOP
      link
      fedilink
      English
      arrow-up
      2
      arrow-down
      1
      ·
      4 months ago

      That doesn’t solve the problem of forgetting :(

      I could train myself to get in the habit, but maybe auto saving is easier, no?

      • zlatko@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        4 months ago

        I wonder if JJ anonymous branches would be something that solves this. I’ve only read about it, have not used JJ yet.

  • Nighed@feddit.uk
    link
    fedilink
    English
    arrow-up
    1
    ·
    4 months ago

    For my personal projects I somehow ended up with git being on a OneDrive synced folder - carries over the general changes, then explicitly commit and push to get it to GitHub etc.

  • Drew Belloc@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    4 months ago

    You could also use syncthing so when you are conected to your lan you work folder will always be synced from one pc to another, that way you won’t have a bunch useless commits on git and you can still commit and push the changes from any computer

    • matcha_addict@lemy.lolOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 months ago

      When I looked into solutions, I thought of syncthing, but read comments from people saying they had issues with this approach, especially regarding the .git directory

  • Matty_r@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    4 months ago

    I used to have a similar situation, I used Vscode remote development to effectively work from any machine. Another thing I tried was using Nextcloud to watch the working directory, which automatically synchronized files when they change.