Skip to content(if available)orjump to list(if available)

Beej's Guide to Git

Beej's Guide to Git

62 comments

·February 5, 2025

Sxubas

> The Old Command: git checkout

I didn't even know git switch existed, let alone git checkout was considered the old alternative. I feel old.

To be fair I started learning git a little less than 10 years ago but woah, I can't express how it feels that someone learning git today will be confused of why I use git checkout. Like using old fashioned language.

More on topic, this guide would've been super useful when I was learning. It is really easy to follow and covers common FAQs.

I fondly remember being intimidated by my first merge conflict, aborting it and just doing some workarounds to prevent the conflict.

pkage

I remember reading the excellent Beej's Guide to Network Programming[0] and Beej's Guide to Unix IPC[1] as a teenager, which were incredibly approachable while still having depth—fantastic reads both and very influential on the programmer I ended up being.

[0] https://beej.us/guide/bgnet/ [1] https://beej.us/guide/bggit/

soseng

Same here! I was also a teenager in the mid-90s. And I was amazed by IRCd server code and bots. I bought a used copy of the book Slackware Linux unleashed w/CD-ROM and it had some networking code examples in C. I found Beej's Networking site because I was confused by a lot of that networking code. Became even more obsessed and went a deep rabbit hole. I spent a lot of time visiting different book stores hoping they had programming books. Bought Richard Stevens' amazing reference books and never looked back. Thanks for enabling my passion all these years later Beej!

1propionyl

+1, I have almost exactly the same story!

(I didn't read the IPC guide.)

beej71

Hey all--if you find things wrong, post 'em. I'll clean 'em up. :)

Love, Beej

junebash

Just a quick shout-out; I was one of the many many students you taught at Lambda School, and just wanted to say your instruction was one of the highlights of my time there. Thanks for doing what you do!

fabiensanglard

Loved your Network programming guide :) !

1propionyl

I found your networking guide as a kid with only some scripting experience, and it served to get me into C programming in general, so I have a special fondness for it.

Appreciate the work! Neat to see you still writing pieces like this all these years later!

scrapcode

I can't help but feel that Git has completely missed the forest through the trees that you can make a 30+ part guide explaining how to use it.

macjohnmcc

And still shoot yourself in the foot.

verandaguy

Eh, yes and no.

Git porcelain stuff's plenty good for probably 95% of users. `rebase -i` comes with a guide on which commands do what, and you could write a couple of paragraphs about how to format `git log`'s output with your own preferences and tradeoffs -- and porcelain usually includes stuff as eclectic as `git gc`, `git fsck`, and `git rev-parse` by most accounts.

Git plumbing's definitely a bit more obscure, and does a bunch of stuff on its own that you can't always easily do with porcelain commands because they're optimized for the common use cases.

TL;DR: while Git's big (huge even), a lot of what it provides is way off the beaten path for most devs.

ujkiolp

not my experience - almost always some edge case leads me to a git rabbit hole

tldr: even if you never plan to use anything advanced, you’ll end up in some weird situation where you need to do something even if you’re in the “95% of the users”

no shade, yes ofc you “could this, could that” to make things work and we have been stuck with this for so long that an alternative doesn’t even seem plausible

ajross

My sense, bluntly, is that if people spent half the effort learning git that they do whining about it, no one would bother making a 30+ part guide just explaining stuff you could find in a man page.

Commits are snapshots of a tree. They have a list of ancestors (usually, but not always, just one). Tags are named pointers to a commit that don't change. Branches are named pointers to a commit that do change. The index is a tiny proto-commit still in progress that you "add" to before committing.

There. That's git. Want to know more? Don't read the guide, just google "how to I switch to a specific git commit without affecting my tree?", or "how do I commit only some of my changed files?", or "how to I copy this commit from another place into my current tree?".

The base abstractions are minimalist and easy. The things you want to do with them are elaborate and complicated. Learn the former, google the latter. Don't read guides.

chasil

The deal killer for me, the inescapable aspect of my users, is that they insist upon checking passwords into revision control.

Because the C and PL/SQL people are on CVS, I can fix this with vi on the ,v archive.

First on TFS repositories, and now with git grep I can easily find exposed passwords for many things. But it's just SQL Server!

We will never be able to use git responsibly, so I will peruse this guide with academic interest.

