Git: how to use stash
softwarecave.org
softwarecave.org
git commit -a -m 'Work in progress'
Switch to other branch, do work on it. When you come back to the branch,
git reset --soft HEAD^
It's far less volatile than stash, and far better at keeping WIP in the right place when things get so hectic that you have abandoned work on more than one branch.
Stash is only really safe to use for very short-lived uses, such as "stash/pull/unstash"
To get back to where you started (changes not added to the index, since you are doing commit -a) you should do `git reset --mixed HEAD~` (or drop the --mixed argument as it's the default).
Personally, if I'm on a local branch I don't care, but I guess it might bother some people.
You can clean up such work-in-progress commits before pushing by using interactive rebase.
stash is a quick hack, and is useful as a quick hack, but commits just work better.
stash apply
use stash branch PickingBackUpStash
That said, the majority of my uses of stash are "git stash; git pull; git stash apply" Or, alternatively, "git stash --keep-index; build; test; commit; git stash pop"The whole point is I am not leaving the stash for long. If I have left a stash dormant for some time, I likely just drop it. Similar with branches.
Do you recall the actual circumstances? That shouldn't happen in the normal course of things...
All I know is there was some stash dropping and checking out involved and somehow I lost a couple day's worth of work...oops.
I've often had the workflow of "make a bunch of changes on master; git checkout -b new_feature_branch; git commit -am 'save it off'; git checkout master".
I haven't looked into the plumbing that goes into that, but I suspect it's actually stashing and popping for you.
I still usually give it a try, and then stash when that gives me a problem. With 12 developers and some high-churn files, it's pretty common that 2 people are doing something with the same file, even with branches that only last 1-2 days.
It works for other things too, e.g. you can undo rebase with `git reset master@{1}` (i.e. reset master to previous location of master).
https://github.com/tjmehta/git-wip
Let's me organize my "stashes" based on work-in-progress local branch commits. Also easier to rebase onto if it's a local branch that hasn't been pushed yet etc.
git wip
- alias for git add .; git commit -m __wip;
git unwip
- checks if last commit is a __wip and git reset HEAD^Loads of fun.
Really? Why would you "normally" jump through all these hoops just to work in the same directory? Using svn, my response to this is to jump back to the root of all my projects (~/codebase/ at my current work), checkout a clean version of the branch I need to work on, make changes / test / commit, and rm -rf the whole branch, changing directory back to where I was. If I get blocked on that change for a few hours, I don't have to jump through additional hoops to go back to working on the thing that was interrupted, since I can just change directory back to it.
Is there a reason why this is an awful workflow in git?
> [...], apply the changes (patch) from the file and continue the work. While it is not something difficult, it can be done much easier with Git.
Since he's saying "it can be done much easier with Git", the implication is that he wasn't using Git earlier, and the workflow with `git stash` is far cleaner than that diff/patch workflow.
At least, I think that's the case. I really can't imagine someone doing that instead of just using Git and `git stash` in the first place.
git add -p
git stash
test
commit
git stash pop git stash
git pull --rebase origin master
git stash pop> I think it’s worth emphasising that stash stores just your changes and not a snapshot of the entire tree. This means that you can create your stash on one branch and then pop it onto another branch. When you pop from the stash a merge is done including any conflict resolution that may be necessary.