0

Changing a branch with git checkout usually takes uncommitted changes to the new branch (and tries to merge them in, or fail).

Sometimes I'm working in a feature branch, and I am not quite ready with whatever I'm working on to commit them, but need to switch branches because I need to work on something else.

If this is just a "quick look" stashing, changing branches there and back and unstashing works.

If this is something that requires more time (in some cases two days or even a week), stashing is not really a good option, as other stuff may get stashed, so it's not exactly straight-forward to find which stash I am looking for), and even after changing back to the old branch, sometimes I even forget that I had a half-solution already. Committing everything as a "wip" commit, and then later soft-resetting or amending that would be an okayish workaround, but it's still a workaround, and again may be forgotten that a wip commit is there, messing up the git log, unless one again spends time and effort to rebase stuff.

So the question is:

Is it possible to change branches, without committing changes, and without taking them to the new branch? And when changing back, the changes would be there either staged or unstaged?

Ákos Vandra-Meyer
  • 1,890
  • 1
  • 23
  • 40
  • The wip commit to be later reset is my personal favorite, but depending on the repo size and other factors, you might prefer a multiple worktrees strategy. Let one worktree in its present state and work on another branch in an additional [worktree](https://git-scm.com/docs/git-worktree). – Romain Valeri Mar 10 '22 at 08:44
  • I've never used them, but worktrees might be able to do this. But typically I will just either `git stash` or WIP commit, and there's nothing wrong with doing that. (Note: stash itself makes 2 or 3 commits under the hood, so it's really a WIP commit in disguise). – Tim Biegeleisen Mar 10 '22 at 08:44
  • I think you have 3 options: Commit as WIP, stash and have multiple working trees. My choise is always WIP commit, I don't think it's that luch more extra work, you can check [this answer](https://stackoverflow.com/a/67823541/11261546) to see a bit of a workflow – Ivan Mar 10 '22 at 08:46
  • You should look into using worktrees, then you can have multiple branches checked out at the same time in different directories, from the same local repository. And no, with 1 working folder, you can't check out a different branch and not bring your uncommitted changes. What should git do with them to keep them until you come back? That's what stashing is for. – Lasse V. Karlsen Mar 10 '22 at 08:48
  • `git worktree` is almost certainly your best approach here. Make sure your Git is at least version 2.15. For more background, see also [Checkout another branch when there are uncommitted changes on the current branch](https://stackoverflow.com/q/22053757/1256452). – torek Mar 11 '22 at 00:49

1 Answers1

1

I don't think that is possible using just git branch. You should, however, be able to use git worktree in order to have multiple versions of your repository checked out in different directories.

Here's a link to the documentation for git worktree: https://git-scm.com/docs/git-worktree

Frost
  • 11,121
  • 3
  • 37
  • 44