how to use git add,git status,git commit and git diff

Sarath Pillai's picture
using git add,git commit,git diff,git status commands

Note:  Before going ahead with this post, we suggest reading the below ones as prerequisites.

Introduction To git Version Control System

Installing and configuring GIT

How to configure a GIT repository

We will start from where we stopped in the previous post. In the previous post we cloned a git repository. And had a look at the command git status.

Lets get inside our project directory where we have cloned the remote repository. Running git status command inside the newly cloned repository will show you the below information.

$ git status

# On branch master

nothing to commit (working directory clean)

 

the result of the git status shows us two things, that you have a very clean repository, which means you just have cloned the repository,and have not modified any files in that directory. Every files in this directory is tracked, but none is untracked, because this is a new repository,that you have cloned.

The status command also tells me which branch am in, it tells us that am in “master” branch. I will explain about branches later.

Now lets add a new file in our repository directory, we will keep the filename, test. After adding the file “test” in the repository, git status command will show us the below things.

$ git status

# On branch master

# Untracked files:

# (use "git add <file>..." to include in what will be committed)

#

# test

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

the status output says that there is an untracked file called “test”, and please run the command “git add test” to start tracking that file.Note that test filename is mentioned in the status result under the Untracked files heading.

Note: Whenever git sees a new file in your repository, it doesn’t start to track that file, unless you ask git to do so.

Now lets start tracking our newly made file “test”, by the “git add” command.

$git add test

now rerunning the git status command will show you:

$ git status

# On branch master

# Changes to be committed:

# (use "git reset HEAD <file>..." to unstage)

#

# new file: test

#

you can see from status output that our new file is included in the next commit. Our new file “test” is now staged(which means will go in the snapshot in next commit), and is tracked.

Now lets understand one thins, that if you pass a directory name to “git add” command it will take all the files in the directory to staging or start tracking them. “Git add” command is a multipurpose command you can start to track files as shown above, you can stage files etc.

Now lets change a file already in the repository, if suppose we have a file named “test1” other than the “test” file,which is already been tracked. After editing “test1” file the git status command output will be something as below.

$ git status

# On branch master

# Changes to be committed:

# (use "git reset HEAD <file>..." to unstage)

#

# new file: test

#

# Changed but not updated:

# (use "git add <file>..." to update what will be committed)

#

# modified: test1

Now we can see that “test1” file appears to be in the section named “changed but not updated”, which conveys that the file is modified and yet not staged. With the help of the git add command we can stage the modified file.

$ git add test1

Now out git status output will show the following.

$ git status

# On branch master

# Changes to be committed:

# (use "git reset HEAD <file>..." to unstage)

#

# new file: test

# modified: test1

This clearly shows that “test” is a new file and “test1” is modified. Now both these test and test1 files are in staging area and will go to the snapshot in the next commit.

The important thing to understand here is that if you once again modify any of the file, then you need to run git add <filename> once again, otherwise the git commit will take the state of the file when we last run git add, not the current one. Which means we need to run git add every time we modify a file(in fact thats not the case, this can be done in another way..will come to that later).

Now lets understand, a special file called .gitignore inside your repository. If you need some of your files, not to be tracked or listed inside the git status command(which means you don’t want some files to be present in any of your commits) then you can include patterns of those files in .gitignore file.

Suppose you want not to track files that ends with .gz extention then you can include them in .gitignore file.

$ vi .gitignore

*.gz

Git status command will only let you know, what are in staging area, it will show you whether there is some file which is untracked etc. But it will not show you the exact difference between previous state of a file and current state, if you modified a file.

Git diff command will show you the exact difference, or say if you added some lines to a file, it will show you those lines which are modified.

A simple “git diff” command will tell you the difference between whats your current state of the files, and the state of the files in staging area.

“git diff --cached” or “git diff –staged” will compare the staged state and last commit state.

Make this difference very clear, that “git diff” will only show you the difference, between the staged and current state. “git diff –staged” will show you the difference between your staged files and last commit.

And if you have staged all your changes/modifications through “git add” command then “git diff” will not show you any output because there is no difference between the staged and current state.

Now lets commit our changes if we have staged all our changes. Commiting can be done by the following way.

$git commit

running the above “git commit” will launch an editor(this editor can be set by the variable in git through the command git config --global core.editor)

in the editor you can type in the message which is relevant to your modification of the repository.

Or you can do all these in one command itself.

$git commit -m “modified test and added test1 file”

Now you have commited, your changes to the repository. The commit will give you some output such as the branch name,checkum or hash of your commit. So in this commit whatever was there in your staging area went to the snaoshot.

You can easily skip the each “git add” we do after modifying the files, to stage them through the following option in the “git commit” command.

$git commit -a -m “your message”

-a option will add all the files which are already tracked to your staging area before commit.

Note: Still if you have made a new file in your repository, you need to start tracking it through “git add” command, and stage it through “git add” for the first time. Then afterwords you can easily stage and commit the modification, through -a option in “git commit” command.

Thank You all..Hope you enjoyed the post.

Rate this article: 
Average: 3.5 (83 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.