Git Commands Recap : Add, rm, commit, diff


Git Add : git add <file_name1>,<file_name2>

If a file need to be committed to repo, it needs to be brought first to staging area(staging index) from working directory. The "git add" command is used to move files from working directory to staging index(Changes need to be committed area). Here we create three files and use git add command to move it in staged which can be verified using git status command.

➜  custom_cloned_repo git:(master) ✗ git add index.jsp
➜  custom_cloned_repo git:(master) ✗ git add .


git add command expects argument(s) - Either list of files names separated by comma or period(.) - all the files in working directory.

How to remove file from staged area(To un-stage) :
Using "git rm --cached <file_name>" - file can be moved from staged index to working area(un-stage). Below rm command un-stage front.css and move to untracked section .
➜  custom_cloned_repo git:(master) ✗ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 new file:   front.css
 new file:   front.js
 new file:   index.jsp

➜  custom_cloned_repo git:(master) ✗ git rm --cached front.css 
rm 'front.css'
➜  custom_cloned_repo git:(master) ✗ git status               
On branch master
Your branch is up-to-date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 new file:   front.js
 new file:   index.jsp

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

 front.css


Git Commit : git commit /git commit -m "<commit_message>"

The git commit command takes files from the Staging Index and saves them in the repository. "git commit" opens configured editor for capturing commit message, if not configured vim will be used as default editor.
Command "git config --list" displays git configuration, Refer this for associating text editor with git.

git commit with -m flag is used to specify comments inline(default editor do not open for capturing commit message).
➜  custom_cloned_repo git:(master) ✗ git commit -m "added js and index jsp"
[master 67d8efc] added js and index jsp
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 front.js
 create mode 100644 index.jsp


Git Diff : git diff

git status tell us what files have been changed, but does not conveys what those changes are.The git diff command is used to find out what those changes are.

The git diff command can be used to see changes that have been made but haven't been committed, yet. It also displays:

  • the files that have been modified
  • the location of the lines that have been added/removed
  • the actual changes that have been made

Note: Outcome of "git log -p" is similar to "git diff".

2 Comments

Previous Post Next Post