Comment by Certhas

Comment by Certhas 2 days ago

13 replies

The real "internal model" of git contains much more data/moving parts.

There isn't one tree of commits, there are typically at least two: local and remote

Branches are not just pointers to commits, but also possibly related to pointers in the other tree via tracking.

Stash and index and the actual contents of the working directory are additional data that live outside the tree of commits. When op says "avoid git reset hard" it's because of how all these interact.

Files can be tracked, untracked and ignored not ignored. All four combinations are possible.

lalaithion 2 days ago

None of these seem to preclude a command to make an arbitrary branch point to an arbitrary commit without changing anything else.

  • karatinversion a day ago

    You are looking for

      git update-ref <branch-name> <commit-sha>
    • DiggyJohnson 17 hours ago

      Wouldn't the fail or break under any circumstance where they don't immediately share a history?

  • fragmede 2 days ago

    This works if the branch exists or creates it if it doesn't exist, but not if it's checked out.

        git branch -f branch_name commit
    
    if it's checked out:

        git reset --hard commit
    • seba_dos1 a day ago

      > but not if it's checked out

      ...and for a good reason that should be apparent to anyone who understands git's model (HEAD points to a ref in this case, so if you suddenly change what that ref points to without updating the working tree you create an inconsistency).

      You can do that manually of course (with `git update-ref` or even a text editor), but then you get to clean up the mess yourself.

      • Certhas a day ago

        To me that looks like git is leaking implementation details left and right.

        So much for "a branch is simply a pointer to a commit"...

        • seba_dos1 12 hours ago

          Do you react the same way when an OS prevents you from writing to a file with an exclusive lock placed on it? So much for "a file is simply a collection of data stored as a single object"...

          If a git repo was purely a collection of meaningless pointers and graph nodes, git would be a graph manipulation utility, not a version control system. The fact that some of those pointers have a meaning is what makes it useful and it doesn't contradict the fact that what you're working on is still just a bunch of pointers and nodes.

      • thfuran a day ago

        Couldn't head just detach without any consistency issue?