9

I am working on a feature branch and have not finished the work there - Now I need to change to a different branch to fix something

for example

feat1 - 6+ files changed

When I checkout to feat2 branch, after git add . in feat1, git seems to carry over the staged yet uncommitted file changes.

If I commit these file changes in feat1, checking out to feat2 will not carry over those changes

How can I switch branches without committing file changes?

3
  • 1
    Use this command GIT stash Commented Feb 2, 2014 at 20:56
  • 1
    @ZachRussell: It's git stash, not GIT stash. Commented Feb 2, 2014 at 21:11
  • Yes my stupid phone is autocorrecting :/ Commented Feb 2, 2014 at 21:12

4 Answers 4

15

Stash them:

$ git stash save -u "Some logical description of the changes"
$ git stash list
stash@{0}: Some logical description of the changes
$ git checkout other-branch

When you're done, you can use git stash apply to apply the changes in your stash and keep the stash around, or git stash pop to apply the changes and remove the stash as long as there are no conflicts.

If you end up with multiple stashes at once you can apply or pop them in an arbitrary order using the stash@ string, e.g. git stash apply stash@{6}.

5
  • but doesn't stash work like a stack? I must stash pop to revert back. Won't this be a problem if I work on multiple feature branches? Commented Feb 2, 2014 at 20:59
  • @NickGinanto, you can apply or pop arbitrary stashes. See my updated answer.
    – Chris
    Commented Feb 2, 2014 at 21:02
  • 1
    Yes, stash defaults to stack-like behavior, but you can specify a specific item on the stash: git stash list prints them as stash@{1} etc, and you can apply and drop a specific one.
    – torek
    Commented Feb 2, 2014 at 21:02
  • is that the "best-practice" approach to work on several branches at once? or is the git way is to work and finish working on a certain branch? Commented Feb 2, 2014 at 21:04
  • @NickGinanto, we're getting into opinion here. git stash is a tool; use it when you feel it's appropriate. You can also use git commit and optionally rewrite your history later, or create a temporary branch, or...
    – Chris
    Commented Feb 2, 2014 at 21:06
5

Most people recommend git stash.

I prefer just doing a git commit. You can always git commit --amend later. Just make sure not to push that particular commit. If it helps—and for me, it does—just make a branch for "work in progress on feature". For instance:

$ git checkout zorg
Branch zorg set up to track remote branch zorg from origin.
Switched to a new branch 'zorg'
... work ...

At this point, I realize I need to save the work-so-far and go do something else:

$ git checkout -b zorg-stones-1
Switched to a new branch 'zorg-stones-1'
$ git commit

Now everything is all nicely saved away on a local branch that I have named in a way that helps me remember what I was doing, when I come back later.

Often, I come back later to find that origin/zorg has been updated, so:

$ git fetch
[shows that origin/zorg is updated]
$ git checkout zorg && git merge --ff-only origin/zorg
[now local branch zorg is updated too; --ff-only is just for paranoia]
$ git checkout zorg-stones-1
$ git rebase zorg
[rebase messages here]

If the rebase does not go well (or I need to rework things), I use git rebase --abort (or just skip the rebase attempt) and then start a new zorg-stones-2 branch based on the updated zorg. When I'm good about it, I commit often enough and don't have to do a lot of git rebase -i zorg to fix it up a whole lot before doing git checkout zorg; git merge zorg-stones-N to bring in the final version, ready to git push or whatever.

I often have a bunch of blah-mods-N branches to (eventually) delete this way. It's definitely true that stashing is less branch-clutter-y. But I prefer to have everything I did accessible by name up until I deliberately toss it out.

3

You should use git stash

http://git-scm.com/book/en/Git-Tools-Stashing explains your exact case-scenario

1

Even if you did not finish your work I would suggest committing them.

Committing is always a good idea. - However, pushing is usually only a good idea when you are finished.

If your commits are not perfect you can always clean them up or combine them using something like git rebase -i before pushing them.

Not the answer you're looking for? Browse other questions tagged or ask your own question.