Git Tutorial

This is a complete beginners tutorial on GIT version controlling system.

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 hello 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 the repository

Create the below java class under the directory hello

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

Checking status of the 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

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

Modification on the working directory files

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

Modification to the HelloWorld.java file

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

Checking the status

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

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 the 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 the changes

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

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 the 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

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.

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

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

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 *

Time limit is exhausted. Please reload CAPTCHA.