Status, Add and Commit
In Git a file can be in one of the following three states.
In Git a file can be in one of the following three states.
- Working Directory
- Staging Index
- Repository
git status
You can view the status of all the files in any of the first two states using git status command.
- git status
- git status -s
- git diff
- git diff --staged or git diff --cached
There is $ git status command to view what is tracked/staged or is in working directory. There is a flag --short or -s too. The image describes how to read the short output.
git add
This command can perform one of the following actions.
Notes
When you make a commit Git checksums each subdirectory and stores those tree objects in Git repository. It stores a commit object that contains
You can specify message inline by using
$git commit -m "Your commit message here"
To add all the tracked files in working directory into staging and commit at the same time, use
$git commit -a
To get details of all the changes automatically and make it part of commit message use,
$git commit -v
Best practices for writing commit message -
This command can perform one of the following actions.
- Start tracking a file
- Add a file to staging area
- Mark a conflicted file resolved
Notes
- Tracked files are the ones which are in previous snapshot.
- If you add a file and then modify it; the modifications will be in working directory only. You have to execute $ git add again to make the changes part of same commit.
When you make a commit Git checksums each subdirectory and stores those tree objects in Git repository. It stores a commit object that contains
- SHA-1 value for the commit
- a pointer to the snapshot of the content you staged
- author’s name and email,
- the message that you typed, and
- pointers to the commit or commits that directly came before this commit (its parent or parents)
- zero parents for the initial commit,
- one parent for a normal commit, and
- multiple parents for a commit that results from a merge of two or more branches
You can specify message inline by using
$git commit -m "Your commit message here"
To add all the tracked files in working directory into staging and commit at the same time, use
$git commit -a
To get details of all the changes automatically and make it part of commit message use,
$git commit -v
Best practices for writing commit message -
- good descriptive commit messages - e.g. not "Fixed typo" but "Corrects spelling of Jhon"
- start with short single-line summary with less than 50 characters
- optionally you can add a blank line and a more complete description
- keep each line in description less than 72 characters
- write commit messages in present tense, not in past tense e.g. "Fix bug" or "Fixes bug" and not "Fixed Bug"
- label the commit as what it does rather than what you did in that commit
- add ticket number from issue tracking system
Removing Files
To remove a file from git you need to perform following two actions.
- Remove it from tracked files
- Delete from working directory
This is what $ git rm <file_name> command does. As this will delete the file from working directory too, you will not find the file anymore.
If you want git to stop tracking a file, but to keep it in working directory -
$ git rm --cached <file-name>
As git does its own file name expansion therefore escape characters are necessary infront of special characters. E.g. $ git rm log/\*.log Note the backslash (\) in front of the *.
Moving Files
If you rename a file directly git will treat it as old named file deleted and new name file created. Both will be untracked. E.g.
$ mv homepage.html new_home.html
$ git status -s
D homepage.html
?? new_home.html
And if you do same using $ git mv it will correctly identify that file is renamed and it will stage the changes too.
$ git mv header.html new_home.html
$ git status -s
R header.html -> new_home.html
Tagging
Git gives you the ability to tag important points in history. E.g when you release first version of the application, you can tag that commit on master branch as v1.0
$ git tag -a v1.0 -m 'my version 1.0'
There are two types tags which you can create.
1. Lightweight
2. Annotated
Lightweight Tags
A lightweight tag is a checksum stored in a file with no other information. It is just a pointer to specific commit which doesn't change.
$ git tag v0.1-lw <optional_sha-1_value>
Now to list that tags,
$ git tag
v0.1-lw
And to see what the tag is pointing to,
$ git show v0.1-lw
commit d821d77b0435a79f249dc8cb0214434c74b4b56d
Author: Bob <bob@techmightsolutions.com>
Date: Sun Dec 27 22:48:14 2015 +0530
Adds header to website
Annotated Tags
Annotated tags are stored as full objects in Git database. It is always recommended to have annotated flag as -
- they are checksummed
- have tagger's name, email, date and message
- can be signed and verified by GNU Privacy Guard (GPG)
$ git tag -a v0.1 -m "This is alpha version 0.1" <optional_sha-1_value>
$ git tag
v0.1
v0.1-lw
$ git show v0.1
tag v0.1
Tagger: Mary <mary@techmightsolutions.com>
Date: Mon Dec 28 07:58:15 2015 +0530
This is alpha version 0.1
commit d821d77b0435a79f249dc8cb0214434c74b4b56d
Author: Bob<bob@techmightsolutions.com>
Date: Sun Dec 27 22:48:14 2015 +0530
Adds header to website
To share your tags,
$ git push origin --tags