Monday, March 29, 2010

git stash

git stash is really neat for working with different branches. Lets same I'm in one developer branch in the middle of something and I want to switch branches to save something. Well rather than having to make a temporary branch so I don't lose my work and I can use git stash.

If we make a change to a file and then try to change branches we get this

santiago@santiago-desktop:~/arduino-0016/hardware/libraries/USB_Host_Shield$ git checkout master
error: You have local changes to 'Max_LCD.cpp'; cannot switch branches.

So what we do is run git stash, which tucks things away onto a local stack

santiago@santiago-desktop:~/arduino-0016/hardware/libraries/USB_Host_Shield$ git stash
Saved working directory and index state "WIP on dev: 0209cfa... changed MAX3421E to Max3421e for case sensitive compilers"
HEAD is now at 0209cfa changed MAX3421E to Max3421e for case sensitive compilers
(To restore them type "git stash apply")

santiago@santiago-desktop:~/arduino-0016/hardware/libraries/USB_Host_Shield$ git checkout master
Switched to branch "master"
//inspect changes, now I need to switch back

santiago@santiago-desktop:~/arduino-0016/hardware/libraries/USB_Host_Shield$ git checkout dev
Switched to branch "dev"

santiago@santiago-desktop:~/arduino-0016/hardware/libraries/USB_Host_Shield$ git stash pop
# On branch dev
# Changed but not updated:
# (use "git add ..." to update what will be committed)
#
# modified: Max_LCD.cpp
#
# Untracked files:
# (use "git add ..." to include in what will be committed)
#
# Max3421e.o
# Max_LCD.cpp~
# Max_LCD.o
# Usb.h~
# Usb.o
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (da0cd6de12e2128986ffdfe053f0c81cb5b264b3)

This makes a complicated situation much easier!

No comments: