News for hackers. Stuff that matters.
Re:Lesser known Git commands by ryanSrich on Friday September 30, 2016 @11:35AM (#12613073)
I use stash when I forget to branch. So I might be working and then realize I never branched. So I stash my changes, create a new branch, and then pop my changes on the new branch.
Re:Lesser known Git commands by dahart on Friday September 30, 2016 @11:42AM (#12613113)
Makes sense, but fwiw, there's a safer alternative: branch immediately, then go back to the source branch and reset.

There's nothing wrong with your workflow, btw, stash works in that case, as long as you immediately pop & don't get interrupted by lunch or a phone call.

Stashing is obviously providing some value that people like, it's very popular. But considering that it has extra danger, and that it's really easy to work without stash, I'm still confused why it's so popular.

And fwiw, most of the stash accidents I've seen are fairly simple scenarios like you described, and some teeny tiny twist or interruption causes someone to get confused and lose their stash, or think they lost their stash, and panic.

Re:Lesser known Git commands by leephillips on Friday September 30, 2016 @12:44PM (#12613573)
I'm not a git expert (yet), but I agree. I've read the stash man page a few times and tried to imagine when I'd want to use it over the alternatives, but I never do.
Re:Lesser known Git commands by ashark on Friday September 30, 2016 @11:48AM (#12613167)
My #1 use of stash is as the quickest way to express "get rid of all this crap". I rarely intend to ever retrieve it.
Re:Lesser known Git commands by dahart on Friday September 30, 2016 @12:07PM (#12613294)
Now here's one I actually like. As an alternative to git reset, a git stash is safer, and gives you an easier way than reflog to recover if you dropped a commit you want back.
Re:Lesser known Git commands by spike021 on Friday September 30, 2016 @12:47PM (#12613592)
For one of my projects, the 'build process' requires that I use the git archive command (basically archive to .tar.gz without any git-related files/directories). git archive on its own is fine for final builds because it's based off of HEAD for the current branch, but if I want to test a build with working changes, I would either need to commit all the changes _or_ use git stash and then archive based on the state I just stashed.

So in this case git stash works a lot better for a 'temporary' commit, kind of deal.

It might not be optimal but I've found the process to work pretty well for this particular project's situation.

Re:Lesser known Git commands by ianleeclark on Friday September 30, 2016 @11:56AM (#12613227)
git checkout -- .

Also works well for this use case

Re:Lesser known Git commands by loeg on Friday September 30, 2016 @11:59AM (#12613248)
'git reset'.
Re:Lesser known Git commands by ashark on Friday September 30, 2016 @12:02PM (#12613265)
Doesn't do the same thing, without some more options. Stash is shorter.
Re:Lesser known Git commands by spinningarrow on Friday September 30, 2016 @11:51AM (#12613184)
If you're stashing your changes, that means they're just in the working copy and haven't been committed to the incorrect branch.

In this case, simply create the branch and switch to it (git checkout -b branchname) and you're good. It's like it never happened ;) (remember that branches are just labels - until you commit it doesn't really matter).

Re:Lesser known Git commands by ryanSrich on Friday September 30, 2016 @11:56AM (#12613229)
Typically once I've noticed I haven't branched is after I've already added and committed.
Re:Lesser known Git commands by sammoth on Friday September 30, 2016 @12:04PM (#12613276)
Then just create the branch where you are, and afterwards go back and reset the original branch
Re:Lesser known Git commands by convolvatron on Friday September 30, 2016 @02:31PM (#12614302)
and make sure to patch up the upstream reference
Re:Lesser known Git commands by prashnts on Friday September 30, 2016 @12:27PM (#12613455)
I use `legit` aliases for this. It allows me to do `git sprout <new branch name>`, where it stashes my untracked changes, create a new branch, pop the stash and applies it. Pretty handy!

legit: https://github.com/kennethreitz/legit [1]

[Ignore the pretentious "for humans" tagline though...]

Re:Lesser known Git commands by majewsky on Friday September 30, 2016 @05:06PM (#12615292)
`git sprout <branch-name>` looks identical to `git checkout -b <branch-name>`.
Re:Lesser known Git commands by ben174 on Friday September 30, 2016 @11:43AM (#12613123)
Why stash? Why not just branch then commit?
Re:Lesser known Git commands by Grue3 on Friday September 30, 2016 @12:14PM (#12613340)
Because you might be currently not at the place where you intended to branch off. For example trying to make a bugfix on an experimental feature branch, but then you realize you should branch it off master.
Re:Lesser known Git commands by geofft on Friday September 30, 2016 @12:19PM (#12613391)
So branch to a temporary branch, commit, checkout the branch you wanted to be on, cherry-pick the temporary branch to grab its head commit, then delete the temporary branch once you're sure you don't need it.

This isn't completely safe because deleting a branch also deletes its reflogs, but it does avoid use of the stash command at all, which I find good for my own safety (because it reduces cognitive load). And it scales obviously to making multiple commits onto the temporary branch, etc.

Re:Lesser known Git commands by eridius on Friday September 30, 2016 @01:08PM (#12613709)
Or even just branch, commit, rebase onto the desired branch.