In this lesson, you'll learn how to commit your lab files to version control. The following information about Git and GitHub is stripped down to the basics that you need to get started. If you want to dive deeper into using Git and GitHub for version control, check out the dedicated course on Version Control with Git and GitHub.
Create Repository in Git
If Git isn't yet installed on your system, then follow the instructions for your operating system on the official Git documentation on installing Git.
What is a Git Repository
Git uses the idea of a repository, which you can think of simply as a top-level folder. Anything inside a Git repository will be tracked under version control.
Note: It's important that you only set up Git repositories in directories that you actually want to track.
A common mistake when first getting started with version control is to accidentally turn your entire computer into a Git repository. Instead, you only want to track the directories that contain your coding projects. You'll also want to have a separate Git repository for each one of your coding projects.
Git Init
The first thing you'll want to do is to open your terminal. Use it to move into the directory that contains all your course labs. If you've named the folder labs and placed it as suggested at the beginning of the course, then you can go there by typing:
cd ~/Documents/codingnomads/labs
This directory contains folders with your coding exercises, and it's the project that you want to start tracking with Git as a repository. Once you are inside this folder, initialize a new empty Git repository with the following:
git init
You should see an output message similar to this:
Initialized empty Git repository in /Users/martin/Documents/codingnomads/labs.git
Congratulations! You've just set up a new Git repository. However, you haven't yet put any of your files under version control.
The next thing you need to do is to add your project files to the Git repository so that Git knows what to keep track of.
Add Files to Git Repo
To add a file, you use the git add <filename> command for each file or directory that you wish to add. You can also add more than one file at a time. You'll learn how to do that later.
Git Add
First, you'll learn to add a single file to Git for tracking. The project folder you downloaded contains a file called README.md. Add this file to Git by typing:
git add README.md
The README.md file is a file that contains information describing this project. The process works the same for any other file inside this folder. Focus on what happened in Git.
But... how would you know what happened?
Git Status
Git has a handy way of checking what's currently going on inside of its belly. You will use this command very often when working with Git. Check the status of your Git repository by asking Git What's up?:
git status
Git will dutifully reply with a couple of lines of output that will look similar to this:
On branch master
No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage)
new file: README.md
...
Note: Your output will be much longer since Git also tells you about all the files inside of the repository that you haven't yet added. For now, only focus on the top of this message and ignore the rest. You'll learn more about the rest shortly.
You can see that README.md is highlighted in green. This means that the file has been added to the Git repo, but it has not yet been committed. Git uses a two-step process when putting your files under version control. By adding files, you decide which ones to bundle up together, and by committing them, you give that bundle a name and register it in Git's version tracker.
What is Git Commit
By completing the first step with git add, you have added README.md to what is known in Git as the staging area. It means that the file is ready to be committed to the Git repository, which represents a separate step.
The staging area allows you to add any number of files to be included in a single commit to the Git repository. Each commit you make to a Git repository marks a snapshot of the history of your project. It's like writing an entry to your project's very own history book.
Info: Git also allows you to time travel. You can not only check out any commit in your project's history, but you can also revert back to that point in time in the history of your project. Pretty neat, huh? You can also see exactly who committed which code and what code was committed, and read the label on each commit, the so-called commit message.
The Initial Commit
Go ahead and make your first commit. You will start by labeling the file bundle you created with the required commit message. While this commit only contains one file, keep in mind that this bundle can consist of multiple files.
You can add the commit message directly to the git commit command by adding it wrapped in quotes after the -m option. Your first commit message will be:
Initial commit: Add CodingNomads online labs
And the full commit command, including the message, will look like below:
git commit -m "Initial commit: Add CodingNomads online labs"
Note: If this is your absolute first time committing to Git, you might be presented with a question about your identity. To settle Git's concerns once and for all, you can follow the instructions printed to your console and fill out the quick form for your name and email: git config --global user.email "[email protected]" and git config --global user.name "Your Name" Remember to use the same email address you used or will use to sign up for GitHub.
Your terminal should now present you with text output resembling the one below:
[master (root-commit) f1ad783] Initial commit: Add CodingNomads online labs 1 file changed, 265 insertions(+) create mode 100755 README.md
This output tells you that the file has been committed to your local Git repository. Congratulations, the README.md file is now under version control!
As mentioned, a commit in Git marks a moment in time of your project. Once it has been recorded, you can always go back to this moment or any other commit, should you need to in the future.
Additional Resources
- CodingNomads: Version Control with Git and GitHub course
Summary: Git Repository and Git Commit
- A repository is a top-level folder, and anything inside will be tracked under version control
- It is important to choose your repository wisely and only track files that you want to track
git statusreturns the current status of your git tree- A git commit marks a moment in time of your project
- Once a commit has been recorded, you can always go back to this moment or any other commit
Steps to Create a Git Repository
- Initialize a new Git folder with
git init - Add the file to Git's staging area with
git add <filename> - Commit the content of your staging area to version control with
git commit -m "your commit message"