“Distributed Version Contorl System”

Store, track,restore and merge code…

3 Stages of Git

1. The things to know when working with git is installation, initialisation and moving code through 3 areas. Firstly Git is to be interacted with on the linux command line. Once git is initialised there are 3 areas of working. The working directory, staging area and local repository. The working directory is were you edit code on your local editor, the staging is an area you move the code into, a place where you can compare, correct and verify before moving it permanently into the local repository.

$ yum install git -y

2. $ Yum install git – to install git on a linux host. Check the version 2.17.2, see the location where it installed and verify that its there.

$ git init

3. I have made a directory where I will be making my project “offers-app”. Now I need to explicitly inform git to initialise this directory and continue monitoring it for change. $ git init – This directory now can be considered as my local repository. Every time I change files and folders, git is going to track and monitor it.

Java file

4. i created a simple HellowWorld.java file in the vi editor.

$ git status

5. If I type the command $ git status, I get the message no commit yet. Having made some java code in the vi and re-type the command, the terminal will display an untracked file. The file has be

$ vi~/.gitconfig

6. When youre storing something into the git database, git expects you to provide your name and email so that it can uniquely track. In the event of a group of people working on a file, git should know who exactly done the change. Add user details by using the cmmand $ git config –global user.name Jwanderema and verify using $ vi ~/.gitconfig.

$ git add

7. Once you have an untracked file, then youre going to add the file into the staging area. $ git add is the command to add your file from the working directory to the staging area. Type git status to the add and the file is untracked and in red. Type the command $ git add HelloWorld.java and then the git status again. Then you see the file status has turned green to tracked.

$ git commit

8. Once you have decided that this file needs to be tored in the git database, you need to run a command $ git commit -m ‘Created new Repository, added first code file’. The git commit is going to store you r change/ the current information inside the git repository. Now it shows that there have been a total of 6 insertions to the file. The git status command compares each file available I your working directory to your local repository. Now the file has been pushed into the local git repository.


9. Now let me modify the file. I simply added a comment line in the code. You can see it the cat display. When I now run the command $git status, I get the message that this file as been modified. It noticed the difference between the file on the local repository and the working directory. Once I add it, the file now becomes green, registered as staged and ready to be committed.

$ git log

10. The $git log command will show the changes which has been made to the file. It shows who made the change, name, email, time stamp, which files were changed, commit message and is tagged to a unique ID.


11. README.md file created in the vi editor.

untracked file

12. Now I added the a new Readme file and modified the HelloWorld file by adding a print line, this is reflected as untracked filed in the git status. In order to the commit both files I run the $ git add . Command. And the status read 2 files have been changed with a total of 7 insertions.

$ git log --stat

13. Now when I run the $ git log –stat command, it shows me all the changes made to each individual file. Each commit uniquely identified with a hash value. Once these files are committed, there is no other way you can do any modifications unless you add a new commit.

$ git diff

14. How to work in the staging area. I changed the file file again and added a new print line and added it to the staging area. Now I want to compare the file in the staging area with the file in the git repository. I do this by running the command $ git diff –staged. This is always going to compare the staged version with the lasted version pushed into the repository. As you can see the addition in green is that there is Print line with no comment slashes in front.

$ git reset HEAD

15. I wanted this line to be a commented. There is a way to remove the file from the staging area and put it back into to the working directory. The way is to run command $ git reset HEAD HelloWorld.java. Now you see the message Unstaged changes after reset. Run git status and you see the file in red (in the working directory). Having edited the file to add the 2 slashes in front of the comment. This is now reflected when running the diff command.

re-push code after reset

16. This is now ready to run commands $ git add and $ git commit -m ‘Added a comment line to my new print statement’. To be staged and then pushed to the local repository.

Commit history

17. Now when I run $ git log I see I have 4 versions of commits. What if I wanted to see know changes in between commits. For example. Between commit 2 and 3 or commit 1 and 4? When you run the diff command, git compares the staging area with the latest commit in the repository, known as the HEAD as per sketch. If you wanted to compare with the commit just before the latest commit, you will run command $ git diff HEAD~1, if the next one then $ git diff HEAD~1 and so on.


18. $ git diff HEAD~1 will print all the changes behind HEAD – 1. If you read the git log, the comment message reflects the comment display at the bottom under git diff.


19. Like wise on this screenshot, on the top is the git diff HEAD~2 command run, which displays the REAME file being created. Read the message under the second commit, it says a README was added. This way you can compare the code between relative commit versions.

Close Menu