Don't even get me started on secrecy management.

I am looking forward to retirement!

rblatz

The devs shouldn’t have access to prod credentials in the first place. That’s the real issue.

skydhash

Or do read books and guides. But in an exploratory manner. So when you do have a need for a specific operation (which happens rarely) you have a mental map that can give you directions.

dijit

Well, what's terrifying is that the guide is so long.

I am aware that beej's guides are typically quite comprehensive, but the vast nuances of git truly eluded me until this.

I guess Jujitsu would wind up being a much slimmer guide, or at least one that would be discoverable largely by humans?

beej71

With most of my guides I try to make it so you can quit reading when you feel you've read enough. No need to read the whole thing.

And on that note, I feel like the guide covers maybe 10% of Git :), but hopefully 90% of common usage.

djeastm

>And on that note, I feel like the guide covers maybe 10% of Git

guh

I'm just going to be emailing myself versions of files with MyFile.Final.RealFinal2.txt from now on

vvpan

It tells me that git is the wrong tool for the majority of people but it just happened to stick.

stephen_g

I think it's probably the opposite, Git has amassed a lot of complexity because it's been adapted to being a tool that is able to can satisfy the majority of requirements.

I've never found that I need to touch most of it in the 15 or so years I've been using it, but it's there if your project needs it.

Barrin92

No. Git is a complex program but version control is an inherently complex problem that requires powerful tools. There's certain set of problems where, as a programmer, you're going to have to sit down and actually read the book.

The universe doesn't owe you an easy 10 minute video solution to everything, it's an annoying educational expectation that people seem to have developed. Some things are just that difficult and you have to learn them regardless.

forrestthewoods

No. Source control is not that complicated. Git is just bad. As an existence proof: Mercurial is much better and simpler.

I can teach someone who has never even heard of source control how to use Perforce in about 10 minutes. They will never shoot themselves in the foot and they will never lose work. There are certainly more advanced techniques that require additional training. But the basics are very easy.

Git makes even basic things difficult. And allows even experts to shoot their face off with a rocket launcher.

Git sucks. The best tool doesn't always win. If MercurialHub had been founded instead of GitHub we'd all be used a different tool. Alas.

elevatedastalt

I regularly conduct 2 hr long "Intro to the Git Data Model" courses at my workplace (1-2 times a year). I literally take them into the .git directory and unzip the files to show how everything is just plain text representation of basic data structures. It's honestly cool to see it click in their heads.

We have a basic Git cookbook we share with any new joinees so that they start committing code, but most of them just follow it religiously and don't understand what's going on (unsurprisingly).

However, literally everyone who attends the course comes out with a reasonable working understanding of Git so that they know what's actually happening.

That does NOT mean that they know all the commands well, but those can be trivially Googled. As long as your mental model is right, the commands are not a big deal. And yet, the vast majority of the discussion on HN on every single Git post is about the command line.

Funnily enough the class sounds a lot like the alt text of https://xkcd.com/1597/ (Just think of branches as...), the difference is that that is unironically the right way to teach Git to a technical audience, and they will come out with a fundamental understanding of it that they will never forget.

I honestly think it's such a high ROI time investment that it's silly to not do it.

lucasoshiro

I did it once, I was indeed really nice, and the discussion that we did after was very cool. I put in the last slide of the presentation some questions for my colleagues answer based on the Git data model, e.g.: "Can we move a commit to another branch?" or "What guarantees that we don't have cycles in the commit graph". I was really satisfying that people came out thinking Git, not only using it!

elevatedastalt

Exactly, and it's such a high success rate!

This is precisely why it enrages me when all HN discussion about Git devolves to the same stuff about how it's complex and this and that.

A technical person who has general sense about basic data structures (Leetcode nonsense not needed) can be taught Git in under 2 hours and they will retain this knowledge forever.

If you can't invest that little time to learning a tool you will use everyday and instead will spend hours Googling and blindly copy-pasting Git commands, that's on you, not on Git.

o11c

That last question is a cryptography question in disguise; the answer is "the fact that SHA-1 collisions are still impractical for most people".

leonseled

Haven’t checked out the article, I’m sure its great. But another reco is boot.dev’s git course taught by Primeagen. It’s interactive and He goes real deep down to manipulating files in the .git directory. Came out of that course with a whole new mental model of how git works.

