Git and GitHub: How to work with Git Files? What are the basic Git Commands to know? Working tree, Index, and Commit history explained
Table of contents
- Installing and Initializing Git
- Stages of Git Workflow
- Working with Files in Git
Installing and Initializing Git
To work with git files, it is important to install Git and initialize the Project Folder as a git repository.
1. Git Installation
Once git is installed on your system, upon successful installationgit --version
returns the version that has been installed.
$ git --version
git version 2.37.1.windows.1
2. Initializing Git Repository
To initialize and work with a git repository and git commands, go to the required path, and in your Project folder execute
git init
to initialize as a git repository.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware
$ git init
Initialized empty Git repository in D:/Study/Git/Project/MySoftware/.git/
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
Stages of Git Workflow
1. Woking directory/tree - The working directory contains the files that you are currently working on. The files residing in the working directory are not tracked and are called "Untracked files".
2. Staging Area/index - The staging area contains all the files that you are going to store in your local git repository. git add
is used to move the files to the Staging Area.
Once a file is moved to the staging area, these files are tracked and any changes made to these files are reflected in the git status
, meaning you are preparing these files for the next commit.
3. Local Repository - The local repository acts as a checkpoint for the changes made in the project at any point in time. It contains all the project information, tracking the changes that had occurred in the project till now. git commit
adds the tracking files from Staging Area to the local git repository. The local repository has all the information about the series of commits made and the commit history can be viewed by the git log
command.
Working with Files in Git
1. Adding New Files: from Working Directory to Staging Area
Creating a new file in the working directory
Working Directory contains files that you are currently working on.touch [filename].extension
creates the file in your Working Directory.
The following code creates new files.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ touch hashnode.txt //Creates a new text file
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ touch README.md //Creates a new markdown file
2. Git Add : Moving the Untracked Files to the Staging Area
The newly created files in the working directory are initially untracked, these files are moved to the Staging Area to be staged or tracked further.
git add
adds the files to Staging Area.
3. Git Status : Checking the status of the files
git status
shows the status of the files at any point in time. The states of the files are as follows.
i) Untracked Files
git status
displays the status of newly created files.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
hashnode.txt
nothing added to commit but untracked files present (use "git add" to track)
ii) Staged or Tracked Files
git add .
adds all the files from the Working directory to the Staging Area.
git status
displays the status of staged files.
new file: hashnode.txt
and new file: README.md
signify that new files are added to the staging area. No changes or modifications were made to the file.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git add .
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: README.md
new file: hashnode.txt
iii) Committed Files
git commit -m "[Commit Message]"
saves the tracked files in the local repository.
git status
signifies that all the files are added to the Repository.
nothing to commit, working tree clean
implies that all files are being tracked.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git commit -m -"Added hashnode and readme file"
[master 492a8cf] -Added hashnode and readme file
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md
create mode 100644 hashnode.txt
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git status
On branch master
nothing to commit, working tree clean
4. Staging the Modified Files
Any changes made to the tracking files are now stated as "modified" whenever the status is checked before the next commit. These modified files must be added to the staging area and then commit all the tracking changes. This cycle repeats when you edit the tracking files.
Changes not staged for commit:
- specifies files to be added to the Staging Area.
Changes to be committed:
- specifies files to be added to the local repository and marks it as a checkpoint.
5. Git Commit : Storing Files in Repository
Once you are done with all the changes made in your Staging Area, you are now ready to commit.
Committing the changes means that you are storing the file with all the changes made to date on your local repository.
Note that you can only commit the files that were added to the Staging Area.
git commit
- Stores the file on your local git repository.
Syntax - git commit -m "[Your Commit Message]"
Shortcut: Instead of adding the files to the Staging area and then committing the files,
Adding -a
to git commit lets you stage the already tracked files, skipping git add command.
Hence, you can directly add and commit the files.
Syntax - git commit -a -m "[Your Commit Message]"
6. Git Log: Viewing the history of commits
git log
shows the commits made in your local repository along with the commit ID and the commit message. The most recent logs are displayed first.
- Commit ID - For every commit you execute, a hash string using the SHA-1 algorithm is generated.
- Commit Message - Given by the user in reference to knowing what changes were made.
(HEAD -> master) is a pointer pointing to the last commit executed.
elcot@elcot-PC MINGW32 /d/Study/Git/Project/MySoftware (master)
$ git log
commit e5684962ac48b18ab520f53febb744fd73f54b45 (HEAD -> master)
Author: Your Name <Your Email ID>
Date: Thu Sep 29 14:57:44 2022 +0530
Added new text file
commit 2cb23253c5e6487e030e980835826b303c76e623
Author: Your Name <Your Email ID>
Date: Thu Sep 29 14:22:41 2022 +0530
Added new .vs file and modified text files
commit d433425bc503f40c2d93b0cfd15af01dc2fd79f2
Author: Your Name <Your Email ID>
Date: Thu Sep 29 14:09:20 2022 +0530
Deleted hashnode and readme
git log --oneline
- displays the series of commits executed in a single line.