This page is under regular updates. Please check back later for more content.
Git & GitHub
Git Basic Operations

Git Basic Operations

Commit

  • It allow us to store in Git database, different versions of our project or snapshots. And after that we are pretty and fast move to any version of our project by checking out to specific commit.
  • Commit has same structure as blob and tree i.e., it has Content, object type and object length.

image

  • Each commit has following information i.e., Pointer to database, Author name, email, commit description and parent (optional)
  • Each commit has it's own SHA1 hash.
  • Commit is just "wrapper" for the tree object and contains pointer to specific tree

Configuring Author details

  • We can set the author details like name and email either gloablly or per project using git config.
CommandDescription
git config --global user.name <name>Set the author name
git config --global user.email <email>Set the author email (must be same as of GitHub)
git config --listList all the saved Configurations
➜  git-project git:(master) βœ— git config --list
user.email=xanderbilla@gmail.com
user.name=Vikas
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true

First Commit

image

  • In order to commit we must have some changes in our project and the modified files must be in staging area.
  • The first commit in our project is called "root-commit".
  • Every commit is git object and each object has it's own hash value.
  • To check the files present in staging area use git ls-files -s command
➜  git-project git:(master) βœ— git ls-files -s
100644 69bae2c1b12818cb90531fef73ac3543ddb61f84 0       file1.txt
100644 81c08026cd4bb6a571607aa26a86a29baf58fef1 0       file2.txt
  • We have two modified files in our staging area. So we will be able to commit changes.
  • To create a commit we use git commit -m <description>
➜  git-project git:(master) βœ— git commit -m "C1# Added two files"
[master (root-commit) 287483c] C1# Added two files
 2 files changed, 4 insertions(+)
 create mode 100644 file1.txt
 create mode 100644 file2.txt
  • In the above output -
OutputDescription
masterThe commit is on master branch
root-commitThis is the first commit and don't have any parent commit
287483cSHA1 hash of this commit
C1# Added two filesThis is the description of the commit
2 files changed, 4 insertions(+)Information about the changes
create mode 100644 file1.txtInformation about the files changes that is create/modified/deleted. 100644 is the permission and file1.txt is about the file information.

Commit Object

  • All the git commits are saved inside .git/objects folder
➜  git-project git:(master) ls .git/objects
28  69  81  c4  info  pack

If you notice the commit 287483c is stored inside folder 28.

  • To view the commit we can use git cat-file -p <commit-hash> command.
  • To view the object size we can use git cat-file -s <commit-hash> command.
  • To view the object type we can use git cat-file -t <commit-hash> command.
➜  git-project git:(master) git cat-file -p 287483c
tree c4a69d80c3913be02bdeccb770b0a156d5ae98f1
author Vikas <xanderbilla@gmail.com> 1730735371 +0530
committer Vikas <xanderbilla@gmail.com> 1730735371 +0530

C1# Added two files
➜  git-project git:(master) git cat-file -s 287483c
178
➜  git-project git:(master) git cat-file -t 287483c
commit

Adding New file to Working Directory

➜  git-project git:(master) echo "This is third file" >> file3.txt
➜  git-project git:(master) βœ— ls
file1.txt  file2.txt  file3.txt
➜  git-project git:(master) βœ— git ls-files -s
100644 69bae2c1b12818cb90531fef73ac3543ddb61f84 0       file1.txt
100644 81c08026cd4bb6a571607aa26a86a29baf58fef1 0       file2.txt

Here we have added file3.txt in working directory. But the files are not in staging area, it is only present in the working area.

➜  git-project git:(master) βœ— git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file3.txt

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

In the status it says Untracked files that means the file is not in git database and to add the file to track it by using git, it should be added using git add.

There are mainly 4 file tracking status -

image

Git files lifecycle

