Reputation: 1926
When working with multiple feature branches in parallel, I need to stash
or commit
the local changes before I can switch to another branch. I think both stash and commit feel rather clumsy to me. Is there a more elegant alternative?
Why stash
is clumsy: I work on multiple branches and switch between them from time to time. Sometimes I end up forgetting to pop, or popping the wrong stash.
Why commit
is clumsy: my changes are work-in-progress. They are not complete and should not be committed. What I end up doing is committing them first (because I'm forced to), and later doing a soft reset so that my previous commit comes back to "local changes". Or, sometimes I let the temporary commits pile up and do an interactive rebase later.
What I'm looking for: like a "smart stash". Stash the local changes automatically when I switch to a another branch, and pop the stash associated with this branch automatically when I switch back. Could this be done?
Upvotes: 2
Views: 930
Reputation: 11
My solution is to create a temporary commit for work in progress, and for each noncommittal save of work status before switching branch, always use git commit --amend
. The --amend
flag lets you create a commit that replaces the current-branch-pointed commit (in our case, an unserious, work-in-progress commit), rather than create additional commits on top of it. Each time you commit with --amend
, your commit message could actually be "NO COMMIT" to remind yourself of that. Then you can forget about all the complications of Git, and just think of commit --amend
like the good old SAVE button that lets you overwrite your previous file (the non-serious commit) again and again, leaving no additional traces.
When you've worked enough and decide this unserious, status-saving commit should become a real commit, then just git commit
without --amend
, then on top of that, create the next unserious commit with the message "NO COMMIT"
I guess this is the easiest way to save work-in-progress for people who have to jump between different branches often.
Upvotes: 1
Reputation: 1323403
Another approach would be to use git worktree
, which allows you to have multiple working directories
That way, switching branches can mean simply leaving your current work in progress as is, and.. changing folder.
No need to automatically stash/commit.
The drawbacks of that approach could be:
Upvotes: 0