Check out a free preview of the full Complete Intro to Web Development, v2 course:
The "Working with Git" Lesson is part of the full, Complete Intro to Web Development, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian shows how to use basic git commands to commit and share code to a project.

Get Free Access Now

Transcript from the "Working with Git" Lesson

>> Brian Holt: We're gonna work with Git just a tiny bit. So if you have a bunch of people all working on one project, you need some way to collaborate. And it's not like Google Docs where we just all open the same file and start editing it. That would be absolute pandemonium.

[00:00:15] So what we do is we have this thing called a source control management system. There's quite a few of them, there's CVS, there's Mercurial, there's Git, there's Bazaar. And there's other ones that I'm not thinking of, nor do I particularly care about. Git is the one that everyone uses, and particularly in the friend world I don't know anyone that uses anything other than Git.

[00:00:40] Except maybe Facebook, Facebook for whatever reason decided to use Mercurial. Git is written by the same guy that wrote Linux, Linus Torvalds. He said he named it after himself, because git is a British slang word for an idiot. His words, not mine. So what we're gonna do is I have this, let's go back into one of our other projects.

[00:01:03] Like we were doing the popdemo one, this is actually, let's do the calculator. So cd calculator, I have three files in here and let's say I wanna put this up on GitHub so that I can share with other people. GitHub is where you can share a code with other people, that's, it's the hub of git follows, right?

[00:01:25] Fist thing I'm gonna just like I did MPM I'm gonna say git init. So, now I begin an empty directory, you can ignore these three things. This is, my computer freaks out when there's no commits. Yours won't, just mine. So what I'm gonna do.
>> Brian Holt: git init, like it doesn't really do much else.

[00:01:46] If I hit ls -lsa, you can see here that there is a new git directory here. This is how Git keeps track of everything. So don't delete your .git folder, you will delete all of your source control. So here I'm gonna say git add.
>> Brian Holt: Is this I what do next?

[00:02:07] I gotta make sure I stay on track, git add. So there's kinda two stages to something being add to git, there's staging and there is a commit. Okay, so when you add things to stage, you stage a bunch of files and then you create one atomic commit. Which is like one update, one patch that you send over to your server.

[00:02:30] So what I'm gonna do, is I'm gonna add everything, but you can add things individually. I could just add calculator.css individually and calculator.js but that's burdensome. So you can either say, git add. So now I have things and if I hit git status. Sorry, again ignore all this error messages.

[00:02:49] git status,
>> Brian Holt: You can see it here that I have these three files that are being, they're ready to be staged. So changes to be committed.
>> Brian Holt: Okay, so now if I say, git commit -m "initial commit for the calculator". You want to make this some sort of useful error message to your future self.

[00:03:16] That's like, I added this feature or I fixed this bug or something that you can look at in the future and say okay, that's what this commit did. Okay, so I'm gonna say, this is the original commit. That's usually enough for the first commit. And then now, my error messages stopped, which is nice.

[00:03:33] And now I have commit, right? So now I have one, I've started tracking my project. So if I hit git status now, nothing has changed since the last commit. So now if I made another change to it, let's just code.
>> Brian Holt: And I come in here and I change maybe like, maybe I change the title of this to be Calculatorv2.0, right?

[00:04:05] And I save that, and I hit git status, it's gonna say, hey index.html has been changed since I last seen it. So this is a new change that if you wanna commit it, you can. And actually I can say git diff, please show me what has changed. And it's gonna say, you took this out and you added this, which is cool, right?

[00:04:31] So now I wanna add that, so I'm gonna say git add index.html, and if I hit git status again I can that it's ready to be committed. So I'm gonna say git commit -m "updated the title of index.html", okay? And so now I have nothing to commit. And I can look at my git log and see the last commit was updating the title of index.html.

[00:05:02] It was at this time by this programmer. And you can see down here was the initial commit. So you can see all the past commits as well.
>> Brian Holt: Make sense?
>> Brian Holt: If you want more commit, there's a lot of important parts of git that I did not talk about.

[00:05:21] I did not talk about pulling, or cloning, or merging, or rebasing, or stashing, or checkout, or branch. And these are all commands that you probably need to know. My coworker also works at Microsoft with me, and she also worked with me at Reddit, and she's super smart. Nina Zakharenko, teaches a course on Git that is worth your time.

[00:05:42] And Git is definitely a tool that is not very fun to use, but is very much worth investing your time into. Because If you mess up Git, you delete everything, right? You lose changes and things like that, and it's quite disastrous. But the cool thing about Git, it's really hard to permanently screw up.

[00:06:01] So if you screw something up, I guarantee you can get back to where you were, if you know the right Git commands. So it's worth your time to figure out, like how do I un-shoot myself? There's a site called ohshitgit.
>> Brian Holt: So, it goes through a bunch of [LAUGH], fuck this noise, I give up.

[00:06:25] That's hilarious. It's a bunch of shit things. Things like, shit I did this and it tells you how to get out of these, so.
>> Brian Holt: Really easy to screw up Git, just so you know. And you also would definitely not be the first and you will not be the last.

[00:06:42] [LAUGH]