News for hackers. Stuff that matters.
Re:Flight rules for Git by chrisseaton on Tuesday October 16, 2018 @11:43AM (#18230261)
What 'footguns' are there? I find it pleasingly hard to accidentally delete anything, or get myself into any kind of corner when using Git. What am I missing?
Re:Flight rules for Git by crooked-v on Tuesday October 16, 2018 @12:25PM (#18230655)
Consider `git checkout` and having a file called `master` at the root level of the repo.
Re:Flight rules for Git by CJefferson on Tuesday October 16, 2018 @03:18PM (#18232328)
Further, what does `git checkout a/b` do?

* Checkout a file called a/b

* Checkout branch b from remote a

* Checkout a local branch called a/b

I've seen people end up with local branches with names like 'upstream/mybranch', then get super-confused about how these branches aren't "upstream". git should really ban local branches with a / in their name.

Re:Flight rules for Git by bicolao on Tuesday October 16, 2018 @01:39PM (#18231405)
It's a bug that "git checkout" does not complain in this case. I'll try to fix or report it.
Re:Flight rules for Git by dfabulich on Tuesday October 16, 2018 @01:43PM (#18231442)
Re:Flight rules for Git by chopin on Wednesday October 17, 2018 @05:31AM (#18237506)
Though I agree with you, a warning could be issued.
Re:Flight rules for Git by booleandilemma on Tuesday October 16, 2018 @12:57PM (#18230980)
Sounds like an edge case. This isn’t really a problem with Git per se.
Re:Flight rules for Git by crooked-v on Tuesday October 16, 2018 @01:05PM (#18231076)
A single command doing two completely different things arbitrarily depending on file matches isn't an "edge case".
Re:Flight rules for Git by lixtra on Tuesday October 16, 2018 @01:31PM (#18231315)
But this very common for many tools:

  touch -- -l
  ls *
Re:Flight rules for Git by mmt on Wednesday October 17, 2018 @11:21AM (#18239941)
However, if you run both commands consistently, nothing untoward occurs:

  touch -- -l
  ls -- *
The fact that you have to use a double-hyphen flag to create a file with a name starting with a hyphen is a strong indication that this situation is an edge case.
Re:Flight rules for Git by lixtra on Wednesday October 17, 2018 @04:11PM (#18243277)
And git handles the edge case the same way. That was my point.

  rm master
  git checkout -- master
  git checkout a1b1
  git checkout master -- master
all works as expected
Re:Flight rules for Git by mmt on Wednesday October 17, 2018 @09:15PM (#18245187)
Not quite. I believe the point is that it's not an edge case, since "master" isn't a particularly unusual name, not in the Unix tradition, nor, AFAIK, on any other OS.

You'll note that, for example, your "rm" didn't require a double-hyphen.

The OC's point (and maybe the GP's point [1]), ultimately, is about git command syntax ambiguity. With traditional Unix commands, there's not much ambiguity. At worst, there's merely no obvious way to pass an argument that's a filename starting with a hyphen. The question "what does 'ls -l' do?" would not plausibly have an answer of "output '-l' if a directory entry with that name exists" in addition to "output a long listing of all directory entries".

[1] Does git actually behave differently based on the existence of a directory entry named "master" or does it merely require extra effort to work with such an entry, which would actually be directly analagous to filenames beginning with a hyphen?

Re:Flight rules for Git by crooked-v on Tuesday October 16, 2018 @02:19PM (#18231781)
That's bad too.
Re:Flight rules for Git by lixtra on Tuesday October 16, 2018 @11:46AM (#18230282)
Explain detached head like i’m 5 years old.
Re:Flight rules for Git by xyzzy_plugh on Tuesday October 16, 2018 @11:54AM (#18230359)
Detached HEAD isn't complicated. It simply means there is nothing tracking the current line of work beyond the commit shas themselves. No branch is updated when you make commits.

Conversely, attached HEAD means a branch is being updated whenever you make a commit, e.g. when you make commits on your local master.

Re:Flight rules for Git by lixtra on Tuesday October 16, 2018 @12:27PM (#18230678)
Oh, I see. I created this great patch and commited it. Then I wanted to push it, but it reported some strange error.

I checked out master but now my commit is no longer there. HELP!

Can I somehow push my changes to you so you can fix it?

Ah let's intuitively try:

git log --graph --decorate $(git rev-list -g --all)

For comparisson:

- mercurial would show your commit all the time, no matter what currently is checked out.

- Only when you push, you have to force it, to create a new head in the remote repository because most workflows expect you to merge heads locally.

Re:Flight rules for Git by seba_dos1 on Tuesday October 16, 2018 @12:40PM (#18230806)
All I get from this comment is that "git reflog" should be one of the first things taught about git :P
Re:Flight rules for Git by lixtra on Tuesday October 16, 2018 @12:54PM (#18230943)
Maybe reflog would help. Unfortunately most cheat sheets I saw don't mention it.
Re:Flight rules for Git by craftyguy on Tuesday October 16, 2018 @02:01PM (#18231592)
As other comments here have suggested, git cheatsheets should be ignored if you are learning git.
Re:Flight rules for Git by iainmerrick on Tuesday October 16, 2018 @12:20PM (#18230616)
But why would I get into that state, and how do I get out without losing my work? Neither of those is obvious.
Re:Flight rules for Git by tome on Tuesday October 16, 2018 @12:22PM (#18230633)
> how do I get out without losing my work?

When you move out of detached head state by checking out another branch Git tells you how to save your work to a branch.

Re:Flight rules for Git by iainmerrick on Tuesday October 16, 2018 @12:49PM (#18230905)
It could usefully say that when you do a “git status” when you’re in a detached state, instead of just saying “detached HEAD” and expecting you to figure it out (or google for it).

It could very usefully try harder to avoid getting into that state in the first place.

Re:Flight rules for Git by annabellish on Tuesday October 16, 2018 @12:58PM (#18230993)
>It could very usefully try harder to avoid getting into that state in the first place.

Am I missing something here? How do you get into a detached HEAD state without explicitly taking an obviously weird action, like finding and checkout out a commit hash instead of a branch, and why would it make sense for git to not be in a detached HEAD state should you do that?

Re:Flight rules for Git by jamescostian on Tuesday October 16, 2018 @01:24PM (#18231248)
Imagine someone does task X but they do it very poorly and in master. You want to get rid of their last few commits (without a force push).

Most people don't think about doing a hard reset to the last good commit, and then soft resetting to HEAD~. Instead, they just "git checkout $LAST_GOOD_COMMIT". And afterwards they may even continue by working with a detached HEAD

Re:Flight rules for Git by mappu on Tuesday October 16, 2018 @05:08PM (#18233282)
> an obviously weird action, like finding and checkout out a commit hash instead of a branch

But this isn't an obviously weird action? It happens all the time when you need to branch off from a previous point.

Maybe you're unexpectedly releasing a hotfix, maybe your current development is going in a bad direction, maybe you need to test something against a previous state.

Re:Flight rules for Git by WorldMaker on Tuesday October 16, 2018 @02:37PM (#18231967)
Rebase has several cases that will drop you into a detached HEAD. Rebase has gotten better at warning you when it is happening, and much better at telling you what your next steps are in that situation (such as git rebase abort), at least, but it still does it.

Several times that I've helped junior developer out of a detached HEAD state, when I asked what they thought they were doing before it happened, the answer was "I was just doing my normal rebase before..."

Re:Flight rules for Git by Latty on Tuesday October 16, 2018 @07:22PM (#18234161)
It is (or at least used to be) pretty easy to end up with a detached HEAD when working with submodules and aren't sure what you are doing. It definitely didn't need an "obviously weird" action.
Re:Flight rules for Git by tome on Tuesday October 16, 2018 @12:52PM (#18230928)
> It could usefully say that when you do a “git status” when you’re in a detached state

That's a great idea and it really confuses me why they don't add obviously useful things to the interface.

Re:Flight rules for Git by XCabbage on Tuesday October 16, 2018 @11:50AM (#18230317)
Why should he have to? It's irrelevant to the point he's making, since a tool being conceptually difficult is not the same thing as it having footguns.
Re:Flight rules for Git by masklinn on Tuesday October 16, 2018 @12:32PM (#18230732)
> a tool being conceptually difficult is not the same thing as it having footguns.

But the tool is not conceptually difficult. All the difficulty of git is in the trap its "UI" creates for no reason. The concepts are easy. I taught Darcs to a frontend/flash developer 15 years ago, and it's not because I'm a great teacher.

Re:Flight rules for Git by chrisseaton on Tuesday October 16, 2018 @12:59PM (#18231001)
I get that could be confusing, but there's no 'gun' there, is there? You may have to move your commit, but nothing been damaged?
Re:Flight rules for Git by lixtra on Tuesday October 16, 2018 @01:39PM (#18231397)
Time wasted is equivalent to damage.

Every digital damage can be undone by wasting enough time.

Re:Flight rules for Git by tome on Tuesday October 16, 2018 @12:04PM (#18230469)
If you're in detached head then `checkout -b the_detached_head_i_just_had` and the fact that you were in detached head becomes irrelevant.
Re:Flight rules for Git by mikestew on Tuesday October 16, 2018 @11:54AM (#18230361)
Maybe five year olds shouldn't be mucking with your source code.

Detached is just that: you can party on it all you want, but it's not going to go anywhere when you commit unless you go out of your way.