Live data from Hacker News

Git Branches: Intuition and Reality

jvns.ca

191–200 of 281 posts

Re: Git Branches: Intuition and Reality

#191
post #183

Earlier quoted context omitted.

It is a tree. What makes you think it's just a DAG? Are there commits with multiple parent commits or what?

Yes. Merge commits have two parents.

Two or more. I'm not sure there's a limit.

Try not to do this (imagine a 5-way merge conflict).

Re: Git Branches: Intuition and Reality

#192
post #59

Earlier quoted context omitted.

The issue is that if you consider a branch to be what is really the history of the branch tip, then a branch is not just the part starting from the last join with another branch. Instead it is some directed path through the commit DAG, a path that in general can’t be reconstructed from the information Git keeps. If, for example, you have a structure like | o / \ o o A | | B o o \ / o / \ o o C | | D o o \ / o | then…

Interesting, so then which path(s) does git display when running git-log on this?

You can get ASCII art of that structure with:

  git log --graph --oneline
Older versions you'll also want --decorate to show branches and tags, but I think that's on by default now.

Re: Git Branches: Intuition and Reality

#193

I have found that git makes a lot more sense if you reverse the mental model of lineage. People think about a lineage going forward . But a more useful way to think is in terms of backward pointers. A commit points to it's parent(s). Since a branch is just a commit ID, you can follow the parent links backwards to find the whole history of that branch. So a "branch point" is just where two chains of parent links conve…

That's how I learned it, not having known anything about git or version control beforehand. I used this site:

learngitbranching.js.org/

Which represents commits as circles with arrows pointing to their parents.

Re: Git Branches: Intuition and Reality

#194
post #59

I have found that git makes a lot more sense if you reverse the mental model of lineage. People think about a lineage going forward . But a more useful way to think is in terms of backward pointers. A commit points to it's parent(s). Since a branch is just a commit ID, you can follow the parent links backwards to find the whole history of that branch. So a "branch point" is just where two chains of parent links conve…

The issue is that if you consider a branch to be what is really the history of the branch tip, then a branch is not just the part starting from the last join with another branch. Instead it is some directed path through the commit DAG, a path that in general can’t be reconstructed from the information Git keeps. If, for example, you have a structure like | o / \ o o A | | B o o \ / o / \ o o C | | D o o \ / o | then…

You can reconstruct it manually with a combination of the parent commit order and the automatic merge commit message, if you didn't change the commit message. But yeah, that second part isn't recorded in the structure itself.

Re: Git Branches: Intuition and Reality

#195
post #180

Earlier quoted context omitted.

git checkout will also happily throw away local unstaged modifications, and I would argue that it is even more dangerous because I did not have to type "--hard" to shoot myself in the foot.

That does not sound right at all, I’m pretty sure there’s a warning when you try to checkout a branch that would override local unstaged changes. I might be wrong but I’d like some proof.

Checkout is also used for reverting changes to unstaged files.

Re: Git Branches: Intuition and Reality

#196

I don't use git at work, but in my private hobby projects my friends usually get mad when they watch me juggle changes and branch pointers with git reset --hard and git stash... How do you undo a merge that you didn't mean to do/did wrongly? git reset --hard Have some cosmetic fixups on your local branch that really should go into main (or a separate branch) first before merging a bigger feature? git stash git checko…

Not a fan of the staging area, because it won't be tested. I would rather stash some changes to postpone them, then test and commit the workspace.

Re: Git Branches: Intuition and Reality

#197
post #80

Earlier quoted context omitted.

Git stores snapshots and that’s it. The whole tree, not per-file. As to why Linus doesn’t like storing file moves: https://public-inbox.org/git/Pine.LNX.4.58.0504150753440.721...

I'd be happy to argue why Linus is wrong here. Many things would be much easier if git recorded some more metadata in every commit: file moves, and branch moves, to start with. Having some sort of notion of "parent branch" would be very useful for a number of common operations, and a "renamed file" without having to rely on client dependent heuristics too. Empty files trip people up all the time so a "create file" wo…

People would get lazy and rename a file without telling Subversion they had done it, so it would write a “old file deleted, new file created from nothing” revision. Most of the merge conflict resolution machinery just couldn’t run without the missing guidance. Git infers someone probably renamed a file you edited or vice versa, which seems risky but works better in practice.

Re: Git Branches: Intuition and Reality

#198
post #184

Earlier quoted context omitted.

It is a tree. What makes you think it's just a DAG? Are there commits with multiple parent commits or what?

There absolutely can be. Merge commits have multiple parent commits for example. It's definitely a graph not just a tree.

Parent comment was about disregarding merge commits.

Re: Git Branches: Intuition and Reality

#199

Earlier quoted context omitted.

git checkout will also happily throw away local unstaged modifications, and I would argue that it is even more dangerous because I did not have to type "--hard" to shoot myself in the foot.

There must be something you do terribly wrong if you believe it happened to you in normal use.

The poster is talking about when you do something like this: `git checkout .` That wipes out all unstaged changes on tracked files in the current directory

Re: Git Branches: Intuition and Reality

#200

While the explanation is right in some sense, it misses a few points. Branches are pointers to a commit and that pointer is refreshed when a new commit is created. One could say they are a wandering tag (without explaining a tag for now). The actual chain of commits that represent what we see as branch comes from the commits themselves. Those commits point back to their parent commit. And then one can see why no bran…

> Branches are pointers to a commit and that pointer is refreshed when a new commit is created. One could say they are a wandering tag (without explaining a tag for now).

A good name for these "wandering tags" would be "heads", since it's what git calls them internally (for instance, when not packed, they're stored at the "refs/heads/" path in the repository). This also exposes a distinction between a "branch" and its "head", and that distinction can be useful.

Post reply on HN