Setting Up Git
00:00 Welcome back! Now we’re going to set up the Git version control system, which has kind of become a standard for version control over the last couple of years.
00:10 In my case, it’s absolutely indispensable for keeping my source code safe and working with other people. So now I’m going to show you how you can set up Git and then make some tweaks to Sublime Text so it integrates really well with your Git workflow.
00:25 I’ll walk you through the lightweight Git integration that I use in my own development workflow. Of course, if you’re not using Git in your personal workflow and you don’t plan to, then of course feel free to skip this video and basically everything that mentions Git.
00:41 But if you’re using Git then stay tuned and we’ll get this to a really nice setup and a really nice integration with Sublime Text. As a first step, I want to make sure that you have Git installed correctly.
00:56
And so on Ubuntu, you can ensure that by running the following command, sudo apt install git
, and this will install Git through the system package manager.
01:08
In my case, because of the brand new install, I still needed to install Git. And once that’s completed… It should complete really quickly, yep. Once that’s completed, we can open or we can stay in that terminal window and just type the git
command. And this should give us some output here, which shows that we’ve installed Git. Now we need to adjust a few basic settings here before we can actually use Git in a sensible way.
01:38
So, the way you do that is with the git config
command. And for these settings that we’re going to change, you want to start this command with a --global
.
01:49
You want git config --global
and then user.email
. All right, so I’m just going to punch in some placeholder here. You would obviously replace us with your real email, or let’s say the one you’re using on your GitHub account, if you’re using GitHub. And then the other command I need to run is setting the user.name
. And again, obviously put your real name there. And yeah, now once we’ve set this, we can actually check and make sure these settings were stored correctly by running this command, git config --list
,
02:35 and this should show you the right values that we’ve set before. Okay! So, this is actually it in terms of the Git command line setup, so we can get rid of that. And now the next thing I want to show you is a great plugin for integrating Git with Sublime Text, and that is a plugin called GitGutter.
03:01 It basically highlights the lines modified since the latest Git commit.
03:07 You might be wondering, “Why is this useful?” And it doesn’t seem like much, but it’s actually super useful as you’re writing your code. Let’s say you had an unwanted change in your code, like a debug print statement or something like that, and you just wanted to see which lines were modified.
03:24
You can always do that from the command line and do a git diff
for example, but GitGutter basically brings that git diff
directly into your terminal window.
03:32 So I feel like it just helps for awareness in knowing what changes your made to your code. It’s a really handy plugin. Actually, before we start out, I just wanted to clone an example repository here we can work with.
03:49 This is one of my own. If you have some other code that you want to use, then feel free to use whatever other Git repository you have available, but I just want to make sure you have some example code.
04:01 So this is just cloning one of my open-source projects so that we have something to work with. Okay! Let’s get rid of the terminal again, go back to Sublime Text. And again, we’re going to open the Command Palette, find the Install Package function, and then search for the GitGutter package.
04:21
This is going to install in the background. And now what I’m going to do, I’m actually going to open the folder on that repository that I just cloned with the git
command on the command line. We can close that message from GitGutter. Okay, so now this is just looking at this Git repository.
04:43 It doesn’t look very interesting. But what I’m going to do now is I’m just going to open a Python file here
04:50 and I’m going to make a bunch of changes to it. All right, so I’m going to just make a couple of changes here, some random changes. Let’s insert a couple of new lines,
05:02
# this one is two
. Yay! And then I’m also going to delete a bunch of lines here, and maybe you’ve already noticed these icons here in the sidebar.
05:18 All right, so as I’m updating the file, GitGutter is putting these little icons here and it tells me exactly what’s going on, so I know that I modified these lines and I know that something was deleted here and that I inserted these lines or these ones are new.
05:36 And this is a very low-key way to tell you about the changes made to your file. And this actually also works here for the minimap in the sidebar. It’s kind of hard to see in the video probably, but it kind of highlights the changes here.
05:49 So, I find this to be really helpful just to be aware of the code that changed as I’m working on it. And so this is a plugin I always use—like, I always bring that in. There are a couple more really cool features.
06:05 When you hover over these changes,
06:10 then, for example, it shows you, “Okay, these were the lines that were deleted,” and you can actually roll that back by clicking this button here and it would re-insert the same lines. Of course, you could just undo that.
06:20 So, I find this is a really lightweight way to actually work with Git from inside Sublime Text without moving all the functionality into Sublime Text. Like doing a Git commit, I still personally prefer to do that from the command line, but it’s awesome to have a little bit of support here and to see the changes you’ve made. And I could also roll back this thing, for example, this change that I made here. So GitGutter is great.
06:48
I really feel like it adds a lot to the Git and Sublime Text setup that I’m using. Next up, I want to show you a way you can integrate the git commit
command with your Sublime Text editor or, basically, use Sublime Text to write your commit messages without actually using a plugin that completely integrates Git into Sublime Text.
07:11
again, I want to show you a little example here, so I’m just going to create a little test repository from the command line and I’m going to create an example file. So to show you what I’ve done here, I’ve created this myfile.txt
in the test-repo/
folder.
07:29
And now I’m going to add this file, I’m going to stage this in Git so it knows we have a new file here. And now I’m going to do a git commit
.
07:41 And what happens by default is that this actually pops you into the default command line editor, which is not Sublime Text, right? And it isn’t a nice experience that this bounces us to nano or, you know, to Vim, or whatever command line editor you have defined.
08:01 It’s not a super nice experience to be working with that from the command line, so we’re going to exit this editor and this works by hitting Control + X and that will bounce you back to the command line.
08:11 What I’m going to show you now is how you can set Sublime Text as your Git editor so that you can actually write your commit message inside Sublime Text.
08:21
And again, we’re going to do this with a git config
command. This is a global setting and it’s the core.editor
setting.
08:29
And for that, we’re just going to set it to subl --wait --new-window
.
08:38
The subl
command is what launches Sublime Text, and so we’re just telling Git to use Sublime Text as our Git editor. So in this case, I’m going to run that commit
command again, I’m going to run git commit
again, and now you can already see it popped up Sublime Text and it’s actually waiting for the Sublime Text window to get closed.
09:03 So, now I could type out my commit message,
09:08 and then I could trigger that commit, but there’s actually one more thing I want to show you here and that is how you can get syntax highlighting for Git commit messages.
09:20 Again, we’re going to pop up the Command Palette and in find the Install Package setting, and then we’re going to find the Git Commit Message Syntax.
09:34 It’s this one here, Syntax Highlighting for Sublime Text / Text Mate, Git Commit Message Syntax. And we’re going to install that. All right!
09:42 And once that has finished, I’m actually going to need to cancel this and quit Sublime Text and restart it.
09:51
Actually, I could have left it closed, it doesn’t really matter. But now I’m going to trigger that git commit
command again from the command line, and now it’s going to pop into Sublime Text and now I’m getting nice syntax highlighting. All right, so I can type out my commit message.
10:07 And so it’s a little bit nicer when you’re working with Git from the command line. Now, there are some best practices for Git messages here, for example, regarding the line length recommendation. So for example, you’re supposed to keep the first line here below 50 characters and then the lines after below 72 characters.
10:31 And now, it’s kind of hard if you have to count out these characters manually or kind of go with the selection here. So if you want to keep your commit messages under control with some tool support inside Sublime Text, then there’s actually a really good way to do that.
10:43 First of all, you want to stay in this commit message—don’t close that window yet. And then you want to go to the Preferences menu options, and then click on the Settings - Syntax Specific.
10:56 This is going to pop up a new window with your syntax-specific settings for commit messages. And what we’re going to place here now is a new setting that adds so-called rulers.
11:14
So just add this here, "rulers"
and then a list of [50, 72]
, and I’m going to save this and close it. And now I’m getting these rulers, right? So these lines here, these are the rulers. I’ve got one at column 50, one at column 72. So for typing out the first line, you can see, okay, this would already be too long.
11:37 So if I want to keep that within the standards, then this is probably what I would do.
11:45 And then the second line could be a little bit longer. And that way, I’m guaranteed to stay within those recommendations or those recommended line length limits, right? And if you actually want to learn more about these best practices, there’s a really good tutorial that I would recommend.
12:04 And you can find that out at chris.beams.io/posts/git-commit/.
12:15 And the link is also in the PDF version of the course. So, he has some really good things that are some really good background info on the way you should structure your commit messages and I try to follow that, so I highly recommend that tutorial. Okay, so now let’s actually trigger this commit. I’m going to hit Save here, Control + S, save that file. And then close the window, and it’s going to bounce me back to the command line and Git just told me that it created this commit under my name and with the commit message I had in Sublime Text.
12:49 So, this is really handy, and this is kind of the workflow that I use. I like to use a combination of the Git command line tool and Sublime Text.
12:57 And now, there’s one extra thing I want to show you around using Sublime Text from the command line, because this is actually highly useful I find. A lot of people that I know in my personal development workflow, it kind of centers around command line tools because it gives you a lot of flexibility. But it’s extremely handy if you can jump to your editor—for example, Sublime Text—really easily from the command line.
13:20
And so Sublime Text has this subl
command, subl
,
13:27
that you can use to interact with Sublime Text. So if you just run subl
, for example, it’s going to bounce you to Sublime Text. So actually, if I close this window and do it again, it’s just going to bounce me back, right? Now, you can also use the subl
command to open an existing file. So for example, you could go subl example.py
and that would open this file inside your existing Sublime Text session.
13:54 You can also create a new file, of course,
13:58 just by giving it a name that doesn’t exist yet. And then that would create a new file for you, it would already set the syntax highlighting to Python based on the extension, and then you could type out your code and then save that, and the file would actually be created.
14:15
Another command that I find extremely helpful is the ability to open whole folders inside Sublime Text. So again, you can use the subl
command and then just pass it a directory name and run that,
14:28 and it opens up a new Sublime Text window with that folder open and you can browse it and work with it. So I find that extremely handy for working, switching between projects—I can do all of that from the command line.
14:41
And actually, if you’re already inside a folder here—so let’s say I want to open my Desktop/
folder inside Sublime Text for whatever reason—you can just go subl
and then do a dot (.
) and it’s going to open the current folder.
14:56 All right, so these are some shortcuts you can take. It’s a lot easier than going into Sublime Text and then doing an Open Folder and selecting the folder you want. So this is a huge time-saver I’ve found.
15:09 All right, cool! So, this concludes this section of the course. Okay, great! Just to recap, now we’ve installed Git and we’ve integrated it with Sublime Text so that you can see what changes you made to your code as you go along and kind of compare to the latest version in Git.
15:27 And also, we’ve integrated it with your Git commit messages so you can work from the command line and create your Git commits from the command line and will hook that up into Sublime Text.
15:38 And then also I showed you a couple of commands you can use to interact with Sublime Text directly from the command line.
You must own this product to join the conversation.