Search code examples
gitgit-stash

Git stash/whatever on every git switch?


Can we have such a functionality, let's call it git switch-for-humans that will make me forget about stashing/switching/committing/amending/etc. (and choosing the right one of them every single time) once and forever:

  1. When I'm at branch A and I want to switch to whatever branch B I just do: $ git switch-for-humans B At this point my working tree whatever state it is in (with all the tracked/untracked/etc.files, just as it is) is magically stashed or saved at some storage specific for branch A
  2. I get to branch B.
  3. I do any kind of switching
  4. At some point by calling "git switch-for-humans A" I arrive back to A and the working tree gets magically restored.

This saving/restoration works for every branch, w/o any additional actions from user's side.

Is there some known way to get such behavior from git?..


Solution

  • A simpler approach, that your git switch-for-humans can include, is to use multiple working trees with git worktree.

    Each worktree is a branch checked out, where you can work, and amend/commit files.

    Each git switch-for-humans would simply cd (change directory) to the right worktree, or create it if it does not exist.
    It can even change a symlink in order for your project (possible opened in an IDE) to automatically refer to the right worktree/folder.