Git Commands Recap : init. clone, status, log (--oneline, --stat, --patch) , show

Git Init : git init

Create/Initialise git repository : Command  <git init>:
"init"command is short for "initialise", it's the command that will do all of the initial setup of a repository. Steps to follow for initial setup: 
  1. create a directory called "devinline-git"
  2. Under this, create another directory called "devinline-java-project"
Execute following command to create directory and go inside it. 
➜  ~ mkdir -p devinline-git-recap/java-git-project && cd $_
➜  java-git-project pwd
/Users/n0r0082/devinline-git-recap/java-git-project

Running the git init command sets up all of the necessary files and directories that Git will use to keep track of everything.
➜  java-git-project git init
Initialized empty Git repository in /Users/n0r0082/devinline-git-recap/java-git-project/.git/

All of these files are stored in a directory called .git (a hidden directory in Unix system). This .git directory is the "repo" and It holds all of the configuration files and directories and is where all of the commits are stored.

Git Clone : git clone <path-to-repository-to-clone>

If we have some existing repo, we can clone it. The git clone command is used to create an identical copy of an existing repository. It is useful when we wan to start project with initial files(index.html, css, js or database jdbc files, etc). We have repository in place and we can clone it using "git clone" command.

➜  java-git-project cd ..
➜  devinline-git-recap git clone https://github.com/zytham/JSONPlayWithJackson
Cloning into 'JSONPlayWithJackson'...
remote: Counting objects: 31, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 31 (delta 6), reused 30 (delta 5), pack-reused 0
Unpacking objects: 100% (31/31), done.
➜  devinline-git-recap ls -ltr
total 0
drwxr-xr-x  2 n0r0082  74715970   64 Jun 19 20:32 java-git-project
drwxr-xr-x  8 n0r0082  74715970  256 Jun 19 20:52 JSONPlayWithJackson
  • git clone command expects git repository name and by default will create a directory with the same name as the repository that's being cloned.
  • Default behaviour of name can be overwritten, providing second argument to "git clone" project. Below command creates cloned project with name provided in git clone command. 
➜  devinline-git-recap git clone https://github.com/zytham/JSONPlayWithJackson custom_cloned_repo
Cloning into 'custom_cloned_repo'...
remote: Counting objects: 31, done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 31 (delta 6), reused 30 (delta 5), pack-reused 0
Unpacking objects: 100% (31/31), done.
➜  devinline-git-recap ls -ltr                                                                   
total 0
drwxr-xr-x  2 n0r0082  74715970   64 Jun 19 20:32 java-git-project
drwxr-xr-x  8 n0r0082  74715970  256 Jun 19 20:52 JSONPlayWithJackson
drwxr-xr-x  8 n0r0082  74715970  256 Jun 19 21:40 custom_cloned_repo
Reference : Git Clone

Git Status : git status

The "git status" command will display the current status of the repository.
➜  custom_cloned_repo git:(master) git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working tree clean

This command will-
  • tell us about new files that have been created in the Working Directory that Git hasn't started tracking, yet
  • files that Git is tracking that have been modified

Git Log : git log (--oneline, --stat, --patch/--p) 

The git log command is used to display all of the commits of a repository.
➜   git log

By default, "git log" command displays following values of every commit in the repository.
  • the SHA
  • the author
  • the date
  • and the message
Git uses the command line pager, Less, to page through all of the information. The important keys for Less are:
  • to scroll down by a line, use j or ↓
  • to scroll up by a line, use k or ↑
  • to scroll down by a page, use the spacebar or the Page Down button
  • to scroll up by a page, use b or the Page Up button
  • to quit, use q

git log --oneline 

The --oneline flag is used to alter how git log displays information.
➜   git log --oneline

This command:
  • lists one commit per line
  • shows the first 7 characters of the commit's SHA
  • shows the commit's message

git log --stat 

The --stat flag is used to displays information in descriptive way with following details.
  • displays the file(s) that have been modified
  • displays the number of lines that have been added/removed
  • displays a summary line with the total number of modified files and lines that have been added/removed
➜   git log --stat

git log --patch / git log --p 

The git log command has a flag (--patch/-p) that can be used to display the actual changes made to a file.
➜   git log --patch

Git Show : git show <Hash_of_commit>

To shows a specific commit. Output of above command is similar to "git log -patch"(no scroll as . only one hash details).
➜   git show 59142f8d2dc39584f6b1998a747d1c5e4bfb554d

By default, git show displays:

  •  the commit 
  • the author 
  • the date 
  • the commit message 
  • the patch information
However, git show can be combined with other flags like:
  •  --stat : to show the how many files were changed and the number of lines that were added/removed
  • -p or --patch : this the default, but if --stat is used, the patch won't display, so pass -p to add it again
  • -w : to ignore changes to whitespace

2 Comments

Previous Post Next Post