Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switching branches safely in git

Tags:

git

I'm on the master branch, and want to work on a new feature so I do:

git checkout -b new_feature

Now I am coding, doing a git status shows the files modified.

If I try and move to the master branch it says I can't b/c of the files that were updated.

ok so i'll add them:

git add -A

now if I try and jump to the master branch, it seems to be wanted to merge???

git checkout master

What I want to know how to do is:

  1. while in another branch, I want to stop what I am doing and move to the master branch, not merging anything yet, I just want to stop working on the new feature branch and go back to master.
like image 700
Blankman Avatar asked May 24 '11 21:05

Blankman


2 Answers

You've got two choices (at least). A "work in progress" commit, or the stash.

A "work in progress" commit is just that: it's a commit in a branch that represents unfinished work:

$ git commit -am'WIP (description here)'
$ git checkout master

Later, when you come back to your branch, you can continue working and committing:

$ git checkout mybranch
# do work
$ git commit -am'Finish WIP (description here)'

When done, you can merge your "WIP" commits together, if you want, into coherent commits with no evidence that you committed partial work:

$ git rebase -i HEAD~2    # See instructions for rebase -i elsewhere

Or, you can use the stash:

$ git add .
$ git stash save 'WIP (descriptino here)'
$ git checkout master

Later, when you get back to your branch:

$ git checkout my_branch
$ git stash pop

You'll be right where you left off.

like image 77
Wayne Conrad Avatar answered Sep 27 '22 16:09

Wayne Conrad


git stash may be what you are looking for.

like image 38
Seth Robertson Avatar answered Sep 27 '22 17:09

Seth Robertson