How to track commits in Git – git log command

HOME

The previous tutorial explains the process of staging and committing a change. But, to track all the commits that have occurred in the project, there is a command called git log.

What is git log command?

The git log command shows the snapshot of all the committed changes.  It is used for listing and filtering the project history, and searching for particular changes.

To explain the usage of the git log, let us follow a few steps.

Step 1 – Create an empty directory – GitTest.

Step 2 – Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type a git command as shown below. This command is used for the initial setup of a Git local Repository. To know how to create an empty directory, refer to this tutorial.

git init

Step 4 – Use the below command to display the state of the working directory and the staging area.

git status

Step 5 – Create 2 new files with some content – ReadMe.txt and Index.txt.

Step 6 – Use the below command to promote pending changes (new file or altered file) in the working directory, to the git Staging area.

git add ReadMe.txt

Step 7 – Use the below command to promote the changes from Staging Area to GIT local repository. This is our first commit.

git commit ReadMe.txt

Step 8 – Update ReadMe.txt file

Step 9 – Use the below command to promote both ReadMe.txt and Index.txt to the staging area, which means both files are staged.

git add .

Step 10 – Use the below command to commit both the files to GIT local Repository.

git commit -am "Second Commit with Index and ReadMe files"

Step 11 – Type the below command to see the snapshot of all the commits.

git log

By default, formatting, shows the whole commit history. 

There are various configurations of git log:-

1. git log –patch or git log -p

This command shows the patch for each commit as well as their full differences.

2. git log –stat

This command displays the number of insertions and deletions to each file altered by each commit (note that modifying a line is represented as 1 insertion and 1 deletion). This is useful when you want a brief summary of the changes introduced by each commit. 

3. git log –pretty

git log --pretty=format:"%cn committed %h on %cd"

This command display each commit using printf-style placeholders. The %cn%h and %cd characters in the command are replaced with the committer name, abbreviated commit hash, and the committer date, respectively.

4. git log -n where n is the limit

git log -1

This command limits the number of commits to be displayed.

5. git log –author

git log --author ="Singh"

This command displays all commits for the specified author. If there is no match for the author, nothing will be displayed.

6. git log — fileName

git log -- Index.txt

This command displays those commits that include the specified file, which makes it easier to see the file’s history.

7. git log –after

git log --after="yesterday"
git log --after="2022-02-15"

This command displays a commit from a specific time frame. We can use both –after or –before.

Congratulations!! We have learned about the git log, which is very useful for tracking the changes. Happy Learning!!

How to commit changes in GIT – git commit command

HOME

The previous tutorial explains the staging of the changes. This tutorial explains how to commit changes in GIT.

As we remember from previous tutorials, committing a change (like new file, updated file, etc) is a 2 step process.

“git add” is the first command in a chain of operations that directs Git to “save” a snapshot of the current project state, into the commit history or can say . Now we can say that “git commit” is the second command in above chain of operations.

git commit is used to save the changes to the local Git Repository. When a file is changes and we use git commit, GIT will not automatically commit the changes. Instead, we need to use the “git add” command to mark the desired changes for inclusion.

Let me explain this with an example.

Step 1– Create an empty directory – GitTest.

Step 2Right-click and click on Git Bash Here to open GitBash at that place.

Step 3 – Type a git command as “git init”. This command is used for initial setup of a Git local Repository. To know how to create an empty directory, refer this tutorial.

Step 4 – Type git status command. This command displays the state of the working directory and the staging area.

Step 5 – Create 2 new files with some content – ReadMe.txt and Index.txt.

Step 6 – Type git add ReadMe.txt command. This will  promote pending changes (new file or altered file) in the working directory , to the git Staging area.

Step 7 – Type git status command.

This shows that ReadMe.txt file is already staged and Indesx.txt is not staged. As ReadMe.txt is already staged, we can commit this file to Git local Repository. This can be done by using the command “git commit”.

Step 8 – Type git commit “ReadMe.txt”.

git commit <fileName>

