Mike Lischke
Mike Lischke

Reputation: 53335

Switching branches each with local changes

Imagine there are 2 branches: master and feature. Now I work on a new feature and hence have that branch checked out. Later I need to work on master but cannot commit the feature changes since they are not yet in a good state. I can stage them and switch to master. I do some work there and suddenly I get an idea I need to test in the feature branch, so I want to switch to it, but again I have to stage the local changes in master.

Now imagine you have 10 instead of 1 feature branch. Is it the only way to stash all local changes when switching branches or is there a more intelligent solution?

Please don't question the work flow, it's just an expample to accentuate the annoying work - stash - checkout - work - stash - checkout - apply stash - work - stash - checkout - apply other stash -... cycle.

Upvotes: 2

Views: 56

Answers (3)

lesthi
lesthi

Reputation: 39

Instead of merely commiting locally, you may create new branches right before commiting, e.g:

[work]
$ git checkout -b sub_feature_branch
$ git commit [...]

# Now time to switch to other branch

$ git checkout other_branch
[work & repeat]

Doing so, not only you keep each mainstream branch (master & feature in your case) clean in case you change your mind later, but you also get the ability to postpone merge operations when applicable.

Upvotes: 2

Chris Mantle
Chris Mantle

Reputation: 6683

The solution is to check-in. Don't stash, just check-in whatever you have on the feature branch before you switch to another branch or to master. Leave a nice descriptive commit message. You're thinking in terms of centralized source control, where checking-in incomplete or unfinished work will cause problems for others. But this is Git - it's your local copy of the repository. You're inconveniencing no-one by checking-in, and you don't have to push changes to origin to check-out another branch.

Upvotes: 2

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72755

I don't understand why you don't go ahead and commit the changes on the feature branch as an "interim commit". You can always switch back, and do a git reset HEAD^ to undo the commit and continue working. I rarely use stashes myself.

Upvotes: 2

Related Questions