Beginners Git Tutorial

This is a complete beginners tutorial on GIT version controlling system. In this tutorial I am going to show you how to create a new Git repository in your local system, how to add new files to the repository, how to commit changes in the existing files and how to check history.

I’ve been using Subversion for a few years and I really like Subversion. Combined with TortoiseSVN, I can’t really imagine how it could be any better.

Yet there’s a growing number of developers claiming that Subversion has problems and that we should be moving to the new breed of distributed version control systems, such as Git.

You may need to read my previous tutorial How to install Git on Windows

Creating a Project

This section is to learn how to create a git repository from scratch.

Creating a Directory

Go to the physical drive D and create a directory called GitProjects and under this GitProjects directory create another directory called hello.

Creating a Repository

So in the previous step I have created a hello directory under another directory GitProjects but the hello directory has not yet been repository.

Now open a cmd prompt and navigate to the directory D:\GitProjects\hello and type the command git init to create a repository hello. You will see the message after successful creation of the git repository on directory hello

Initialized empty Git repository in D:/GitProjects/hello/.git/

Adding a Page to Repository

Create the below java class under the directory hello:

public class HelloWorld {
    public static void main(String[] args) {
        // Prints "Hello, World" to the terminal window.
        System.out.println("Hello, World");
    }
}

But the above java file has not been yet added to the repository. To add the above file to the repository, type the command git add HelloWorld.java.

Now the java file has been added to the repository but has not been committed to the repository, something similar to the SVN concept.

Now to commit the file execute the command git commit -m "First java commit", where -m "First java commit" represents your commit comment. You will see the below message after successful commit to the repository:

[master (root-commit) f7f4e7c] First java commit
 1 file changed, 8 insertions(+)
 create mode 100644 HelloWorld.java

Checking Status of Repository

This section is to learn how to check the status of repository.
To check the status of the repository execute the command git status. You will see the below message:

On branch master
nothing to commit, working tree clean

The above message means that there is nothing left in the repository to commit in the current state of the working directory.

Modification on Files

This section is to learn how to monitor on the state of current working directory.

Modification to HelloWorld.java file

Let’s remove the comment // Prints "Hello, World" to the terminal window. from the HelloWorld class

Checking Status

Run the command git status, you will see the following message:

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:   HelloWorld.java
no changes added to commit (use "git add" and/or "git commit -a")

The first important point here is that git knows HelloWorld.java file has been changed, but the changes are not yet committed to the repository.

Another point is that the status message hints about what to do next. If you want to add these changes to the repository, use git add <file>. To undo the changes use git checkout -- <file>.

Staging Modifications or Changes

This section is to learn how to stage changes or modifications for the upcoming commits.

Staging is a step before the commit process in Git. That is, a commit in Git is performed in two steps: staging and actual commit. As long as a change or modification is in the staging area, Git allows you to edit it as you like (replace staged files with other versions of staged files, remove changes from staging, etc.).

Let Git know about changes

Run the command git add HelloWorld.java. Then again run the command git status. You will see the following message:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
        modified:   HelloWorld.java

Changes to the HelloWorld.java have been staged. This means that Git knows about the changes, but it is not permanent in the repository.

If you want not to commit the change, the status command above reminds you that you can use the git reset HEAD <file> command to unstage these changes.

If you want to add multiple files to the Git repository then execute the command, for example, if you want to add three files – file1, file2, file3 to the repository, git add file1 file2 file3

If you want your all modified files to be added to the repository then execute the command git add -A

Commit Changes

This section is to learn how to commit the changes or modifications.

If you simply execute the command git commit then it will ask for comment message for the commit. If you execute the command git commit -m "<some comment>" then it will not prompt for the commit comment.

When you simply execute git commit then you will see something similar to below window for inputting your comment for the commit and a cursor will continuously blink.

git tutorial

So now press i from keyboard and write the comment as "remove comment" and press Esc from keyboard and type :wq and press Enter key.

You will see the following message in the cmd prompt:

[master 76a6e8d] "remove comment"
 1 file changed, 1 deletion(-)

Now if you execute command git status then you will see the below message and it clearly says there is nothing to commit. So all commits have been made permanent to the repository.

On branch master
nothing to commit, working tree clean

Project’s History

This section is to learn about the history of changes in the project or working directory.

To list the changes in a project, execute the command git log. You will see the history of changes made to the repository hello:

commit 76a6e8d79acf28003d0ba351079bdcadab32af9c
Author: moni <moni@gmail.com>
Date:   Thu Jan 5 10:23:53 2017 +0530
    "remove comment"
commit f7f4e7c6b0a28ecd6cd59ec259fcda48d0174678
Author: moni <moni@gmail.com>
Date:   Wed Jan 4 08:20:42 2017 +0530
    First java commit

The above history shows in multiple lines and if you want to see it in one line, execute the command git log --pretty=oneline. You will see the below output:

76a6e8d79acf28003d0ba351079bdcadab32af9c "remove comment"
f7f4e7c6b0a28ecd6cd59ec259fcda48d0174678 First java commit

You can have other options with the git log command, for example:

To see the history of only last two changes: git log --pretty=oneline --max-count=2

To see the history of changes that have happened since 04/01/2017: git log --pretty=oneline --since='04/01/2017'

To see the history of changes that have happened until 04/01/2017: git log --pretty=oneline --until='04/01/2017'

To see the history of changes done by an author: git log --pretty=oneline --author=<your name>

To see the history of all changes: git log --pretty=oneline --all

More about git log command can be found at https://git-scm.com/docs/git-log

Thanks for reading.

Leave a Reply

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