Repository Info

Ref: git tutorial
Git User Manual

Status

Refs: git-status(1)

Status of the repo: files modified from index, index that differ from the HEAD:

git status

Don’t show untracked files:

git status -u no

Status of the current directory:

git status -- .

Branches

Refs: git-branch(1),

Local branches:

$ git branch

Remotes branches:

$ git branch -r
$ git branch -a # both local and remotes

Show also what are the tracking branches, if any.:

$ git branch -vv

Tags

Refs: git-tag(1),

To know your local tags:

$ gi tag
v1
v2

If you want to know the annotation that may come with the tag you have to use the option --list <pattern> abr. -l<pattern> wich list the tages that match a shell pattern, and n<num> that allow n lines of annotation.

To get all the tags with annotations:

$ git tag -n100 -l \*

See also Sharing Tags

Describe

Refs: git-describe

git-describe show the the most recent tag that is reachable from a commit. By default it uses only annotated tags, but you can use any tag with --tags option.

Exemple:

$ git tag
v0.2
v0.90
$ git describe
v0.90-3-g8a8e4de

You are 3 commits after the version v0.90 at the commit 8a8e4de. The prefix g is added to indicate a git managed version.

Logs

Refs:

git-log(1), git-show(1), git-diff(1), gitrevisions(7).

git tutorial: Exploring history.

Git User Manual: Browsing revisions, Understanding Commits,

Review changes in the whole repository.

$ git log --name-status
$ git log --summary
$ git log --stat
$ git log --patch # abbrev -p

Changes on some file/directory

$ git log  --stat   -- Muttrc
$ gitk -- Muttrc
$ gitk --all -- Muttrc

All the commits which add or remove any file data matching the string 'foo()':

$ git log -S'foo()'

To follow among renames, even crossing directories, use for a single file:

$ git log -p --follow Muttrc

Changes in a commit range:

$ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
$ git log master..test      # ...in branch test, not in branch master
$ git log test..master      # ...in branch master, but not in test
$ git log test...master     # ...in one branch, not in both
$ git log --since="2 weeks ago"

Changes introduced by the last commit:

$ git log -1 --stat
$ git log -1 -p

Changes introduced by some commit: You need only the initial part of the commit sha.

$ git log -1 --stat 20b0f6e1961d5da
$ git log -1 --stat -p  20b0f6e1961d5da
$ git show 20b0f6e1961d5da
$ git show HEAD
$ git show devel # the tip of the "devel" branch or tag
$ git show HEAD^  # to see the parent of HEAD
$ git show HEAD~4 # 4 commits before HEAD

If the commit is a merge commit git show <commit> give only the difference between <commit> and its first parent. To get both:

$ git show <commit>^1
$ git show <commit>^2

You can also use git-diff but by suffixing the commit with ^! to mean the commit and nothing in the ancestors (see gitrevisions)

$ git diff 20b0f6e1961d5da^!
$ git diff HEAD^!

Reflogs

Refs:
git-reflog(1), git-log(1), git-show(1), user-manual: recovering lost changes, git-notes: reflog

The reflog records each position of HEAD in the last 30 days (or configuration gc.reflogExpireUnreachable). The reflog history is local to your repository not shared, or cloned.

To show the reflog use:

$ git reflog show --date=relative
$ git log --walk-reflogs
$ git show-branch --reflog

--walk-reflogs and --reflog are abridged in -g. If the rebase and amend don’t appear in a simple log without -g, when you use the reflog you can see and recover commits that have been amended or let away by a rebase.

You can attain any past commit not yet pruned by:

$ git log master@{1}
$ git show HEAD@{"1 week ago"}

git diff

git diff show differences introduced by commits

Refs:
git-diff(1), git-difftool(1), gitrevisions(7), git-format-patch(1).

Diff and index:

# Changes in the working tree not yet staged for the next commit.
$ git diff
# Changes between the index and your last commit;
# what you would be committing if you run "git commit" without "-a" option.
$ git diff --cached
# Changes in the working tree since your last commit;
#what you would be committing if you run "git commit -a"
$ git diff HEAD

diffs between two branches:

$ git diff master..test

You can also use a difftool, if you want to see the diff with meld:

$ git difftool --tool=meld  master..test

To know the list of available tools:

$ git difftool --tool-help

To define a new tool you set in your .gitconfig:

[difftool "ediff"]
    cmd = emacs --eval \"(ediff-files \\\"$LOCAL\\\" \\\"$REMOTE\\\")\"

You use a triple dot to get the diff between the common ancestor of master and test and the tip of test. Warning: The semantic of the triple dot is different with git log:

$ git diff master...test

Patch to apply to master to obtain test:

$ git format-patch master..test

Commit tree

Refs: gitk(1), tig-manual, git-log(1),

View source commit tree, you can use many GUIs, gitk is provided with git, and tig is a ncurses front-end. .

$ gitk --all
$ tig --all
$ gitg

You can also use git-log, with the option --graph:

$ git log --graph --pretty=oneline --abbrev-commit --decorate --all --color

Looking file content in the tree

Refs: git-grep(1)

$ git grep "foo()"                   # search working directory for "foo()"
$ git grep 'defun.*init *(.*)'       # search working directory for pattern
$ git grep -E 'defun.*init *\(.*\)'  # use extended regexp (default basic)
$ git grep "foo()" v2.6.15           # search old tree for "foo()"
$ git grep init 6874caeedb3c -- *.el # search "init" in  .el files at some commit

Viewing other versions of a file

Refs: git-show(1),

You can use a tag, a branch, or a commit sha.

$ git show devel:src/prog.py
$ git show v2.5:src/prog.py
$ git show e05db0fd4f3:src/prog.py

Show the blog sha associated with a file in the index:

$ git ls-files --stage <path>

If you use plumbing commands, you can also show the blog sha that you obtain with git ls-tree or given by a git show on a tree object. See the details in the git manual: Commit Object.

Finding the top level directory

Ref: git-rev-parse(1)

To show the absolute path of the top-level directory.:

$git rev-parse --show-toplevel

To show the relative path of the top-level repository:

$git rev-parse --show-cdup

or to show the path of the current directory relative to the top-level:

$git rev-parse --show-prefix

I use it to have a default message showing paths relative to top-level with:

$git commit :/$(git rev-parse --show-prefix)<relative-name>

To show the git directory:

$git rev-parse --git-dir

If $GIT_DIR is defined it is returned otherwise when we are in Git directory return the .git directory, if not exit with nonzero status after printing an error message.

To know if you are in a work-tree:

$git rev-parse --is-inside-work-tree

Note also that an alias expansion prefixed with an exclamation point will be executed from the top-level directory of a repository i.e. from git rev-parse --show-toplevel.