Fundamentals of Git and Basic Commands.
Table of contents
This blog is the second part of our Git series, in the first blog we configuring git locally, and in this blog, we will explore the fundamentals of git.
How Git Works?
Unlike other Version Control Systems, Git uses a snapshot method to track changes. Every time you commit in Git, it basically takes a snapshot of those files that have been changed while simply linking unchanged files to a previous snapshot, efficiently storing the history of the files.
Git operations are local, for the most part, meaning it does not need to interact with a remote or central repository.
Life cycle of a file in Git
- Untracked: When you first create a file, Git sees this file but does not perform any operations on it.
- Staged: when you add a file in Git to track.
- Modified: when you made a change in an unmodified file.
- Unmodified: When a file is committed.
When you add new files, they are initially Untracked. We use the git add
command for the new file's status to be changed to Staged.
Staged is the status that git add
, git rm
, and git mv
commands immediately change said files to Staged because there is no Modified status for those operations.
Editing files already existing in the repository will change their status to Modified. Modified files need to be staged using the git add
command to change the status to Staged.
Finally, Unmodified is the status once the Staged files have been committed to the repository.
Working with Git
Working directory / local workspace
Starting with the working area, the working area is where the files that are not handled by git, it is the place where all the untracked files are kept.
In the above screenshot, we have created a file in our git repo in our working area which is untracked.
Staging area / index
A staging area has files that are added by the git add
command, going to be part of the next commit.
In the above screenshot, we have added a file in our git staging area which is tracked but not yet committed.
Local repository
The repository on the computer contains all the project and store all committed items is called a local repository.
We have added our text.txt file in our local repo with the git commit
command.
Remote repository
The remote repository contains your copy of the local repo on a server.
Git Basic Commands
File .gitignore
Git has the option to specify which file or directories to be ignore. To do this, you need to create appropriate templates in the .gitignore
file in the repository. To make git ignore, you can add such a file name to the .gitignore
file.
git init
Initialize a new local git repository inside a directory with git init
or use git init my_project
will create a directory and also initialize a repository.
git add
Command git add
is used to start git tracking files. You can specify that you want to track a particular file or use git add .
to add all untracked files at once.
git status
check the status of a working directory from it's perspective.
git commit
After all necessary files have been added to a staging area, you can commit changes. Staging is a collection of files that will be added to the next commit.
git show
The git show f4689dd
command will help to see the details of a particular commit.
git rm
delete a git-tracked file from a repository.
You can see in the above screenshot that when we use the git rm .gitignore
command, it stages the move immediately. With the move already staged, we just commit the update.
But when we use the only rm text.txt
command, it's not staged immediately, we need to add the untracked file with the git add text.txt
command to the staging area and after that commit the changes.
git mv
Like the git rm
command, the git mv
command automatically stages the update. Because it is already staged, we can then commit the changes to the repository with git commit
. In git repo, this is recommended, that use git mv
and git rm
commands.
git diff
Command git diff
allows you to see the difference between different files in the working area. The git diff
command shows what changes have been made since the last commit. If you add changes made to the staging area via the git add
command and run git diff
again, it will show nothing.
Use the git diff
to see the difference between two commits.
$ git diff 1c16945 9dcff67
To show the difference between staging and the last commit, add the parameter:
$ git diff --staged
Repository History
The git log
command displays a brief description of a repository, it includes the change, date and time, who made the changes, commit message and commit identifier (hash value).
- The
git log
command shows a history of the repository in descending order. - You can tidy this history with the
git log --oneline
command, which shows a clean-cut history.
There are many ways to see commit history:
The most basic one shows all the commits from end to start, with all the info related to commits.
$ git log
commit 15ec4ed0801180c9a168d67e225b15d3343c6afe (HEAD -> main)
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:57:43 2022 +0500
remove text file
commit b48130452072e68c22f738ae83ce2000e0b0e45a
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:54:44 2022 +0500
remove .gitignore
commit b2e3baf8c7d2f8bbb1089af6d1322bb71d5880cc
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:38:49 2022 +0500
Add .gitignore file
commit f4689ddb426fcf398861396d52c69ba847cd86b2
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:19:00 2022 +0500
Add a text file
To see some number of the last five commits:
git log -5
To see a commit from some commit to some other commit, we use the hash value of those commits.
git log af156ff 32509d0
To see the statas of the commits:
git log --stat
commit 15ec4ed0801180c9a168d67e225b15d3343c6afe (HEAD -> main)
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:57:43 2022 +0500
remove text file
text.txt | 0
1 file changed, 0 insertions(+), 0 deletions(-)
commit b48130452072e68c22f738ae83ce2000e0b0e45a
Author: John Woe <johnwoe@example.com>
Date: Sun Aug 28 18:54:44 2022 +0500
remove .gitignore
.gitignore | 0
1 file changed, 0 insertions(+), 0 deletions(-)
Check out and Detached Head
Git checkout changes your working directory to match a specific commit and your git’s head is in a detached head state, the Git repository is reverted and your work is temporarily lost means going back to the past. The commands below are used:
$ git checkout <first 7-bits of commit>
$ git checkout main
Reset and Revert to undo
To replace the last commit, git commit --amend
replaces/updates the last commit, if anything is present in the staging area will also commit and your previous commit has been replaced.
To reset, git rest
removes commits and resets the head. Don’t use git commit --amend and
git reset`, these two commands will create a conflict if you are collaborating with a team in your remote repository.
Thanks for reading, stay tuned.
Subscribe to my newsletter
Read articles from Syed Asif directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Syed Asif
Syed Asif
Associate Engineer