Getting Started with Git Basics

Getting Started with Git Basics

Getting-Started-with-Git-Basics

Here in this article the very basic commands of git which we most of us will be using on the daily basic. We take the analogy of a Book project and try to understand how we can version control the required files related this project by converting this project into a git repository by initializing and see how we can track the files related to this project using git version control system. We understand about the different stage involved in versioning a file or set of files and how we can push this content to a remote server for making this project open for collaboration.

Test Environment

Fedora 37 workstation
Git v2.40.0

Procedure

Step1: Initializing a git repository from a new project

As a first step let us create a new folder and initialize it so that it converts into the git repository as shown below.

[admin@fedser development]$ mkdir learngit
[admin@fedser development]$ cd learngit/
[admin@fedora learngit]$ git init
Initialized empty Git repository in /home/admin/learngit/.git/

Step2: View tree structure .git repo

Let us try to view the directory structure and the files that get created when we initialize a folder to convert it as a git repository.

[admin@fedora learngit]$ tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── prepare-commit-msg.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── push-to-checkout.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

Step3: Create Project

Here we will try create some set of folders and files to create a book project structure as shown below.

[admin@fedora learngit]$ mkdir chapter1 chapter2 chapter3

[admin@fedora learngit]$ echo "GitHistory" > chapter1/GitHistory.txt 
[admin@fedora learngit]$ echo "InstallingGit" > chapter1/InstallingGit.txt 
[admin@fedora learngit]$ echo "ConfiguringGit" > chapter2/ConfiguringGit.txt 
[admin@fedora learngit]$ echo "InitializingRepo" > chapter2/InitializingRepo.txt 
[admin@fedora learngit]$ echo "CloningRepo" > chapter3/CloningRepo.txt 
[admin@fedora learngit]$ echo "Branching" > chapter3/Branching.txt
[admin@fedora learngit]$ echo "license" > license.txt 
[admin@fedora learngit]$ echo "# learngit" > readme.md

[admin@fedser learngit]$ tree .
.
├── chapter1
│   ├── GitHistory.txt
│   └── InstallingGit.txt
├── chapter2
│   ├── ConfiguringGit.txt
│   └── InitializingRepo.txt
├── chapter3
│   ├── Branching.txt
│   └── CloningRepo.txt
├── license.txt
└── readme.md

Step4: Check the Initial Status of Project

As of now we did nothing to make sure that our project files and folders are version controlled. We can check the current status of the project files as shown below. As you can see from the output all the files are currently under “Untracked files” stage.

[admin@fedser learngit]$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	chapter1/
	chapter2/
	chapter3/
	license.txt
	readme.md

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

Step5: Track files

Let us now try to add the files into the staging area using the “git add” command and check the status of the files. As you can see from the output currently the files are under the section “Changes to be committed”. That means the files are successfully placed into the staging area and are ready to be commited.

[admin@fedser learngit]$ git add .

[admin@fedser learngit]$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   chapter1/GitHistory.txt
	new file:   chapter1/InstallingGit.txt
	new file:   chapter2/ConfiguringGit.txt
	new file:   chapter2/InitializingRepo.txt
	new file:   chapter3/Branching.txt
	new file:   chapter3/CloningRepo.txt
	new file:   license.txt
	new file:   readme.md

Now let us try to commit the changes using “git commit” and verify the status

[admin@fedser learngit]$ git commit -m "Initial Commit"
[master (root-commit) e2d9213] Initial Commit
 8 files changed, 8 insertions(+)
 create mode 100644 chapter1/GitHistory.txt
 create mode 100644 chapter1/InstallingGit.txt
 create mode 100644 chapter2/ConfiguringGit.txt
 create mode 100644 chapter2/InitializingRepo.txt
 create mode 100644 chapter3/Branching.txt
 create mode 100644 chapter3/CloningRepo.txt
 create mode 100644 license.txt
 create mode 100644 readme.md
 
[admin@fedser learngit]$ git status
On branch master
nothing to commit, working tree clean

Until now we have seen how we can stage and commit the changes. Now let us look at the log history to get an idea of what was done until now using “git log”.

[admin@fedser learngit]$ git log
commit e2d92139ab6eb967ef9c35c9d83385ffa2dfc43a (HEAD -> master)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 13:53:43 2023 +0530

    Initial Commit

Step6: Modify files

Here we will try to modify our existing files which are already being tracked and check the status of those files. As you can see from the output the status of the files now change to “modified” and are not staged yet.

[admin@fedser learngit]$ echo "GitHistory2" >> chapter1/GitHistory.txt
[admin@fedser learngit]$ echo "InstallingGit2" >> chapter1/InstallingGit.txt
[admin@fedser learngit]$ echo "ConfiguringGit2" >> chapter2/ConfiguringGit.txt
[admin@fedser learngit]$ echo "InitializingRepo2" >> chapter2/InitializingRepo.txt
[admin@fedser learngit]$ echo "CloningRepo2" >> chapter3/CloningRepo.txt
[admin@fedser learngit]$ echo "Branching2" >> chapter3/Branching.txt

