Git Branch Switching/Stashing?

93 views
Skip to first unread message

Ken Snyder

unread,
May 24, 2012, 3:06:20 PM5/24/12
to uta...@googlegroups.com
Is there any way to take all the uncommitted changes on the current branch and move them to another branch? The use case is that I commit to a branch named like "My-Feature" and then merge with "develop" when the feature is complete. The problem is that sometimes I do some work thinking I am in the "My-Feature" branch but then I realize I was really in the "develop" branch.

I want to move all of the uncommitted changes to "My-Feature" and then switch to the "My-Feature" branch and continue working.

Maybe something with `git stash`?

Thanks,

Ken

Merrick Christensen

unread,
May 24, 2012, 3:17:33 PM5/24/12
to uta...@googlegroups.com
I thought your working tree came with you by default when you switched branches?

--
Lunches rotate between cities every Wednesday.
Lindon: first Thursday of every month: http://utahjs.com/meetings/
 
----
Unsubscribe: utahjs+un...@googlegroups.com
Options: http://groups.google.com/group/utahjs

Sean Hess

unread,
May 24, 2012, 3:24:14 PM5/24/12
to uta...@googlegroups.com
They do come with by default. "git checkout x" will work. 

That will work unless there is a conflict, in which case use "git stash", switch branches, then "git stash apply" and you can merge by hand

Ken Snyder

unread,
May 24, 2012, 3:25:44 PM5/24/12
to uta...@googlegroups.com
Ah. I've been using GitHub for OSX and It apparently keeps the working changes tied to the branch.

I see if I do `git checkout branch` it works as you say!

Thanks

Ryan Florence

unread,
May 24, 2012, 4:47:24 PM5/24/12
to uta...@googlegroups.com
First, make your life easier and alias checkout to co:

git config --global alias.co checkout

Then simply:

git co my-topic

If the branch is new then

git co -b my-topic

If you accidentally committed it to the wrong branch (oh no!)

git reset --soft HEAD^
git co my-topic

First command removes the last commit but leaves the files in-tact (like you never committed)

Git stash is most useful when you need to pull from a remote (it creates a "temporary" commit).

git stash
git pull --rebase origin <branch>
git stash pop

Ken Snyder

unread,
May 24, 2012, 4:53:52 PM5/24/12
to uta...@googlegroups.com
Thanks. That is really helpful.

Marcos Minond

unread,
May 25, 2012, 7:44:26 PM5/25/12
to uta...@googlegroups.com
You can commit (and push) those changes to "My-Feature" and the cherry pick that commit over to any other branch.

git commit -am "bug fixes"
git checkout develop
git cherry-pick [your commit's hash]
git push
git checkout My-Feature 


And don't checkout before stashing or committing, you'll lose your changes.
Reply all
Reply to author
Forward
0 new messages