top of page
Writer's pictureSiddhesh Kadam

Git Init, Staging, Commit

To begin with Git configuration, we must first understand the local git cycle. We need to understand three major layers or phases in depth.



Working Directory refers to the directory or location in which the developer makes modifications to the codebase. This is a developer's playground where all modification/alteration/deletion/addition action occurs in the codebase. The working directory is where Git does not handle files. These files are also known as "untracked files." Staging Area refers to the layer where files that will be part of the next commit, letting git know what changes in the file will occur for the next commit.


A Git repository records and maintains the history of all modifications made to a project's files.


Let's try to understand this practically.

  • Git Repository

To Create an empty Git repository let's create a project folder and initialize Git using init.

[root@siddhesh ~]# mkdir MyProject1
[root@siddhesh ~]# cd MyProject1/
[root@siddhesh MyProject1]# git init 
Initialized empty Git repository in /root/MyProject1/.git/
[root@siddhesh MyProject1]# ls -la 
total 4
drwxr-xr-x.  3 root root   18 Feb  2 12:56 .
dr-xr-x---. 14 root root 4096 Feb  2 12:55 ..
drwxr-xr-x.  7 root root  119 Feb  2 12:56 .git
[root@siddhesh MyProject1]#

Here, We created an empty project folder called "MyProject1" and used the "git init" command to get Git started.

This resulted in the creation of a.git (hidden) folder under our project directory.

.git is a Git repository that stores all of our file modifications/changes/tracking.


  • Create project files and include them in staging.

We'll now add three sample scripts of python under our project directory.

[root@siddhesh MyProject1]# echo "import os" > myscript.py
[root@siddhesh MyProject1]# echo "import sys" > myscript2.py
[root@siddhesh MyProject1]# echo "import re" > myscript3.py

Find out the working tree status using "git status"

[root@siddhesh MyProject1]# git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#	myscript.py
#	myscript2.py
#	myscript3.py
nothing added to commit but untracked files present (use "git add" to track)
[root@siddhesh MyProject1]#

"git status" shows the current state of the working directory and staging area. It shows you which changes have been staged and which have not, as well as which files are not being tracked by Git. In the output above you can see that script created by us is in an Untracked state which means Git is not aware of these files as it got created newly and there is no record present of this in staging. To include these files in staging lets run "git add <filename> <filename1> <filename2>" or "git add ."

[root@siddhesh MyProject1]# git add myscript.py  myscript2.py myscript3.py 

OR

[root@siddhesh MyProject1]# git add .

Run the "git status" command again to check the status of git.

[root@siddhesh MyProject1]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   myscript.py
#	new file:   myscript2.py
#	new file:   myscript3.py
#
[root@siddhesh MyProject1]# 

You can now see that the status of newly added files has changed from Untracked to a new file. New changes have been pushed to the staging area after "git add" was executed, and the repository can now be committed using "git commit"

[root@siddhesh MyProject1]# git commit -m "Newly Added Script Of Python"
[master (root-commit) 589f5a7] Newly Added Script Of Python
 3 files changed, 3 insertions(+)
 create mode 100644 myscript.py
 create mode 100644 myscript2.py
 create mode 100644 myscript3.py
[root@siddhesh MyProject1]# 

We used the "git commit" command to commit repository changes. The -m flag in "git commit" should be used to include a comment. This is necessary to easily track the committed changes.

In the output above we can see that total of 3 files changed and a total of 3 lines got newly added.

  • View Git Commit Changes

You can view the commit changes using "git log"

[root@siddhesh MyProject1]# git log
commit 589f5a7049d6c9c4ed446ee56b974d8e05c6e76d
Author: Siddhesh Kadam <siddhesh@builddevops.com>
Date:   Thu Feb 2 13:31:26 2023 +0000


    Newly Added Script Of Python
[root@siddhesh MyProject1]# 

This gives details of the commit ID, Author, Date, and Comment which was provided during the commit. To get more details of this commit use "git show <commit id>"

[root@siddhesh MyProject1]# git show 589f5a7049d6c9c4ed446ee56b974d8e05c6e76d 
commit 589f5a7049d6c9c4ed446ee56b974d8e05c6e76d
Author: Siddhesh Kadam <siddhesh@builddevops.com>
Date:   Thu Feb 2 13:31:26 2023 +0000


    Newly Added Script Of Python


diff --git a/myscript.py b/myscript.py
new file mode 100644
index 0000000..21b405d
--- /dev/null
+++ b/myscript.py
@@ -0,0 +1 @@
+import os
diff --git a/myscript2.py b/myscript2.py
new file mode 100644
index 0000000..de10111
--- /dev/null
+++ b/myscript2.py
@@ -0,0 +1 @@
+import sys
diff --git a/myscript3.py b/myscript3.py
new file mode 100644
index 0000000..b199df5
--- /dev/null
+++ b/myscript3.py
@@ -0,0 +1 @@
+import re
[root@siddhesh MyProject1]# 

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page