News for hackers. Stuff that matters.
Re:Oh Shit, Git? by lalaithion on Thursday January 16, 2025 @02:36PM (#42729838)
The disconnect between git's beautiful internal model of blobs, a tree of commits, and pointers to commits, and the command line interface is so wild. All of these recipes are unintuitive even if you have a firm grasp of git's model; you also need to know the quirks of the commands! To just look at the first one... wouldn't it be more intuitive for the command line interface to be:

    # this command exists already;
    $ git switch -c some-new-branch-name
    # is there a command that simply moves a branch from one commit to another without changing anything else? It feels like it should be possible given how git works.
    $ git move-branch master HEAD~
Re:Oh Shit, Git? by Certhas on Thursday January 16, 2025 @03:49PM (#42730721)
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.

Re:Oh Shit, Git? by lalaithion on Thursday January 16, 2025 @04:48PM (#42731353)
None of these seem to preclude a command to make an arbitrary branch point to an arbitrary commit without changing anything else.
Re:Oh Shit, Git? by karatinversion on Thursday January 16, 2025 @06:44PM (#42732411)
You are looking for

  git update-ref <branch-name> <commit-sha>
Re:Oh Shit, Git? by DiggyJohnson on Friday January 17, 2025 @12:30PM (#42740833)
Wouldn't the fail or break under any circumstance where they don't immediately share a history?
Re:Oh Shit, Git? by karatinversion on Monday January 20, 2025 @11:26AM (#42770192)
I just tested it by creating a repo with two branches without a common ancestor, and I was able to move a branch pointer to either history with update-ref, so no, I don't think so
Re:Oh Shit, Git? by fragmede on Thursday January 16, 2025 @05:22PM (#42731671)
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
Re:Oh Shit, Git? by seba_dos1 on Friday January 17, 2025 @12:19AM (#42734347)
> 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.

Re:Oh Shit, Git? by Certhas on Friday January 17, 2025 @03:36AM (#42735314)
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"...

Re:Oh Shit, Git? by seba_dos1 on Friday January 17, 2025 @05:37PM (#42744011)
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.

Re:Oh Shit, Git? by thfuran on Friday January 17, 2025 @12:44AM (#42734445)
Couldn't head just detach without any consistency issue?
Re:Oh Shit, Git? by seba_dos1 on Friday January 17, 2025 @01:00AM (#42734522)
Theoretically it could, but that would be a rather surprising side effect. You could also check the new revision out and leave HEAD intact. Which one of those outcomes you would expect and why?

"error: ref in use by higher layers" makes much more sense to me in this case.

Re:Oh Shit, Git? by Certhas on Friday January 17, 2025 @03:42AM (#42735349)
If you buy the "git is just a tree of commits and pointers" mental model it's absolutely not a surprising side effect but would be the logical thing to expect. I moved a pointer to a commit around, why would that change where HEAD is pointed.

Turns out it's a tree of commits and pointers to within that tree and a master pointer that come in two versions: pointing towards the pointers or pointing towards the tree. And pointers behave very differently when the master pointer is pointing to them...

Elegant. Simple. :P

Re:Oh Shit, Git? by seba_dos1 on Friday January 17, 2025 @05:26PM (#42743939)
> I moved a pointer to a commit around, why would that change where HEAD is pointed.

...because HEAD points to what's checked out. This pointer does not just exist and hang around, it has its semantics. Not understanding that reveals flaws in your mental model.

Besides, the side affect you find "not surprising" here is... rewriting HEAD to change what it points to. Then you ask "why would that change where HEAD is pointed". Sounds like you may be confused. Are you forgetting that a ref may point not just to a commit, but also to another ref? This is the whole idea behind branches after all, having HEAD point to a ref is exactly what makes branches semantically different from tags - if you don't understand it then no wonder you're confused.

(protip: if you find git's "pointers to pointers" confusing, perhaps because in C a "pointer" and "pointer to pointer" are separate types that make multiple dereferencing steps explicit, think of them as symlinks instead and it should become clearer - that's in fact how symrefs used to be implemented in the past)

When a pointer is in use by higher layers, a good UI will prevent you from making direct changes underneath it unless you force it or go low-level enough for it to not matter. The only sin of git I can see here is that `git` command provides you both high-level and low-level interfaces to manipulate the data structure you're working on with no clear distinction for the user.

Re:Oh Shit, Git? by thfuran on Friday January 17, 2025 @02:37PM (#42742361)
It doesn't seem surprising to me. It probably ought to print ought a warning that head has detached though, like some other commands already do. That error message on the other hand seems very unhelpful. It's lingo that only makes sense if you're neck deep in the plumbing.
Re:Oh Shit, Git? by seba_dos1 on Friday January 17, 2025 @05:32PM (#42743972)
There's no such message there, it was a description of a situation written by me and it doesn't even actually match the git's lingo. Should have made it clearer I guess.

It is surprising. You wanted to edit the value of `main` ref, yet suddenly you now edited `HEAD` too without meaning it. Bailing out and letting you actually decide whether you want to do it or not is the correct thing to do for a high-level command like `git branch` (alternatively it could ask you what to do interactively). If you don't want such safeguards and you know what you're doing, use `git update-ref` which will happily let you break whatever you want.

Re:Oh Shit, Git? by neild on Thursday January 16, 2025 @02:49PM (#42729992)
The "move a branch from one commit to another without changing anything" command is "git reset".

"git reset --hard" is "...and also change all the files in the working directory to match the new branch commit".

"git reset --soft" is "...but leave the working directory alone".

Re:Oh Shit, Git? by rav on Thursday January 16, 2025 @03:50PM (#42730734)
Actually, "git reset --soft" moves the current branch to another commit, without moving the index (aka staging area) along with it, whereas "git reset" (aka "git reset --mixed") moves the current branch AND the index to another commit. I really couldn't wrap my head around it before I had gone through "Reset demystified" [1] a couple times - it's not a quick read but I can strongly recommend it.

[1] https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified

Re:Oh Shit, Git? by lalaithion on Thursday January 16, 2025 @04:53PM (#42731399)
git reset only works if you're on the branch you want to move, which is why every one of these example flows has you create your new branch, then do the reset, then switch to the new branch, instead of just allowing you to move a branch you're not on.
Re:Oh Shit, Git? by Terr_ on Thursday January 16, 2025 @04:13PM (#42730978)
[delayed]
Re:Oh Shit, Git? by JadeNB on Thursday January 16, 2025 @06:29PM (#42732279)
> Something I heard somewhere that stuck with me: git is less less of a Version Control System, and more of a toolkit for assembling your own flavor of one.

That's how it is in principle, but it seems to me that there aren't that many different CLI "porcelains" in practice. Kind of like how Knuth figured people would essentially write their DSLs on top of plain TeX, not spend most of their time in giant macro packages like LaTeX.

Re:Oh Shit, Git? by dragonwriter on Thursday January 16, 2025 @08:05PM (#42732925)
> That's how it is in principle, but it seems to me that there aren't that many different CLI "porcelains" in practice.

I think that's because most of the people that make custom tooling to support particular workflows build it into graphical (including IDE extensions, web-based. etc.) frontends, not CLIs.

Re:Oh Shit, Git? by jimbokun on Thursday January 16, 2025 @03:50PM (#42730733)
Are there alternative git command lines that keep the beautiful internals, but implement a more elegant and intuitive set of commands to manage it?
Re:Oh Shit, Git? by dalia-reds on Thursday January 16, 2025 @04:17PM (#42731017)
Check out jujutsu or jj (same thing). It's its own VCS, but it uses git as a backend, so it works with GitHub and other git integrations
Re:Oh Shit, Git? by stouset on Thursday January 16, 2025 @05:12PM (#42731570)
Seconded jujutsu. It's 100% git-compatible and one of those rare birds that is both more powerful and simpler to use in practice due to rethinking some of the core ideas.
Re:Oh Shit, Git? by maleldil on Thursday January 16, 2025 @06:08PM (#42732079)
Another vote for jujutsu. No one else needs to know you're using it. You can think of it as just a different CLI for git (although you shouldn't mix them). I used to use third-party interfaces like lazygit, but I don't need them anymore because jujutsu _just makes sense_.
Re:Oh Shit, Git? by jonasced on Friday January 17, 2025 @06:34AM (#42736470)
Lazygit has a terminal UI but might otherwise be what you're looking for: https://github.com/jesseduffield/lazygit
Re:Oh Shit, Git? by rav on Thursday January 16, 2025 @03:42PM (#42730629)
For move-branch: Use `git branch -f master HEAD~` if you're currently on another branch, or `git reset --soft HEAD~` if you're currently on master.
Re:Oh Shit, Git? by pitaj on Thursday January 16, 2025 @02:46PM (#42729951)
I prefer just using `git switch` because it's easy to remember the flags (and the position of arguments), but you're right, there is a simpler way:

    git switch -c some-new-branch-name
    git branch -f master HEAD~
Re:Oh Shit, Git? by DangitBobby on Thursday January 16, 2025 @03:40PM (#42730595)
You should also be able to do

  git branch -f master origin/master
Re:Oh Shit, Git? by pitaj on Thursday January 16, 2025 @03:52PM (#42730761)
This doesn't work if your local master was already ahead of origin
Re:Oh Shit, Git? by DangitBobby on Thursday January 16, 2025 @03:54PM (#42730777)
Indeed, as with all of these examples exceptions will apply and, it's a good idea to check the log before taking any such action. I believe your example also depends on exactly how many commits you've made that need to be moved. In any case, it depends on me remembering exactly what `~` signifies.
Re:Oh Shit, Git? by lalaithion on Thursday January 16, 2025 @04:54PM (#42731415)
Good to know! Thanks for the tip.
Re:Oh Shit, Git? by lilyball on Thursday January 16, 2025 @05:00PM (#42731467)
The "move a branch" command is `git push .`. Yes, you can push to the current repo. I have a script called git-update-branch which just does some preflight checks and then runs `git push --no-verify . +$branch@{upstream}:$branch` to reset a branch back to its upstream version.
Re:Oh Shit, Git? by zahlman on Thursday January 16, 2025 @10:11PM (#42733696)
> The "move a branch" command is `git push .`. Yes, you can push to the current repo.

Wouldn't that copy a branch rather than moving it?

Re:Oh Shit, Git? by lilyball on Sunday January 19, 2025 @10:25PM (#42764674)
"move a branch" means changing the commit the branch points to. `git push . $sha:$branch` will update $branch to point to $sha (you'll probably want to force this, unless you're just fast-forwarding the branch).
Re:Oh Shit, Git? by assbuttbuttass on Friday January 17, 2025 @12:05PM (#42740395)
> is there a command that simply moves a branch from one commit to another without changing anything else? It feels like it should be possible given how git works.

git switch -C master HEAD~