1/16/2025 at 6:42:00 PM
Some changes I would make:1. Always use `git switch` instead of `git checkout`
2. Avoid `reset --hard` at all costs. So for the "accidentally committed something to master that should have been on a brand new branch" issue, I would do this instead:
# create a new branch from the current state of master
git branch some-new-branch-name
# switch to the previous commit
git switch -d HEAD~
# overwrite master branch to that commit instead
git switch -C master
# switch to the work branch you created
git switch some-new-branch-name
# your commit lives in this branch now :)
3. I'd apply the same to the `cherry-pick` version of "accidentally committed to the wrong branch": git switch name-of-the-correct-branch
# grab the last commit to master
git cherry-pick master
# delete it from master
git switch -d master~
git switch -C master
4. And also to the "git-approved" way for "Fuck this noise, I give up.": # get the lastest state of origin
git fetch origin
# reset tracked files
git restore -WS .
# delete untracked files and directories
git clean -d --force
# reset master to remote version
git switch -d origin/master
git switch -C master
# repeat for each borked branch
by pitaj
1/16/2025 at 7:36:19 PM
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~
by lalaithion
1/16/2025 at 8:49:51 PM
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.
by Certhas
1/16/2025 at 9:48:29 PM
None of these seem to preclude a command to make an arbitrary branch point to an arbitrary commit without changing anything else.by lalaithion
1/16/2025 at 11:44:53 PM
You are looking for git update-ref <branch-name> <commit-sha>
by karatinversion
1/17/2025 at 5:30:37 PM
Wouldn't the fail or break under any circumstance where they don't immediately share a history?by DiggyJohnson
1/16/2025 at 10:22:33 PM
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
by fragmede
1/17/2025 at 5:19:22 AM
> 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.
by seba_dos1
1/17/2025 at 8:36:29 AM
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"...
by Certhas
1/17/2025 at 10:37:49 PM
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.
by seba_dos1
1/17/2025 at 5:44:46 AM
Couldn't head just detach without any consistency issue?by thfuran
1/17/2025 at 6:00:28 AM
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.
by seba_dos1
1/17/2025 at 8:42:44 AM
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
by Certhas
1/17/2025 at 10:26:49 PM
> 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.
by seba_dos1
1/17/2025 at 7:37:35 PM
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.by thfuran
1/17/2025 at 10:32:16 PM
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.
by seba_dos1
1/16/2025 at 7:49:02 PM
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".
by neild
1/16/2025 at 8:50:34 PM
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
by rav
1/16/2025 at 9:53:31 PM
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.by lalaithion
1/16/2025 at 9:13:09 PM
> 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 wildSomething 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.
by Terr_
1/16/2025 at 11:29:49 PM
> 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.
by JadeNB
1/17/2025 at 1:05:32 AM
> 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.
by dragonwriter
1/16/2025 at 8:50:17 PM
Are there alternative git command lines that keep the beautiful internals, but implement a more elegant and intuitive set of commands to manage it?by jimbokun
1/16/2025 at 9:17:31 PM
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 integrationsby dalia-reds
1/16/2025 at 11:08:06 PM
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_.by maleldil
1/17/2025 at 11:34:48 AM
Lazygit has a terminal UI but might otherwise be what you're looking for: https://github.com/jesseduffield/lazygitby jonasced
1/16/2025 at 10:12:24 PM
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.by stouset
1/16/2025 at 7:46:04 PM
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~
by pitaj
1/16/2025 at 8:40:20 PM
You should also be able to do git branch -f master origin/master
by DangitBobby
1/16/2025 at 8:52:53 PM
This doesn't work if your local master was already ahead of originby pitaj
1/16/2025 at 8:54:52 PM
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.by DangitBobby
1/16/2025 at 9:54:48 PM
Good to know! Thanks for the tip.by lalaithion
1/16/2025 at 10:00:17 PM
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.by lilyball
1/17/2025 at 3:11:12 AM
> 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?
by zahlman
1/16/2025 at 8:42:33 PM
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.by rav
1/17/2025 at 5:05:16 PM
> 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~
by assbuttbuttass
1/16/2025 at 9:12:50 PM
Not trying to defend the choice of `git checkout` over `git switch` (and `git restore`) but they were introduced in v2.23 of Git [0], which was released about 5 years ago [1]. If you take a look at their help pages, they still include a warning that says> THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.
Granted, it has been in there for basically as long as the command(s) existed [2] and after 5 years perhaps it might be time to no longer call it experimental.
Still, it does seem like `git checkout` might be a bit more backwards compatible (and also reflective of the time when this website was originally created).
[0] https://github.com/git/git/blob/757161efcca150a9a96b312d9e78...
[1] https://github.com/git/git/releases/tag/v2.23.0
[2] https://github.com/git/git/commit/4e43b7ff1ea4b6f16b93a432b6...
by mrshu
1/16/2025 at 10:06:32 PM
5. Teaching `git add .` as default to add changes to the staging area is not ideal. Show adding specific files instead has less room for subsequent "oh shit" and better.by baobun
1/17/2025 at 3:13:49 AM
Learning about the `-p` option for `git add` was one of two things that revolutionized my Git usage. (The other was figuring out how to write effective commit messages.)by zahlman
1/17/2025 at 5:06:42 AM
This is the main reason to use a GUI imho.by wodenokoto
1/17/2025 at 5:28:57 AM
Tig is a great one for the terminal fwiw.gitg for something simple, graphical and widely available.
by baobun
1/16/2025 at 11:15:20 PM
True enough, but it does make for good practice with the index and splitting workflows later on when you need to clean it up.I think there's space for "git add ." as a didactic step. It maps cleanly to the most obvious way to understand a commit, as "here's what I've done". Bootstrapping from that to an understanding of "commits as communication with other developers" will naturally happen over time.
by ajross
1/17/2025 at 12:24:23 AM
Is not very compatible with printlog-debugging. I'd rather encourage devs to prod around as they go if it benefits them, which causes grief for either them or reviewers in the end if they've internalized what you just said.Explicitly adding internalizes a personal review process as inherent part of the push process, instead of something you attempt to force on top later.
It's better with a collaboration workflow that limits the span of time with expected discipline, imo.
by baobun
1/17/2025 at 12:47:54 AM
You can have both. Make sure the whole diff is what you want it to be before invoking `add .`by recursive
1/17/2025 at 8:51:23 PM
Sure. I hear the pull-out method is also an effective contraceptive.by baobun
1/17/2025 at 11:33:18 PM
If it works for you, I'm not going to try to talk you out of it.by recursive
1/16/2025 at 10:27:12 PM
Could you motivate why you suggest these? Why is `switch` better than `checkout`? And why not use `reset --hard`?by jaapz
1/16/2025 at 11:09:32 PM
Not comment OP, but checkout has two very different uses merged into one: restoring files and switching branches. To not break compatibility, git has now switch and restore commands that make commands more readable and understandable.You should avoid reset --hard because it will delete all your uncommited, and you could end up in situations where that's really bad. Using reset --keep will keep uncommited changes, and failing if any uncommited change cannot be kept.
by jopicornell
1/17/2025 at 3:32:04 AM
I just do git add -A
git stash
git reset --hard
git stash pop
by teaearlgraycold
1/17/2025 at 7:21:00 AM
What do you mean avoid "reset --hard"? Why or why is it not enough in practice? I use it quite often, along with "alias git-restore-file='git restore --source=HEAD --'". It seems to work.by johnisgood
1/16/2025 at 7:40:49 PM
What's the problem with `reset --hard`?by CharlieDigital
1/16/2025 at 7:50:36 PM
It leaves behind tracked files that were moved or deleted between revisions.by pitaj
1/17/2025 at 5:05:45 PM
> 2. Avoid `reset --hard` at all costsSounds like you might be looking for `git reset --keep`
by xk3
1/16/2025 at 10:38:19 PM
Rewriting these for jj users. I'm prefering long option names and full command names for clarity here, but all the commands have shortened aliases and all the option names have single-letter alternatives. `@` means "the current revision", `x+` means "the revision just after `x`", `x-` means "the revision just before `x`".2. "Accidentally committed something to master that should have been on a brand new branch".
This doesn't really have an analogue. Branches ("bookmarks") only move when you tell them to. If you make a new commit on top of master, it doesn't point master to it, it just lives one past the tip of master. But let's say you accidentally moved master to include the new commit you shouldn't have:
# set master to the previous commit (and reaffirm that
# you're okay moving a bookmark backward)
$ jj bookmark set master --allow-backwards --revision @-
# there is no step two, you're still editing the change you already were
3. Move a commit from one branch to another. # move the revision one-past-master on to our desired bookmark
$ jj rebase --revision master+ --destination name-of-the-correct-bookmark
# there is also no step two; technically we're not updating the bookmark
# to point to the new commit yet, but this isn't something you'd do as rote
# habit in jujutsu anyway
4. Fuck this noise, I give up: # list all the operations I've performed against the repo
$ jj op log
# restore to some previous known-good state
$ jj op restore {id}
Bonus content, translated from the article:> Oh shit, I committed and immediately realized I need to make one small change!
# move the current edits into the previous revision
$ jj squash
> Oh shit, I need to change the message on my last commit! # re-describe the previous revision
$ jj describe --revision @-
> Oh shit, I tried to run a diff but nothing happened?! # there is no staging area, all your changes are part of the repo and there is no
# staging area pseudo-commit; please understand that this still works elegantly
# with "patch-add" workflows and does not imply that large change sets can't be
# easily broken up into small commits
> Oh shit, I need to undo a commit from like 5 commits ago! # find the commit
$ jj log
# back it out
$ jj backout {id}
> Oh shit, I need to undo my changes to a file! # find the commit
$ jj log
# restore the paths provided to their contents in the given revision
$ jj restore --from {id} [paths...]
And finally there are a few things that are super easy/obvious in jujutsu that are far more annoying in git.> Oh shit, I committed and many commits later realized I need to make one small change!
# moves the changes in the current working copy into the revision provided
$ jj squash --into {id}
> Oh shit, I committed and many commits later realized I need to make extensive changes! # sets your working copy to the commit provided; later commits will be
# auto-rebased on top live as you make modifications
$ jj edit {id}
> Oh shit, I need to reorder two commits! # does what it says on the tin
$ jj rebase --revision {a} --insert-before {b}
> Oh shit, I haven't committed anything in hours but I need something from an interim change from like thirty minutes ago # look in the "obsolete log" for earlier iterations of the current revision
$ jj obslog
# restore the contents
$ jj restore --from {id} [paths...]
> Oh shit, I made a bunch of changes but want them to be in multiple commits (e.g., patch-add workflow) # choose the parts to move out; you'll end up with two revisions, one with each half
$ jj split
> Oh shit, I need to break out a change from my current work into a new branch off master # choose the parts to move out; you'll end up with two revisions, one with each half
$ jj split
# move the stuff I pulled out onto master
$ jj rebase --revision @- --destination master
# optional: name it; most of the time you wouldn't bother
$ jj bookmark create new-name --revision master+
> Oh shit, I need to make three sequential changes but roll them out one-by-one. I also might need to make fixes to previous ones before later ones are rolled out. # author a new change on top of master and name it a
$ jj new master
…
$ jj bookmark create a
# author a new change on top of a and name it b
$ jj new
…
$ jj bookmark create b
# author a new change on top of b and name it c
$ jj new
…
$ jj bookmark create c
# edit a; nothing else is necessary to ensure b and c remain as descendants of
# revision a
jj edit a
…
# author a new change as part of b; nothing else is necessary to ensure c remains
# up to date on top of b
$ jj new --insert-before c
…
# point c at the new change
$ jj bookmark set b
by stouset
1/17/2025 at 12:43:47 AM
Please kindly write one for a jj-specific issue: "my build vomitted out a bunch of files and I used any jj command before editing my .gitignore"I've found myself using git to fix the mess in this particular instance.
by amanwithnoplan
1/17/2025 at 1:00:25 AM
$ jj file untrack {paths-or-pattern}
Alternatively if you have a bunch of files spewed everywhere with no rhyme or reason which can't be globbed or enumerated reasonably: $ jj status | grep '^A' | awk '{print $2}' | xargs jj file untrack
by stouset
1/17/2025 at 3:06:03 AM
One thing I really appreciate is that you can run `jj new master` at _any_ time to drop what you're doing and start a new change. The way jj handles the working copy, conflicts, and visible heads means there's just no need to think about uncommitted changes, unfinished conflict resolution, detached head, etc.. So many things that would get in your way just can't happen.by hooper
1/17/2025 at 7:31:06 AM
I haven’t thought about it at all but you’re right. It’s surprising how nice it is that I can enter a repo and `jj new main` without needing to remember any context whatsoever.My post was a pretty naked attempt to showcase how much less convoluted basic operations are in jj vs. git and hopefully drum up some interest. Hopefully someone bites.
by stouset
1/17/2025 at 7:40:57 PM
`jj new trunk()` is even better than `jj new main`, I just realized, ha!by steveklabnik
1/17/2025 at 8:12:34 PM
It is! I've fully migrated my repos over to `main` at this point so it's rare I have to think about the difference. You could also make an alias to `jj n` or something to make it even easier.by stouset
1/17/2025 at 2:27:34 PM
git switch is too new and its man page says "THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE."by Am4TIfIsER0ppos
1/16/2025 at 10:48:28 PM
millennial boomer here where is the gen z cheat sheet for this git switch thing that i keep hearing aboutby dustingetz
1/16/2025 at 11:12:31 PM
> 1. Always use `git switch` instead of `git checkout`Even harder: always use "git reset --hard".
Basically don't use local branches. The correct workflow for almost every task these days is "all branches are remote". Fetch from remotes. Reset to whatever remote branch you want to work above. Do your work. Push back to a remote branch (usually a pull request branch in common usage) when you're done.
If you need to manage local state, do it manually with tags (or stash, but IMHO I never remember what I stashed and will always make a dummy commit and tag it).
Don't ever try to manually manage a branch locally unless you (1) absolutely have to and (2) absolutely know what you're doing. And even then, don't, just use a hosted upstream like github or whatever.
by ajross
1/16/2025 at 11:32:44 PM
This sounds like the correct Git workflow if you think the correct VCS to use is SVN.by smrq
1/17/2025 at 12:08:46 AM
And that sounds like you failed to understand me. I didn't say "don't use branches". I said "all branches are remote". Pushing to a branch is communication with other human beings. Mixing your own private state into that is confusing and needless in 99% of situations (and the remaining 1% is isomorphic to "you're a maintainer curating branches for pushing to other people at a well-known location").All branches are public.
by ajross
1/17/2025 at 12:46:17 AM
I have quite a few projects that do not have a "remote" and will probably never have a remote repo. Should I not be using branches at all?by ryandrake
1/17/2025 at 12:58:40 AM
> All branches are public.What actual problem does this solve? For me, WIP branches only ever get pushed up if at least one of two things are true about them:
1) They're actually worth preserving, and not some experimental garbage that ended up being totally pointless.
2) I need to get them off of my local machine for disaster-recovery purposes.
> If you need to manage local state, do it manually with tags (or stash, but IMHO I never remember what I stashed and will always make a dummy commit and tag it).
I don't see the benefit one gets from putting work that's not fit for publication in a dummy commit on a public branch. That's just asking for garbage that noone should concern themselves with to accidentally get pushed up at the end of a long-ass day.
by simoncion
1/17/2025 at 2:26:07 AM
> 1) They're actually worth preserving, and not some experimental garbage that ended up being totally pointless.That seems naive. You don't know what's pointless for years, usually. Can I tell you how many times I've gone back to stale pull requests and topic branches to recover "How did I do this?" code?
> 2) I need to get them off of my local machine for disaster-recovery purposes.
That's called a "backup", and yes, data robustness is a big advantage of this workflow. You're acting like this is some kind of rare event. I push my local work to a branch (or three) on github every hour!
A corrolary is hardware independence, btw. Working off remote branches means I can also stand up a replacement development environment with a simple clone. (And the corrolary to that means that I can trivially document this such that other people can stand up development environments for my stuff, too!)
by ajross
1/17/2025 at 12:49:12 AM
This is a workflow I’ve never seen on any team or project I’ve worked on. Another commenter already mentioned the remote branch for everything preference, but usage of tags is especially interesting to me. I think that’s how most people use branches, and tags tend to be more permanent. What do you do when you come back to the commit with the tag, cherry pick it over and delete the tag? It sounds like an overly complicated process compared to having a branch and rebasing onto the current branch when you finally go to make the change for real.by snafferty
1/17/2025 at 2:52:07 AM
Local branches aren't names for anything other humans beings care about. All "branches" discussed in a team are remote. But because branches have "history" and "state", keeping your local names around is just inviting them to get out of sync with identically or similarly-named branches out there in the rest of the world.> It sounds like an overly complicated process compared to having a branch and rebasing onto the current branch when you finally go to make the change for real.
Not sure I understand the problem here? The rebase is the hard part. It doesn't help you to have a name for the code you're coming "from". If it collides it collides and you have to resolve it.
What I said about tags was just as short term memory "This commit right here worked on this date", stored in a way that (unless I delete or force-update the tag) I can't forget or pollute. Branches don't have that property. And again local branches don't have any advantages.
by ajross
1/17/2025 at 3:31:06 AM
At first I was put aback by this, but it actually kinda makes sense. I mean, if people are giving off unwarranted advises about "the right way" here, yeah, you should start with a remote branch, and push all your work ASAP. Especially when you are closing the lid of your laptop to change location....Not that I am gonna follow that advice, of course. Same as I'm not gonna use git switch for a task git checkout does perfectly well.
by krick