1

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?..

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
lithuak
  • 6,028
  • 9
  • 42
  • 54
  • 1
    Well, I suppose that would be pretty cool to have! But... including untracked files? That's pretty ambitious. What if they're git-ignored? What about files marked assume-unchanged? I suspect there are too many edge cases for this to be possible. – joanis Jan 08 '22 at 19:00
  • @joanis good questions! I guess we would have options for that, like we have them for almost everything in git already ) The thing is - I really thought somebody had to already come up with some solution to this based on some hooks/named stashes/something (which btw have those options for untracked, etc.) that I'm just not aware of. – lithuak Jan 08 '22 at 19:04
  • I don't think this exists simply because human A wants it to do thing A, human B wants it to do thing B, human C wants it to do thing C, and so on. If you find a large enough group of humans that all want the same thing, you'll probably find someone who has done it, but I'm definitely not going to be the guy who makes the assumptions about what humans want. :-) – torek Jan 08 '22 at 23:44

1 Answers1

3

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250