[admin@fedser learngit]$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   chapter1/GitHistory.txt
	modified:   chapter1/InstallingGit.txt
	modified:   chapter2/ConfiguringGit.txt
	modified:   chapter2/InitializingRepo.txt
	modified:   chapter3/Branching.txt
	modified:   chapter3/CloningRepo.txt

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

Now let us again try to stage and commit our changes.

[admin@fedser learngit]$ git add .
[admin@fedser learngit]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   chapter1/GitHistory.txt
	modified:   chapter1/InstallingGit.txt
	modified:   chapter2/ConfiguringGit.txt
	modified:   chapter2/InitializingRepo.txt
	modified:   chapter3/Branching.txt
	modified:   chapter3/CloningRepo.txt

[admin@fedser learngit]$ git commit -m "update files commit 2"
[master 5d430bb] update files commit 2
 6 files changed, 6 insertions(+)

[admin@fedser learngit]$ git status
On branch master
nothing to commit, working tree clean

We can now again check the log status and see two commit done as per our activity.

[admin@fedser learngit]$ git log
commit 5d430bbfa870532547da51fdf8e3cf6370a05067 (HEAD -> master)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 14:00:27 2023 +0530

    update files commit 2

commit e2d92139ab6eb967ef9c35c9d83385ffa2dfc43a
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 13:53:43 2023 +0530

    Initial Commit

Step7: Add a new file and modify existing tracked file

Here in this step we will try to add some new files and modify existing files and check the status of our repository as shown below. As you can see from the output now we have two entries with different state for each of the file. One is a new file and other is a modified file after we have staged those files.

[admin@fedser learngit]$ touch contributing.md
[admin@fedser learngit]$ echo "## For contributing read contributing.md" >> readme.md
[admin@fedser learngit]$ git add readme.md contributing.md 
[admin@fedser learngit]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:   contributing.md
	modified:   readme.md

Let us now commit the changes and verify the working directory status s and log history as shown below.

[admin@fedser learngit]$ git commit -m "adding contributing and updating readme"
[master 2f9fb33] adding contributing and updating readme
 2 files changed, 1 insertion(+)
 create mode 100644 contributing.md

[admin@fedser learngit]$ git status
On branch master
nothing to commit, working tree clean

[admin@fedser learngit]$ git log
commit 2f9fb33c4123b0a8a3e7ffc1a209293f4dd39f79 (HEAD -> master)
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 14:04:05 2023 +0530

    adding contributing and updating readme

commit 5d430bbfa870532547da51fdf8e3cf6370a05067
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 14:00:27 2023 +0530

    update files commit 2

commit e2d92139ab6eb967ef9c35c9d83385ffa2dfc43a
Author: novicejava1 <sudhirbhoga@gmail.com>
Date:   Tue May 2 13:53:43 2023 +0530

    Initial Commit

Step8: Create a .gitignore file

In this step we are going to create a special file “.gitignore” which basically consist of entries of files which we want to ignore. As an example i have added two entries to ignore files with .log and .bin extensions to be ignored from getting committed into the git repository.

[admin@fedser learngit]$ vi .gitignore
[admin@fedser learngit]$ cat .gitignore 
*.log
*.bin
[admin@fedser learngit]$ touch learngit.log
[admin@fedser learngit]$ echo "Initial commit of project done" > learngit.log
[admin@fedser learngit]$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
	.gitignore

nothing added to commit but untracked files present (use "git add" to track)
[admin@fedser learngit]$ git add .gitignore 
[admin@fedser learngit]$ git commit -m "update .gitignore file to ignore log and bin files"
[master f0a11ab] update .gitignore file to ignore log and bin files
 1 file changed, 2 insertions(+)
 create mode 100644 .gitignore

[admin@fedser learngit]$ git status
On branch master
nothing to commit, working tree clean

Now let us try to verify the log history again but this time with a formatting option “–online” and see how our output is rendered precisely in short.

[admin@fedser learngit]$ git log --oneline
f0a11ab (HEAD -> master) update .gitignore file to ignore log and bin files
2f9fb33 adding contributing and updating readme
5d430bb update files commit 2
e2d9213 Initial Commit

Step9: View Staged and Unstaged Changes differences

Here we are going to use the “git diff” tool to see the differences between files which are present in commit snapshot and files in the staging aread. Let us try to update our file in working directory and see how its different from the staging area file. You can see that we have introducted a new line into the “contributing.md” file.

[admin@fedser learngit]$ echo "To contribute to this book subscribe subscribe to our channel #leargit" > contributing.md
[admin@fedser learngit]$ git diff
diff --git a/contributing.md b/contributing.md
index e69de29..a60cda6 100644
--- a/contributing.md
+++ b/contributing.md
@@ -0,0 +1 @@
+To contribute to this book subscribe subscribe to our channel #leargit

Now let us add that modified file into the staging area and again check the difference between working directory file and stage file. This time there should not be any differences as both the files are same.

[admin@fedser learngit]$ git add contributing.md 
[admin@fedser learngit]$ git diff

But now, let us look at the difference between the file in the commit snapshot and the file currently in staging area. This can be done using the below command. Now again you will be able to see the difference in the file in each of this stage.

