Paul Manta
Paul Manta

Reputation: 31567

Switch branches without losing uncommitted work

Is there any way to switch from one feature branch to another, when the changes in the current feature branch are not yet ready to be committed?

I often find myself in a situation where I'm out of ideas when implementing a new feature or fixing a bug and I need to switch gears and work on something else. However, I do not know how to switch branches without losing all the uncommitted work I did up until that point.

Upvotes: 5

Views: 968

Answers (1)

poke
poke

Reputation: 387547

You basically have two options:

  • Stashing: You can use git stash to save all the changes and keep them somewhere save. Afterwards, you will end up with a clean working directory so you can switch branches. Then, when you come back, you can use git stash apply to apply the changes from the last stash, so you’re back at where you left off.
  • Commit: There is actually no reason why you couldn’t commit the changes. If you later want to change the commit, you can either use git commit --amend to amend that commit and “make it better”, or you can use git reset --soft HEAD~1 to remove that commit (without losing the changes you did).

Upvotes: 6

Related Questions