Git

Table of Contents

Online Resources


Notes

+---------+  +----------+ +--------+ +------+
|untracked|  |unmodified| |modified| |staged|
+----+----+  +----------+ +----+---+ +---+--+
     |             |edit       |         |
     |add          +----------->         |
     +------------->           |stage    |
     |             |           +--------->
     |       remove|           |         |
     <-------------+           |         |
     |             |           |   commit|
     |             <---------------------+
     |             |           |         |
  • initializing repo: $ git init
  • cloning existing repo: $ git clone <repo url>
files may be tracked -> in last snapshot
                        {
                            * unmodified
                            * modified
                            * staged
                        }
             untracked -> everything else
  • tracking new files: $ git add <filename>
  • staging modified files: $ git add <filename>
  • viewing changes: $ git status
  • compare working directory and staging area: $ git diff
  • compare staging area and last commit:
    • $ git diff --staged
    • $ git diff --cached
  • committing changes:
    • $ git commit
    • $ git commit -m "<commit message>"
  • skipping the staging area: $ git commit -a
  • removing files: $ git rm <filename>
  • remove file and keeo copy in working directory: $ git rm --cached <filename>
  • moving files: $ git mv <src> <dest>
  • viewing history: $ git log
    • show diffs: $ git log -p
    • limit output to 2 entries: $ git log -2
    • stats for each commit: $ git log --stat $ git log --pretty={short,full,fuller,oneline} $ git log --graph
    • format fields:
%H  commit hash
%h  abbreviated commit hash
%T  tree hash
%t  abbreviated tree hash
%P  parent hashes
%p  abbreviated parent hashes
%s  subject
%an author name
%ae author email
%ad author date
%ar author date, relative
%cn committer name
%ce committer email
%cd committer date
%cr committer date, relative

Limiting log output:

-pshow patch introduced with each commit
--statshow statistics for files modified in each commit
--shortstatdisplay only the changed/insertions/deletions in each commit
--name-onlyshow list of files modified after commit information
--name-statusshow list of files affected with A/M/D info
--abbrev-commitshow only first few characters of SHA1
--relative-datedisplay date in relative format instead of full date
--graphdisplay ascii graph of the branch/merge history beside the log output
--prettyshow commits in alternative format
-<n>show last commits
--sincelimit commits to those made after a date
--afterlimit commits to those made after a date
--untillimit commits to those made before a date
--beforelimit commits to those made before a date
--authorshow commits in which the author entry matches
--committer
--grep
-- <path>