[admin@fedser learngit]$ git diff --staged
diff --git a/contributing.md b/contributing.md
index e69de29..a60cda6 100644
--- a/contributing.md
+++ b/contributing.md
@@ -0,0 +1 @@
+To contribute to this book subscribe subscribe to our channel #leargit

Let us now finally commit those changes and ensure their is no difference in the file in each of these stages (ie. working directory, staging area and commit snapshot).

[admin@fedser learngit]$ git commit -m "updated contributing file"
[master fa38ac8] updated contributing file
 1 file changed, 1 insertion(+)

[admin@fedser learngit]$ git diff
[admin@fedser learngit]$ git diff --staged
[admin@fedser learngit]$ 
[admin@fedser learngit]$ git log --oneline
fa38ac8 (HEAD -> master) updated contributing file
f0a11ab update .gitignore file to ignore log and bin files
2f9fb33 adding contributing and updating readme
5d430bb update files commit 2
e2d9213 Initial Commit

Step10: Removing changes

We can remove an already commited file from the git repository using the “git rm” command. Ideally we are just adding the changes to the git repository whether it is adding, modifying or deleting the files.

[admin@fedser learngit]$ echo "dummy file to be removed" > remove.txt 
[admin@fedser learngit]$ git add remove.txt 
[admin@fedser learngit]$ git commit -m "dummy file to be removed"
[master acf06e8] dummy file to be removed
 1 file changed, 1 insertion(+)
 create mode 100644 remove.txt

Now let us try to remove this file which we commited just now using “git rm” and check the status.

[admin@fedser learngit]$ git rm remove.txt 
rm 'remove.txt'
[admin@fedser learngit]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	deleted:    remove.txt

Now let us commit our changes and check the log history again.

[admin@fedser learngit]$ git commit -m "removing dummpy file"
[master c423e39] removing dummpy file
 1 file changed, 1 deletion(-)
 delete mode 100644 remove.txt

[admin@fedser learngit]$ git log --oneline
c423e39 (HEAD -> master) removing dummpy file
acf06e8 dummy file to be removed
fa38ac8 updated contributing file
f0a11ab update .gitignore file to ignore log and bin files
2f9fb33 adding contributing and updating readme
5d430bb update files commit 2
e2d9213 Initial Commit

Step11: Unstaging from Staging area

Let’s say we accidentally staged and file into staging area. We can undo our changes by restoring the last commit into our staging area using the “git restore” command as shown below.

[admin@fedser learngit]$ echo "some garbage inserted" > contributing.md 
[admin@fedser learngit]$ git add contributing.md 
[admin@fedser learngit]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   contributing.md
[admin@fedser learngit]$ git restore --staged contributing.md 
[admin@fedser learngit]$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   contributing.md

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

Step12: Restoring Working directory

Here we are going to restore the file in working directory with the file which is in staing area. If there is nothing in the staging ideally its going to restore file from last commit into the working directory as shown below.

[admin@fedser learngit]$ git diff --staged
[admin@fedser learngit]$ git diff
diff --git a/contributing.md b/contributing.md
index a60cda6..b3387ff 100644
--- a/contributing.md
+++ b/contributing.md
@@ -1 +1 @@
-To contribute to this book subscribe subscribe to our channel #leargit
+some garbage inserted
[admin@fedser learngit]$ git restore contributing.md 
[admin@fedser learngit]$ git status
On branch master
nothing to commit, working tree clean

Now you can verify the file has been restored from the last commit.

[admin@fedser learngit]$ git diff
[admin@fedser learngit]$ cat contributing.md 
To contribute to this book subscribe subscribe to our channel #leargit

Step13: Renaming and Moving files

We can rename a file in the repository using the “git mv” command as shown below.

[admin@fedser learngit]$ git mv license.txt license.md
[admin@fedser learngit]$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    license.txt -> license.md

[admin@fedser learngit]$ git commit -m "moviing license file to markdown file type"
[master f9c7919] moviing license file to markdown file type
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename license.txt => license.md (100%)

Step14: Formatting log history

Now that we have seen how we can stage, commit, modify, ustage and restore files. Let us look at how we can format our log output by passing a format string to output additional data about our commit. Here we are going to format our ouput to show commitid, author name and commit time as shonw below.

[admin@fedser learngit]$ git log --oneline --format="%h - %s - %an - %ar" --color
f9c7919 - moviing license file to markdown file type - novicejava1 - 5 minutes ago
c423e39 - removing dummpy file - novicejava1 - 15 minutes ago
acf06e8 - dummy file to be removed - novicejava1 - 16 minutes ago
fa38ac8 - updated contributing file - novicejava1 - 23 minutes ago
f0a11ab - update .gitignore file to ignore log and bin files - novicejava1 - 54 minutes ago
2f9fb33 - adding contributing and updating readme - novicejava1 - 60 minutes ago
5d430bb - update files commit 2 - novicejava1 - 64 minutes ago
e2d9213 - Initial Commit - novicejava1 - 70 minutes ago

Here is the high level diagram of the different stages or lifecycle of a file that it gets into as shown below.

Hope you enjoyed reading this article. Thank you.