This workshop will be retired on May 31, 2020.
Git-ing Up and Running15:55 with Gabe Nadel
Time to put your money where your mouth is! Let's use what we've learned about Git and Github in a live Xcode project. We'll cover: creating repos, making commits, exploring GitHub and pulling changes.
Welcome back, it's time to put what we've learned about Git to use. 0:00 In this video we'll take the basic steps to get you off and running with Git for 0:03 your own projects, as well as share projects. 0:07 As I mentioned before, there's much more to explore with Git and 0:10 I've linked some of those resources below. 0:13 For now, we'll tackle the basics by setting up a GitHub account. 0:15 Creating a new repo, adding our new Xcode project, making commits, 0:20 exploring GitHub, and pulling changes. 0:24 In the next video, we'll use a different account to access our repo to show how 0:28 this process looks from a collaborative perspective. 0:32 When we do this will demonstrate how we, create a new branch, make commits, 0:35 merge our branches, and resolve conflicts. 0:39 So for starters, let's create a GitHub account. 0:43 Some of you may already have one, but just in case, let's walk through that. 0:46 We'll head to github.com, and we'll wanna pick a username. 0:50 You'll want that to be something relatively professional as potential 0:55 employers and teammates will be seeing it. 0:58 I'll create one here. 1:00 Then we'll choose our plan. 1:17 As you can see, having public repositories, 1:20 unlimited repositories in fact, is free. 1:22 You can also get private repos too as per this chart here but 1:25 we'll just go with free for now. 1:28 By the way, the term private just means they aren't open to the public. 1:30 Private repos can still be shared with others but only by invitation. 1:34 Okay, there we go. 1:39 We now have a GitHub account. 1:40 Now we'll set up a repo here. 1:42 But it looks like we need to verify our email address. 1:46 I'll go do that offline and be back in a second. 1:48 All, right we're back. 1:51 We verified, so we can click on that button again to create our new repo. 1:54 Okay, we'll enter a repo name. 1:57 And we can make that, gitDemo, TH for treehouse. 2:00 The description is optional but we'll add one. 2:08 We're gonna leave it as public. 2:16 We won't initialize this with a README but 2:18 that is a good thing to do on a real project. 2:20 And we'll create it. 2:23 Okay, now on this page we see a few ways that we can get going with our repo. 2:25 We can do a quick set up here and that's what we're going to do. 2:30 I'm gonna copy this link right now. 2:33 We could create a new repo on the command line, push an existing repository, or 2:36 import code from another repo. 2:40 Were not gonna talk about these two but 2:43 let's talk about the command line just for a second. 2:44 Using git via the command line or terminal on your Mac is a very common way to go. 2:48 Many devs do much of their gitting via terminal and as you can see here, 2:53 the commands you would use are pretty familiar, even already. 2:57 Stuff like commit and push. 3:00 Now, you don't have to use the command line for this stuff. 3:02 But as you move forward, you may find yourself in situations where you'll be 3:05 tackling an issue and the solution really requires you to. 3:08 Don't be afraid when that happens. 3:12 It's something you'll need to get comfy with eventually anyway. 3:13 If you're really itchy to get acquainted with git from the command line, 3:17 I've linked a comprehensive Treehouse course on git below. 3:20 Which covers that, as well as a whole lot more about git. 3:22 So we've got our link here, and we're gonna open up Xcode in a second. 3:26 But before we do, I wanna make sure that this machine is configured correctly. 3:30 It's a shared machine, and you might not need to do this at home, but 3:36 it's a good thing to know. 3:39 So we will go to the command line briefly and 3:41 we're gonna type in the following command. 3:43 And I'll put in my new username. 3:49 We'll type in one more with my email. 3:55 Okay, let's open up Xcode and 4:08 we'll create a new project. 4:12 We're gonna make that a single view, And we'll call it getDemoTH. 4:16 We're gonna make it a Swift project, click Next. 4:24 Now we have a choice, and we want this checked. 4:28 Create Git repository on My Mac. 4:31 Now note, doing this will just create a local Git repo. 4:34 Remember, Git is the system that can track all our commits, branches, and the like. 4:37 You can simply use Git locally if you want. 4:41 But GitHub, the hosted service, will allow you to share your project very easily and 4:43 we'll set that up in a minute. 4:48 First, let's make a couple small changes to our project. 4:53 We can hop into the storyboard. 4:57 And let's change our background color. 5:01 Let's make it, I don't know, dark gray. 5:07 Then we'll jump into our ViewController and 5:12 let's create ourselves just a little string constant here. 5:15 We'll say let userName = "dummyUser". 5:19 Now we aren't actually gonna do anything with this code, really. 5:29 I just need to make some changes so we can commit them. 5:32 Speaking of which, let's do just that. 5:35 To commit you, go to Source Control and then Commit. 5:37 As we can see now, in the left panel, there are a few files containing changes. 5:43 The two we've seen are the storyboard changes, and here in ViewController.swift. 5:49 These changes are happening just by virtue of us opening up the project. 5:55 Let's look in Main.storyboard first. 5:59 Now, this code probably looks a little cryptic to you. 6:02 But if we scroll around, you're gonna see that we've got the backgroundColor here. 6:05 And that's in this tinted area. 6:12 Now, the tinted area joined by an oval bar here indicates that there's been a change. 6:16 All the plain white area here is unchanged. 6:22 In looking at the two panels, 6:25 we see on our left, we've got the Local Revision that's AKA, 6:26 the project containing the changes we just made but haven't yet committed. 6:31 And on the right we have the previous committed state, 6:35 which in our case is just what the project was when it was first created. 6:38 If we head over to the ViewController file, 6:43 we see the line with our string constant is highlighted. 6:46 If we click on the little oval bar in the middle, we see we can choose not 6:49 to commit it or we can discard the change if we feel like it. 6:54 We won't do that now but you may want to someday, so 6:58 it's good to know about that option. 7:01 Sometimes you'll make a lots of changes to a file and 7:03 then at commit time you realize you have some extra stuff hanging around. 7:05 And this is a good chance to get rid of it before committing it. 7:08 If you wanna cherry pick just a few of the files to commit, 7:12 you can use these check boxes, on the left. 7:15 Now down here, we need to enter a commit message. 7:20 Let's say changed bgColor and 7:22 added constant. 7:28 Now it's worth mentioning that very often they'll be lots of changes in 7:35 a single commit. 7:39 So you wouldn't give it this level of detail, 7:39 you'd more wanna describe the feature that you're working on or something like that. 7:42 If you're addressing a bug, you might wanna insert the bug number. 7:47 And if it's a large feature, perhaps the feature and 7:50 what specific changes you made. 7:52 But usually you aren't gonna wanna list individual constants or 7:53 color changes like that. 7:57 Now down here, we see a grayed out check box where we could push to remote. 7:59 But, alas, no remote is available. 8:04 That's because we haven't yet told Xcode about our GitHub repo. 8:06 Let's commit our changes to the local repo, and 8:10 then we'll work on hooking up the remote repo. 8:12 But before we do that, 8:17 let's look at a couple quick things to illustrate what's happening. 8:18 First, let's just try to commit again. 8:22 If we do, we see an empty pane. 8:27 There are no new changes to commit. 8:30 Even if we try typing something in here, our button is still gonna be grayed out. 8:31 Now that's a good thing, 8:37 Xcode won't let us commit since we haven't changed anything. 8:38 Next, let's look at the menu again. 8:42 And where it says hm, getDemo, I guess I misspelled that. 8:45 Says getDemoTH- Master, that indicates we're working on the primary or 8:49 master branch of this repository. 8:55 Which, of course, is the only branch we have so far. 8:58 Okay, now let's hook up that remote repo. 9:01 To do that we also stay in the Source Control menu, and 9:03 we're gonna go down here Configure getDemoTH. 9:08 We go to Remotes > Add a remote. 9:12 Paste in our link from before and Add Remote. 9:17 And there we go, we can click Done. 9:21 Now just for fun, let's look at GitHub. 9:25 We'll refresh this, hm, it still looks the same. 9:30 Why don't I see any commits here? 9:34 Anyone, anyone? 9:37 Well, we did commit it but we committed it locally and we never pushed it. 9:39 Back inside Xcode we can do that pretty easily. 9:45 Again we'll go to the Source Control menu and we're gonna push. 9:47 It's gonna push the local changes to the master branch on the origin. 9:53 And because we haven't created that on the origin yet, it's gonna create it for us. 9:58 There we go, push successful. 10:13 Now let's head back to GitHub, We'll hit refresh and, ooh, very nice! 10:16 Some very familiar stuff here. 10:25 We've got our file structure and the commit messages we entered a moment ago. 10:27 Now, just to be clear, all this visiting of GitHub is just to illustrate the point. 10:32 You don't actually have to check this stuff, Xcode has taken care of that. 10:35 But I wanted to show you what's happening behind the scenes. 10:39 So here we can see that we're viewing the master branch, which for 10:42 now is the only branch we have. 10:45 And we can see that in the dropdown. 10:47 If we had other branches, they'd be listed down here. 10:49 If we look at these folders, we see some commit messages. 10:53 Two of them have the message we entered, and then two of them say Initial Commit. 10:56 Well, that's because of these folders, 11:02 not all of them had contents that were changed. 11:04 So they'll display the commit message from their last committed change, 11:07 which in the case of these two, was the initial set up. 11:10 It It may not seem obvious, but this actually points to a powerful feature. 11:13 You see, our remote repo not only has captured the state of our project now, 11:18 as in when we created the remote or when we pushed to the remote. 11:23 But it has the history before that. 11:27 This isn't all that important since we haven't really done much. 11:30 But imagine you create a local project with a local Git repo. 11:33 You work on it offline for a few days, maybe even a few weeks. 11:37 Maybe you're on a trip and you don't have Internet access. 11:40 Once you finally do set up your remote repo, the entire history will be included. 11:43 Similarly, when you join someone else's project, 11:48 you'll have access to the commits that occurred before you joined. 11:51 So there is a lot you can do on GitHub. 11:55 Much more than we'll go into now. 11:57 So I encourage you to view their tutorials and docs, but 11:59 we'll touch on a few while we're here. 12:01 First of all, you can navigate into the files as they exist now. 12:04 If we head to our ViewController file, 12:08 We can see that constant that we created here. 12:14 Now, often you may have done some committing or pushing or merging or 12:18 something else in Xcode or via the command line. 12:21 And looking here in GitHub is a good way to see what actually made its way to 12:24 the remote. 12:28 By the way, I don't mean to insinuate that working with a command line or 12:28 GitHub Isn't reliable, but at times you or a collaborator may commit but 12:32 forget to push or something like that. 12:36 And this is a good way to peep behind the curtain and 12:38 figure out what has happened really easily. 12:40 You'll also notice that up here, we could 12:42 open this file in GitHub Desktop which is a utility you can download for Mac. 12:47 You could edit the file right here, which isn't something you do all that often but 12:52 we will just for fun in a minute, or you could delete it. 12:55 So let's edit this. 12:58 And we'll call this "dummyUserChanged". 13:01 You scroll down and we say, the commit message could be, dummy user updated. 13:05 And we'll commit it, oops, didn't put the right commit message but that's okay. 13:12 By the way, it's worth mentioning that, if your commit message is getting too long, 13:16 you can enter a longer commit description. 13:21 Which might be useful to someone who may come back and look at your code months or 13:23 years from now. 13:27 It's like leaving bread crumbs and it's very helpful to other developers. 13:28 You may be that developer one day and you'll certainly appreciate it. 13:31 Now if we go back to the main page, 13:35 we can see that now we have three commits up here. 13:39 There used to just be two. 13:43 If we click on them, We can see we have our initial commit, 13:45 the one we made in Xcod,e and the one we made just now. 13:50 Off to the right, we see two links for each commit, these are really important. 13:54 Clicking the left button, this cryptic ID, will show us the changes that were made. 14:00 Here we see a- sign signifying the line that was replaced and 14:06 a + sign with the new line. 14:10 Now of course, I didn't actually delete this part, let userName =. 14:12 I just typed in the word change but 14:15 this is how it shows us this line has been replaced with this line. 14:17 If we head back to that list of commits, And click on the set 14:22 of angle brackets over here, we can browse the repository at this point in history. 14:27 Now that's our most recent commit, it doesn't seem all that exciting. 14:32 But if you wanted to browse an old version of a project, 14:36 especially a really big project, this could be really, really helpful. 14:40 Remember in the last video we talked about how our cowboy coder added a feature, 14:47 then deleted it, then the client wanted it back? 14:51 Well, by viewing the state of our project at a past commit, we could hunt through 14:54 the files, find the code we need, and pop it back into our project. 14:58 Even if it's been completely deleted since. 15:01 All right, let's head back to Xcode. 15:04 Well, here we see we still have this old value for the constant. 15:08 How would we get the new stuff from our remote branch? 15:12 Well, we simply need to pull. 15:15 We head up to Source Control and we Pull. 15:17 And look at that, it updates right before our eyes. 15:25 Now the same thing, of course, would have happened if a collaborator on a project 15:27 had changed that and pushed it to remote. 15:30 Wel,l if you don't think that's impressive now, maybe you will one day 15:33 when you're pulling dozens or even hundreds of complex changes. 15:36 Maybe made by several collaborators and it somehow just works. 15:40 We've come to a nice stopping place but we aren't done yet. 15:44 We haven't seen how to create a new branch and merge changes. 15:48 In our next video, we'll wrap up the workshop by doing just that. 15:51 See you real soon. 15:54
You need to sign up for Treehouse in order to download course files.Sign up