Live data from Hacker News

Lesser known Git commands

hackernoon.com

31–40 of 142 posts

Re: Lesser known Git commands

#31
post #16

I've seen more stash accidents than any other kind with git. Stashing is more dangerous than committing or branching, and to me it doesn't seem to provide any advantages... do people actually find stashing easier than branching? Is it just because when you branch you have to name it, and that causes friction? I stay away from stash. Right from the man page: "If you mistakenly drop or clear stashes, they cannot be rec…

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.

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

#32
post #16

I've seen more stash accidents than any other kind with git. Stashing is more dangerous than committing or branching, and to me it doesn't seem to provide any advantages... do people actually find stashing easier than branching? Is it just because when you branch you have to name it, and that causes friction? I stay away from stash. Right from the man page: "If you mistakenly drop or clear stashes, they cannot be rec…

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.

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

#33

> so it’s good practice to create an empty commit as your repository root While I'm aware of issues with rebasing the root commit, I've never heard this advice before and it seems unnecessary.

I actually like to make an empty root commit just as a place to describe what I intend to do with a repo. I'm uncomfortable doing it as a README commit because that lends it an undue amount of permanence once work begins.

Occasionally on repos that are really just for me I even do this as a 'root' commit for work branches, but the way rebase rolls over empty commits makes that kind of problematic in the long run.

Re: Lesser known Git commands

#34
post #16

I've seen more stash accidents than any other kind with git. Stashing is more dangerous than committing or branching, and to me it doesn't seem to provide any advantages... do people actually find stashing easier than branching? Is it just because when you branch you have to name it, and that causes friction? I stay away from stash. Right from the man page: "If you mistakenly drop or clear stashes, they cannot be rec…

While it's true that they can't be recovered through the "normal" safety mechanisms, they can certainly be recovered - that too up to 30 days in most cases.

`git fsck` can help here [0]. Also when a stash is dropped, it's SHA is printed to the console. If the console buffer hasn't been cleared and you realise it soon enough, recovery can be quite straightforward.

[0]: http://stackoverflow.com/questions/89332/how-to-recover-a-dr...

Re: Lesser known Git commands

#35
post #31

Earlier quoted context omitted.

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.

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.

git checkout -- .

Also works well for this use case

Re: Lesser known Git commands

#36

Earlier quoted context omitted.

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.

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).

Typically once I've noticed I haven't branched is after I've already added and committed.

Re: Lesser known Git commands

#37
> git it and empty root commit

Why doesn't git init does this by default? It would be nice if every repo had an empty root commit (with no author, date,... so it has the same commit hash), then every two repos would have a common commit. Semantically it would mean that repos would be just specific branches of a hypotetical large repo.

Re: Lesser known Git commands

#38
post #30
post #6

My personal favorite two: jschroeder@omniscience:~$ git config alias.up pull --rebase jschroeder@omniscience:~$ git config alias.down push They can be used thusly: git up && git down

I like where your head's at and added a little something to help me here at work: void:~% git config alias.funky ! git up && git submodule update And speaking of lesser known commands, I've been using 'git worktree' recently at work and I'm a big fan. Admittedly, it's probably mostly unknown because it's a new feature. It's a little rough around the edges, but it has made things like customer support context switchin…

Could you share your workflow with worktree a little? What kind of development do you do and where does it fit in with your workflow?

I tried using it in a JS project for those times when I'm in the middle of work but someone asks "hey is master working fine on your system?" - eventually though, having to install all the node and bower modules and rebuild everything on the worktree didn't seem much smoother than just stashing my local changes.

Re: Lesser known Git commands

#39
post #31

Earlier quoted context omitted.

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.

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.

'git reset'.
Post reply on HN