Skip to content

Getting Started With Git

Abinav Seelan edited this page Aug 31, 2016 · 6 revisions

Installation

Linux

If you are on Ubuntu, you can just run the following commands to get the git package from the Ubuntu Package Repository

 $ sudo apt-get update
 $ sudo apt-get install git

Windows & OSX

Visit git-scm. Download. Install.

==============

To verify if git has installed correctly

 $ git --version

This should display the git version.

Cool. Now that we have git on our systems, let's dive into some simple commands to get started!

Your First Commit

Ok. Let's talk about commits for a second. Git works on the concept of snapshots. If the history of file changes is a video then a snapshot is a screenshot taken anywhere from that video. Committing is the act of creating this snapshot. That's it! =)

Setting things up

To start things off, let's create a folder to hold our files, shall we?

 $ cd ~
 $ mkdir project_folder
 $ cd project_folder

Executing that will create a folder called project_folder in our home directory and move us into that folder.

Initialising a Git Repository

A repository is a place that holds all those snapshots we create of our files. To initialise this folder to be a repository we run the following

 $ git init

Once you run that, you'll notice that the folder now contains a .git folder. We won't go into a lot of detail on what it contains, but just think of it as housing all those snapshots in it. =)

And that's that...your folder is now a Git Repository. Woohoo!

Adding your first file

Let's create a file in your folder now and add some stuff to it.

 $ touch file1.txt
 $ cat >> file1.txt
   Hello World

This creates a file called file1.txt in your project directory and adds the line Hello World to it.

Now let's see what Git is seeing of our project folder.

 $ git status
   
   On branch master

   Initial commit

   Untracked files:
     (use "git add <file>..." to include in what will be committed)

        file1.txt

   nothing added to commit but untracked files present (use "git add" to track)

As you can see, Git shows that we have an untracked file. An untracked file is a file that the VCS has never seen before in your folder, ie. file1.txt.

Also, Git is known for it's extremely helpful prompts. Here we see nothing added to commit but untracked files present (use "git add" to track). So let's do that. Let's move our new file into the staging phase that we talked about before. (If you skipped that, you can find it here

To do that, run the following

 $ git add file1.txt

Now run git status

 $ git status

   On branch master

   Initial commit

   Changes to be committed:
     (use "git rm --cached <file>..." to unstage)

          new file:   file1.txt

Cool. We've now moved the file into staging. Now all that's left is to package this into a snapshot by committing it. =D

 $ git commit 

This should take us to a place that looks like this:

 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.
 # On branch master
 #
 # Initial commit
 #
 # Changes to be committed:
 #       new file:   file1.txt
 #

This is the terminal editor, asking us to enter a message to explain what this snapshot is so that someone looking at the snapshot can understand and have some context. =)

Just add a message right above all this text....like as follows:

 Added a really helpful file!
 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.
 # On branch master
 #
 # Initial commit
 #
 # Changes to be committed:
 #       new file:   file1.txt
 #

Ok. I'm sure you're wondering why you can't type anything at all...and the editor is going all over the place. that's because the default editor for the terminal is vim. To figure out how to use vim, visit this link here.

And that's that! You've now created your first snapshot of your code! To see it,

 $ git log

 commit 63a21a93491c66d3295e212c02113a14112c8f66
 Author: Abinav Seelan <abinav.n.seelan@gmail.com>
 Date:   Wed Aug 31 17:44:26 2016 +0530

     Added a really helpful file!

git log shows the list of snapshots that are being maintained in the .git folder. You can see your recent commit there along with the message that you added in the previous step! =)

You must be wondering what that 63a21a93491c66d3295e212c02113a14112c8f66 is right? That's called the SHA. It's a tag the .git folder adds to your snapshot. So whenever you do something with git and reference that SHA, the .git folder will know what you're talking about.

The next step!

Let's see what happens and how git sees our folder when we modify a file.

Open up file1.txt in your favourite text editor and add some more lines. We're going to use the command line here for this, but you're free to use notepad if you want. =)

 $ cat >> file1.txt
 This is line two
 This is line 3

Now, as we'd done previously, let's see what git sees.

 $ git status
 On branch master
 Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file1.txt

 no changes added to commit (use "git add" and/or "git commit -a")

Notice that there's something different here. While in the previous exercise we saw that there were some untracked files, we see that git now tells us that file1.txt has been modified.

This is because the file is now known by git as it's seen the file before in a previous snapshot (also called tracking). It now prompts us saying that file1.txt has been updated and if we want to create another snapshot, we can do it by adding the file first.

 $ git add file1.txt

Clone this wiki locally