larryq
larryq

Reputation: 16309

Moving from one branch to another without committing changes

I believe this is what git stash is for, but want to confirm...

I'm working on a couple of topic branches from master. I occasionally need to jump back and forth between them (master included) and pull the latest from our remote, make some changes, then jump back to the other branch.

If I've worked on file that's shared between branches, I can't simply run git checkout <branch> to move between them. I'm told I must add & commit my changes first. I'd prefer not to do that sometimes and was wondering about good ways to go about moving from branch to branch as independently as possible, committing only when desired?

Upvotes: 1

Views: 5199

Answers (1)

Gary Fixler
Gary Fixler

Reputation: 6048

Yes, that's what stash is for. It adds and commits the index and working tree to a new commit and merge commit. Then you have a clean working tree, and you can cleanly checkout any other branch to work on. Then on any branch - not just the one where you stashed it - you can git stash pop to move that stash back into the work tree and index, and drop the stash. Usually you would head back to the original branch and pop there to continue where you left off, but sometimes it's helpful to be able to stash and move elsewhere to avoid a conflict, which stash might be able to circumvent.

Definitely read up on it (git help stash) - there are other options, such as working on only the work tree or index, or adding a message to it so it's easy to remember why you made the stash later, etc.

Upvotes: 2

Related Questions