Most of the time in GIT we are working at the file level. It is possible to get more granualar and work with hunks which are parts of a file e.g. a number of lines.

Using Hunks

The --patch option of the git add command causes GIT to automatically split an updated file into hunks. It then prompts for each hunk and the contributor can decide whether to stage some or all of the hunks.
GIT actually enters a menu system that allows granular control, such as splitting the hunk into smaller units. After adding some hunks but not all to the index, git status shows the same file is ready to be committed and also not yet staged for commit.

There are many commands that can work at the hunk level, including:

  • git checkout
  • git stash
  • git reset
>git add --patch file.txt # This command 
(1/1) Stage this hunk [y,n,q,a,d,s,e,?] ?
>
y - stage this hunk
n - do not stage this hunk
q - quit
a - stage this hunk
d - do not stage this hunk or an of the later hunks in the file
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
>
> git status # After adding some hunks but not all, the status shows the same file is ready to be committed and also not yet staged for commit
Changes to be committed:
   modified:  file.txt

Changes not staged for commit:
   modified:   file.txt

Many ways to reference a commit

The git show command provides information about a commit. The most common way to reference a commit is using its hash (or partial hash), but there are also other ways:

> git show 8d4112 #  use the partial hash to refer to the commit
commit 8d411239358d55f45747c401c5c2c3fba8652d71
Author: gbdixg <gbdixg@domain.home>
Date:   Mon Jun 7 17:44:03 +100
...
> 
> git show HEAD # show information about the latest commit
>
> git show HEAD^ # show the parent commit of HEAD
>
> git show HEAD^^ # show the second parent of HEAD
>
> git show HEAD~2 # show the 2nd commit before HEAD (same as above)
>
> git show HEAD@{"1 week ago"} # show head 1 week ago



This article was originally posted on Write-Verbose.com