This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file and close the editor to create the actual commit. Below is the editor opens. I have added the message – First commit at line 15 (which is not commented – #)

This is how the message is displayed in GitBash now.

Step 9 – Type git status again. This command shows that now we have only Index.txt to stage and commit.

A shortcut command that immediately creates a commit with a passed commit message without opening of text editor.

git commit -m "commit message"

There are some other options to available with git commit.

  1. This command commit a snapshot of all changes present in Git Staging area.
git commit -a

2. A power user shortcut command that combines the -a and -m options. This combination immediately creates a commit of all the staged changes and takes an inline commit message.

git commit -am "commit message"

In the below example,

Step 1 – Modified Index.txt file

Step 2 – Type git status to see that ReadMe.txt is modified so needs to be staged again and Index.txt which also needs to be staged.

Step 3 – Type git add . to stage both files Index.txt and ReadMe.txt to Staging Area.

Step 4 – Type git status to check the status of both files.

Step 5 – Type git commit -am “Second commit with Index and ReadMe files” to commit both the files.

Step 6 – Type git status to check if both the files are committed or not.

Hopefully, this tutorial will help you to understand the process of committing the changes in GIT. Happy Learning.

How to unstage the changes in Git – git rm command

HOME

The previous example has explained the staging of new files to the staging area.

What is git rm?

This command removes the file from the staging area. The files from the working directory will remain intact. This means that you’ll still have a copy of the file locally. The file will be removed from the index tracking of Git project.

Let us explain this with the help of an example.

  1. Create an empty Git RepositoryGitTest
  2. Create a new file – ReadMe.txt and add the file to staging area.
  3. Create another file – Index.html
  4. Update the first file – ReadMe.txt which is already staged.
  5. Use “git add .” to stage both the files.

Later, we realized that we don’t want to commit both the files. We try to commit the logically same files, like files created or changed to fix a particular bug. So, if in the future we want to track back the changes, it is easy to figure out which all files are changed or impacted.

We want to unstage the “Index.txt” file.

Use the below command to reset the changes from the staging area to the working directory.

git rm --cached Index.txt

The “cached” option specifies that the removal should happen only on the staging index.

I have used “git status” command and it shows that “ReadMe.txt” can either be unstage or commit. Whereas “Index.txt” is unstage. So, if we want to commit Index.txt, first we need to stage this file using git add command.

This image shows that git rm moves the staged file from Staging Area to back to working Directory.

I hope this tutorial has helped you to understand git rm command.

How to stage changes in Git – git add Command

HOME

The previous tutorial explains the creation of a Git Repository. This tutorial explains the steps to stage the changes in the Git Repository.

We have a new Git Repository named as GitTest. Go to the Git Repository and Right-click and select “Git Bash Here”.

To know how to create a new empty Git Repository, refer to this tutorial – How to create a new Git Repository – git init Command.

Type “git status” command on Git Bash and press enter.

This shows that there is no uncommitted file available in the Git Repository.

Scenario 1 – Adding/Staging a new file to Git

Now, let us create a new file called – ReadMe.txt in the Git Repository. Again, type the command – “git status”.

When we have added a new file and use the Git Status command, Git shows that the newly created file is an untracked file, since it is not yet tracked by GIT. It is showing a suggestion to use the “git add” command with the file name so that it can be included as a part of the next commit.

As suggested by Git, let us use the “git add” command to commit the file and then use “git status” command to see the status of Git.

When we ran the “git add” command, then changes from the working directory were copied (not moved) to the staging area. 

The primary function of the git add command is to promote pending changes (new file or altered file) in the working directory, to the git staging area. The staging area is one of Git’s more unique features.

Scenario 2 – Adding a new file to git and updating the already staged file

I have created another new file – “Index.txt” and updated the already staged file – “ReadMe.txt”. Type the command “git status” and see the message provided by Git.

This image provides 4 suggestions

  1. If we want to unstage the old file – ReadMe.txt, we can do that by using command “git rm”.
  2. The latest changes are not staged for ReadMe.txt file. This can be done by using “git add” command.
  3. If we want to move the latest changes from staging area to working directory, use “git restore” command.
  4. The new file Index.txt is not at all staged. So, it can be staged by using the command “git add“.

There are multiple ways to stage multiple files in Git.

The first way is to add the name of all the files, as shown below.

 git add ReadMe.txt Index.txt

The second way is to use the below command. This will stage all the unstaged files available in Git.

git add .

Staging Files – This diagram helps you to remember the difference between Staging Area and Git Repository.

I hope this has helped you to understand the difference between Staging Area and Git Repository.

How to create a new Git Repository – git init Command

HOME

I have explained the steps to install Git on Windows 10 in the last tutorial – How to install Git on Windows 10. This tutorial will explain the process to create a new Git Repository.

Git offers a better way of keeping track of changes by using the Git repository. A repository is a place that stores something like files, data, code, etc.

What is Git Repository?

A Git Repository stores file and keep track of the changes. A Git repository is a virtual storage of a project. It allows you to save versions of your code, which you can access when it is needed. That’s why all software project uses a version control system like Git. Git automatically will not start tracking changes to any file. We need to let git know where to track and what to track.

Let us understand how to use git init command.

Step 1 – Create an empty directory – GitTest.

Step 2 – Right-click and click on Git Bash Here.

Step 3 – Type a git command as “git status”.

As the new directory is not a git directory it showed a fatal messagefatal: not a git repository (or any of the parent directories): .git.

What is git status command?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history.

Step 4 – Type git command – “git init”.

Here, you can see an empty git repository is created under C:/GitTest.

What is git init command?

git init is a one-time command which you use during the initial setup of a new repo. Executing this command will create a new .git subdirectory in your current working directory. This will also create a new main branch – master branch. Master or Main are default branch.

The git init” command creates a new hidden folder named.gitin the same directory where it was run.

Running git init in an existing repository is safe. It will not overwrite things that are already there. The primary reason for rerunning git init is to pick up newly added templates (or to move the repository to another place if –separate-git-dir is given).

Congratulation!!! We have understood the use of git init command.