by bacon_waffle · 6y ago · 0 comments
git checkout -b new-branch
I fairly often make a branch as a sort of named undo point, and only check it out if need to undo to that point. Tags could work for that purpose too, but the branch approach seems safer in my usual work situation.Could you say more about this? I use
git checkout -b new-branch
all the time, but that checks it out. What does "using an un-checked-out branch as an undo point" mean? Don't you have to check it out to commit changes to it? I suppose I could read the docs but the git docs are awful.Recently, in explaining this to a colleague who's relatively new to git, I think the "aha" moment came in realising that if you rewrite the history leading to commit X, you're really making new commit Y in addition to the old commit X - you're not changing X in to Y. Leaving a branch pointing at X is enough that git will keep it around after the history rewriting, in case you decide to do something with it later.
Funny thing, nowadays it's going to be deprecated in favor of:
git switch -c newbranchI actually popped on the git development irc channel a few months ago to make this suggestion. The two people I talked to (at least one of whom seemed to be a core developer, although I did not verify this) did not see how, if you're going to conflate the actions of creating a branch and checking it out, it makes more sense to do this as a flag on the primary action, branch creation.
They were nice enough to have an in-depth conversion with a complete newcomer, though, so it was not a negative experience overall. Maybe someone else could convince them :)
That said, I do vaguely recall this seeming weird at first.
Why so? We don't go into great depth, but every hiring committee I've been on I make sure that we ask something about source control.
You can learn a lot about a developer's process by finding out if and how they use source control. (Red flag, for me, is if an apparent senior dev doesn't use a VCS.)
git checkout -t remote-branch