talles

I love how there's a section for exiting vim

boneitis

I'm really interested and really hoping this is something I can sink my teeth into. I've always had frustrating experiences with trying to wrap my head around git and have to regularly use it at my job.

Branching, making commits, and creating pull requests come easy, but beyond that, I know utterly nothing about it.

lucasoshiro

One mistake that I see people making about Git is trying to learn more commands, more flags, more tricks, but not trying to really understand how it works. Perhaps it's your case. You know Git enough to use in your daily basis, so maybe it's time to dive into a lower level and then everything else will be natural.

I strongly suggest reading Pro Git, the official Git book by Scott Chacon and Ben Straub, available for free here: https://git-scm.com/book/en/v2.

I find it very pleasant to read and it really changed my perspective not only about Git but about how to write code in general. You don't need to read it entirely, but suggest at least these sections:

- 1.3 Getting Started - What is Git?: explains a little about snapshots and the three states

- 10.1 ~ 10.3 Plumbing and Porcelain, Git Objects and Git References: this explains Git in its lowest level, which is surprisingly simple but powerful. Those sections were enough for me to write my own "Git" (you can see it here: https://github.com/lucasoshiro/oshit)

beej71

If you do check it out and there are parts that are confusing, I'd love to hear about it.

at_a_remove

[delayed]

rco8786

I have nothing but fond memories of reading Beej's guides.

It's also this sort of work that's becoming less necessary with AI, for better or worse. This appears to be a crazy good guide, but I bet asking e.g. Claude to teach you about git (specific concepts or generate the whole guide outline and go wide on it) would be at least as good.

yoyohello13

Seems more efficient to have one reference book rather than generating entire new 20 chapter books for every person.

I also think if you are at the “don’t know what you don’t know” point of learning a topic it’s very hard to direct an AI to generate comprehensive learning material.

jonahx

> Seems more efficient to have one reference book rather than generating entire new 20 chapter books for every person.

The main advantage of LLMs is that you can ask specific questions about things that confuse you, which makes iterating to a correct mental model much faster. It's like having your own personal tutor at your beck and call. Good guidebooks attempt to do this statically... anticipate questions and confusions at the right points, and it's a great skill to do this well. But it's still not the same as full interactivity.

yoyohello13

I think a mix is the right approach. I’ve used LLMs to learn a variety of topics. I like having a good book to provide structure and a foundation to anchor my learning. Then use LLMs to explore the topics I need more help with.

When it’s just a book. I find myself having questions like you mentioned. When it’s just LLMs I feel like I don’t have any structure for my mind to hold on to.

I also feel like there is an art to picking the right order to approach learning a topic, which authors are better at than LLMs.

nealabq

This is a bit of a stretch, but it's a little like distillation, where you are extracting from the vast knowledge of the LLM and inserting those patterns into your brain. Where you have an incomplete or uncertain mental model and you ask a tutor to fill in the blanks.

Altho maybe I'm stretching the analogy too far.

rco8786

Wow this is really well stated, thanks.

mhh__

True although the don't know aspect is where LLMs will be magic. I envy today's youth for having them (and I'm not that old at all)

I remember fumbling around for ages when I first started coding trying to work out how to save data from my programs. Obviously I wanted a file but 13 year old me took a surprisingly long time to work that out.

Almost impossible to imagine with AI on hand but we will see more slop-merchants.

rco8786

Definitely more efficient in terms of power consumed, not so in terms of human effort to build such guides across nearly every topic one could think of. But you're right, we shouldn't ignore the power consumption.

I have found that asking AI "You are an expert teacher in X. I'd like to learn about X, where should I start?" is actually wildly effective.

yoyohello13

> not so in terms of human effort to build such guides across nearly every topic

How will LLMs be trained if no humans are making learning materials?

usefulcat

This is the sort of work that makes it possible for AI to be useful at all.

beej71

I view AI as a challenge. It means I have to really raise the bar. :)

kevinqi

I don't disagree, but since the quality of AI is largely a function of the quality of human content, there's always going to be value in well-written human content. If humans stop producing content, I think the value of AI/LLMs drop significantly as well.

yapyap

Wow 200+ pages, will def have fun sifting thru this one. Thanks!

lprd

A new Beej guide? In this economy!?