An introduction to Version Control - Part 2 - Git basics

Version control – Git basics

Now that we have Git installed on our machines, I’m going to show you some of the different commands in Git. Grab a coffee and let’s do this!

If you don’t have Git installed on your machine, please click here to see the previous version control article, which explains more about the theory of Version Control as well as talking you through installing Git.

Ok, first of all let’s recap from the previous article. Open Git Bash or its alternative for your system. Now type “git” (without the quotes) and hit enter.

You should be presented with some information as per the image below:

Git

If you get any sort of error instead, you may have to go back and install Git again. Click here for part 1 which will run you through that.

Basic configuration

We will now configure our own information with Git.

Enter the following:

git config –global user.name “Your Name Here”

Replace “Your Name Here” with your name, then hit enter. Next up, enter your email as follows:

git config –global user.email “your_email@youremail.com”

Again, please use your own email address and then hit enter again.

Please note: If you intend to continue on from this article and use GitHub, you’ll want to use the name and email address that you intend to use to register with GitHub.

Git commands

We’ll now go over a few of the commands frequently used with Git, which are Unix commands. Feel free to try these out as you progress through them in order to help you understand.

To list your current working directory, use the following:

pwd

To change your current working directory, use:

cd

cd on its own will take you to your default home directory. cd just stands for “change directory” and can be used in different ways, for example:

cd directoryname

Changes the current directory to directoryname, or alternatively:

cd ..

Moves you to the level above your current working directory.

Another similar command is

mkdir

mkdir is to “make directory” and will allow you to create directories using Git

git init

This command initialises Git on your current working folder, making Git pay attention to it. When you enter this Git will tell you it has initialized an empty Git repository in your current directory.

git status

This will tell you the current state of Git, for example if you’ve added files to your current project folder that are untracked by Git, it will flag this with you here until the files are added to Git.

touch

This allows you to create files using Git, for example “touch Readme.txt”

git add

As mentioned earlier, git status will flag changes to your repo. “git add” allows you to ensure Git recognises any new files. For example, “git Add readme.txt”

git commit -m “adding Readme.txt”

The “commit” command allows you to snapshot the current version of the file. This is one of the most important functions of Git, where you make it take note of the current version of files and what changes have been made to them. -m is a flag to tell Git that you are also adding a message, followed with the message in quotes. This is done as standard with commits so that you know what changes have been carried out at each stage of the development process.

In the next article we’ll discuss setting up a GitHub account, connecting Git to a GitHub account and pushing out your commits to a remote location.

by Paul Devine on 22/12/2014

Let's talk

We're here to help. Send us a message and we'll get back to you as soon as possible.

Get in touch

Guides and Resources

View Resource Library