image

  • If we add any new file to working directory it becomes untracked.
  • It is considered track when it go under different state other than untracked
  • In the life cycle of git it is stated that how the files state changes. It is done by using different commands (shown in figure).
  • When the file is untracked and we need to track (add in staging area) we use git add. - transited to Staged
  • To commit the files available in staging area we use git commit - transited to unmodifed state of the file
  • The above two process keep on repeated for everytime we modify the files.
  • We can also untrack files from git repository/staging area.

Stage File

  • Staging area is an area which is used by git to make sure which files need to be tracked and the changes in existing file present int hat area.
  • We can stage file using git add <file_name>
➜  git-project git:(master) βœ— git add file3.txt
➜  git-project git:(master) βœ— git ls-files -s
100644 69bae2c1b12818cb90531fef73ac3543ddb61f84 0       file1.txt
100644 81c08026cd4bb6a571607aa26a86a29baf58fef1 0       file2.txt
100644 3e0ccc6026ff85ca879665513637963cfc1cffac 0       file3.txt

Unstage File

  • We can untrack the files from staging are or git repository.
  • If we check the project the file3.txt in staging area.
  • To untrack (unstage) the file from staging area use git rm --cached <file_name>
  • the above command doesn't remove file from working directory.
➜  git-project git:(master) βœ— git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file3.txt

➜  git-project git:(master) βœ— git rm --cached file3.txt
rm 'file3.txt'
➜  git-project git:(master) βœ— git ls-files -s
100644 69bae2c1b12818cb90531fef73ac3543ddb61f84 0       file1.txt
100644 81c08026cd4bb6a571607aa26a86a29baf58fef1 0       file2.txt
➜  git-project git:(master) βœ— git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        file3.txt

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

We can also use git restore --staged <file_name> to unstage a file.

Commiting Changes

  • Once we commit the changes of staging area the file state will be transited from modifed > unmodifed
  • To perform commit refer to commit section
➜  git-project git:(master) βœ— git commit -m "C2# Second commit"
[master 323fb59] C2# Second commit
 1 file changed, 1 insertion(+)
 create mode 100644 file3.txt

The above will create the new tree and blob in the git repository.

➜  git-project git:(master) git cat-file -p 323fb
tree 3042f40863731558918af4d961398798711503ee
parent 287483c698151ee255c5722276a656bd8f95f979
author Vikas <xanderbilla@gmail.com> 1730739808 +0530
committer Vikas <xanderbilla@gmail.com> 1730739808 +0530

C2# Second commit
➜  git-project git:(master) git cat-file -p 28748
tree c4a69d80c3913be02bdeccb770b0a156d5ae98f1
author Vikas <xanderbilla@gmail.com> 1730735371 +0530
committer Vikas <xanderbilla@gmail.com> 1730735371 +0530

C1# Added two files

Here if you notice the latest commit has a parent commit and a new tree is created which is pointing to the parent commit.

Basic Git Command

CommandDescription
git config --global user.name <name>Set the author name
git config --global user.email <email>Set the author email (must be same as of GitHub)
git config --listList all the saved Configurations
git ls-files -sCheck the files present in staging area
git commit -m <description>Create a commit with a description
git cat-file -p <commit-hash>View the commit
git cat-file -s <commit-hash>View the object size
git cat-file -t <commit-hash>View the object type
git statusCurrent state of git repository
git add <file>Add files to staging area
git rm --cached <file>Unstage files from staging area
git restore --staged <file>Unstage files from staging area
git commitWrite changes to git repository
git logHistory of changes (commits)
git checkout <commit/branch>Checkout commit or branch

git status

This command display multiple information -

  • Current branch
  • State (untracked/staged/modified) of files
  • File ready to be commited

git add

This command takes files from working directory and place them into staging area.

git commit

  • This command create new commit object.
  • This command also create new blob and tree if needed.

git checkout

It means files are taken from that commit or branch and placed in working directory (It will overrride the files of working directory) Also used to travel between differnt version of projects and branches.

git log

  • It show multiple information such as - commit description, date, and author of the commit, type of object, hash of the object