Git — Initializing a Repository, and Tracking Basics

Let’s dive in. We’re going to look at some of the things you need to know to use Git. Before long, you’ll be using it comfortably, and will start the journey down the long winding road of version control philosophy. You’ll be thinking about things like when to commit, what to commit, and the story your branch history tells. Before that though, we need to take our baby steps.

 

Who is this for?

If you know anything about Git, skip me. You’ll be bored to tears. If you have no idea what version control is, and are getting started programming, this is for you.

Know the command line. Love the command line.

Do yourself a solid. Get to know your terminal, shell, or prompt. Whatever you have on your machine. You’ll be a better programmer because of it. All the commands I show here are to be used there. You can download the GitHub application here, and it will provide you with a Git Shell that you can use to try out these commands which conveniently leads to my next point…

 

Sign up for GitHub

Go make yourself an account on GitHub. It’ll be a place to store your code, and view other people’s code after you have your sea legs. You can follow me here.

 

Git Commands

git init

The first command in our list is git init. This command is used in the topmost directory of your project. This command creates your git repository.

Image-1

Here, we navigated to the top most directory we wanted to track, and ran the git init command. As you can see here, an empty repository has been created.

 

git status

In this section, we will introduce the most common command there is. The git status command will show you what is going on. If your working tree is not clean, meaning your working directory does not match your last snapshot, or commit, this command will let you know that by telling you in what way your working tree differs. It will tell what files are new or untracked, recently deleted, or modified. This command also tries to be helpful, and will let you know how to do common operations depending on the state of your directory.

Image-2

Here we can see a few things. First we can see that we’re on the master branch (don’t worry about that now). The text ‘Initial commit’ means we don’t have any commits yet, and our repository is empty. Our next section is where we find out what is going on in our working tree. It’s telling us that we have an untracked file in the repository, and what we could do about it.

 

git add

Next up is the git add command. This command, in its simplest form takes a file, or directory as its argument resulting in this form :git add <file|directory>(| means Or). This command allows you to place files in what is called the staging area, or index. Files located in the staging area are classified as staged. Files must be added to the staging area to be included in the next commit. Files eligible to be added to the staging include untracked files, and tracked files that have been changed since the last commit.

Image-3

Our first two lines are exactly the same, and can be ignored for now. Next, we can see that we have added our ExampleFile.txt to the staging area. Again, the status tries to be informative, and lets us know commands we could run. One option that we have is that we can run the git rm command along with the --cached option.

 

git rm

This command will remove files from the staging area, and from the working tree. In other words, it will delete it. By supplying the --cached option, the file will no longer be tracked, but will be not be deleted. Here we see the effects of this command.

gitbareminimums_fifth

 

git commit

The git commit command takes all files that have been added to your staging area, and creates a snapshot of it. This will allow you to revert all the tracked files back to their state at this point. Once you run the git commit command, a text editor will be opened for you to write a message to accompany this snapshot. Always write an informed message so you know what changes were included in the commit.

Image-4

After running your git commit command, an editor should have opened. If it was VIM, press i, and you’ll be able to enter text. Once you’re finished entering text, press esc, :x, and then the enter key. Now, we look at our status, and git is reporting that our working tree is clean.

The primary sequence seems to be: 1). Modify file. 2). Add file to the staging area. 3). Commit with a descriptive message. What we can do is shorten this sequence slightly with some added flags to the git commit command. The first flag we’ll discuss is the -a flag. This will take any tracked file, and automatically add it to the commit skipping the adding step. It will ignore untracked files. Next, we can use the -m flag followed by a quoted string to add a commit message. This shortens the sequence to: 1). Modify file. 2). Run commit with -a flag, and the -m flag followed by a quoted string. This is the form I will use going forward where appropriate.

git mv

The last command we’ll look at is the git mv command. This will simply move a file. Renaming a file, even if it does not go anywhere is considered a move. Here’s a quick example of it.

gitbareminimums_eight

 

Summary

We looked at a couple commands to get us up, and running with Git. These commands look pointless, because it’s tough to see what is gained by taking the time to learn, and use Git, but stick with it, and you will find that the benefits far outweigh the costs.

Author: Peter

Thank you for visiting my profile. I encourage you to get in contact with me, and follow me on GitHub : https://github.com/emancipatedMind. I look forward to collaborating with you.

Leave a Reply

Your email address will not be published. Required fields are marked *