Skip to content

Git Command Reference

Posted on:February 14, 2023Β atΒ 01:22 PM

Git Command Reference

List of common git commands which I use.


πŸ“œ Log Commands

git log --follow -p -- <file>

Use Case: View the complete history of a file including past renames and diffs. Example:

git log --follow -p -- src/app.py

git whatchanged

Use Case: Similar to git log, but shows changes introduced by each commit. Mostly used for low-level debugging. Example:

git whatchanged

git log --name-only

Use Case: Show commit messages and file names that were changed. Example:

git log --name-only

git log -- <file1> <file2>

Use Case: View all commits that affected specific files. Example:

git log -- foo.py bar.py

git log --stat

Use Case: Show summary of changes in each commit including lines added/deleted. Example:

git log --stat

git log --oneline

Use Case: Compact log view with one line per commit. Useful for quick overviews. Example:

git log --oneline

git log --merges

Use Case: Show only merge commits. Example:

git log --merges

git log --no-merges

Use Case: Show only non-merge commits. Example:

git log --no-merges

git log master..feature

Use Case: See commits in feature branch not present in master. Example:

git log master..feature

git shortlog

Use Case: Summarized log grouped by author. Great for contributor summaries. Example:

git shortlog

πŸ” Diff & Change Tracking

git diff --stat --summary <rev1>..<rev2>

Use Case: View a summary and stats of differences between two commits. Example:

git diff --stat --summary 6eb715d..HEAD

git stash show -p stash@{1}

Use Case: Show the patch/diff for a specific stash. Example:

git stash show -p stash@{1}

git diff --name-status -C <rev1> <rev2>

Use Case: See the status (Added/Modified/Deleted) of files and detect renames between commits. Example:

git diff --name-status -C HEAD~3 HEAD

git diff --stat <rev1> <rev2>

Use Case: Show changes with summary stats between two commits. Example:

git diff --stat abc123 def456

git diff @{upstream}

Use Case: Compare local branch with its upstream (remote tracking branch). Example:

git diff @{upstream}

↻ Checkout & Restore

git checkout <commit_hash> -- <file_path>

Use Case: Restore a specific file from a past commit. Example:

git checkout abc123 -- src/config.yaml

git checkout <remote>/<branch> -- <file_or_dir>

Use Case: Restore a file or directory from a remote branch. Example:

git checkout origin/dev -- docs/

git checkout stash@{n} -- <file_path>

Use Case: Restore a file from a specific stash. Example:

git checkout stash@{2} -- index.html

🌿 Branch Management

git branch --merged <branch>

Use Case: List branches already merged into a given branch. Example:

git branch --merged master

git branch -m [<old-name>] <new-name>

Use Case: Rename a Git branch. Example:

git branch -m feature-x feature-login

πŸ’ Cherry Pick & Merge Insights

git cherry -v master <branch>

Use Case: List commits in a branch that are not yet merged into another. Example:

git cherry -v master feature-login

git cherry-pick <commit_hash>

Use Case: Apply a specific commit from another branch onto your current branch. Example:

git cherry-pick 4a5e6f7

πŸ”Ή Useful when you want to port a fix or feature without merging an entire branch.


πŸ”§ Additional git cherry-pick Options

-n / --no-commit

Use Case: Apply changes but do not commit them immediately. Ideal for batching multiple cherry-picks into one commit.

git cherry-pick -n abc123

--edit

Use Case: Open the commit message in an editor before committing.

git cherry-pick --edit abc123

-x

Use Case: Append a reference to the original commit in the new commit message. Useful for traceability.

git cherry-pick -x abc123

--strategy=<strategy>

Use Case: Specify a merge strategy if conflicts are expected (e.g., recursive, ours, theirs).

git cherry-pick --strategy=recursive abc123

--continue, --abort, --quit

Use Case: Manage the cherry-pick process during conflicts.

git cherry-pick --continue
git cherry-pick --abort
git cherry-pick --quit

Git Config

[core]
	# Set delta as the default pager for git commands that output multiple pages
	# Delta is a modern syntax-highlighting pager for git, providing better diff visualization
	# than the default 'less' pager
	pager = delta

[delta]
	# Use light theme instead of dark theme for delta output
	# Setting to false means delta will use a light background color scheme
	dark = false

	# Display line numbers in the left margin of diff output
	# This makes it easier to reference specific lines when discussing changes
	line-numbers = true

	# Display diffs in a side-by-side format instead of unified format
	# Shows old content on the left and new content on the right
	# This provides a clearer visual comparison of changes
	side-by-side = true

[color]
	# Enable colored output for all git commands that support it
	# This includes status, diff, branch, log, etc.
	# Makes git output more readable by using colors to distinguish different types of information
	ui = true

[column]
	# Automatically format output in columns when the terminal is wide enough
	# Git will decide when to use columnar layout based on terminal width
	# Applies to commands like 'git branch', 'git tag', etc.
	ui = auto

[branch]
	# Sort branches by commit date in descending order (newest first)
	# The minus sign (-) indicates descending order
	# This is useful for seeing recently active branches at the top
	sort = -committerdate

[tag]
	# Sort tags using version number sorting algorithm
	# This intelligently sorts version tags like v1.2, v1.10, v2.0
	# instead of lexicographically (which would put v1.10 before v1.2)
	sort = version:refname

[diff]
	# Use the histogram algorithm for generating diffs
	# This algorithm often produces more intuitive diffs than the default Myers algorithm
	# It's particularly good at handling code that has been moved around
	algorithm = histogram

	# Highlight moved lines in diffs using plain coloring
	# When lines are moved (not just added/deleted), they'll be colored differently
	# to show they were relocated rather than being entirely new/removed content
	colorMoved = plain

	# Use mnemonic prefixes in diff headers
	# Instead of a/file and b/file, uses more descriptive prefixes like:
	# i/ (index), w/ (working tree), c/ (commit), o/ (object), etc.
	mnemonicPrefix = true

	# Detect and show file renames in diffs
	# When a file is moved/renamed, git will show it as a rename operation
	# rather than showing it as a deleted file and a new file
	renames = true

[merge]
	# Use diff3 conflict style for merge conflicts
	# Shows three sections in conflicts:
	# <<<<<<< HEAD (your changes)
	# ||||||| merged common ancestors (original content)
	# ======= (separator)
	# >>>>>>> branch-name (incoming changes)
	# This provides more context than the default two-way conflict style
	conflictStyle = diff3

[alias]
	# Create a compact, graphical log view (lg1)
	# --graph: Shows branch/merge history as ASCII art
	# --abbrev-commit: Shows shortened commit hashes
	# --decorate: Shows branch and tag names
	# --format: Custom format string with colors:
	#   - Blue hash, green relative date, white subject, dim author, auto branch/tag colors
	# --all: Shows all branches, not just current branch
	lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(auto)%d%C(reset)' --all

	# Create a more detailed graphical log view (lg2)
	# Similar to lg1 but includes:
	# - Absolute date (%aD) in cyan in addition to relative date
	# - Two-line format with commit message and author on separate lines
	# - More spacing for better readability
	lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(auto)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)'

	# Set default log alias to use the compact format (lg1)
	# Now you can just type 'git lg' to get the nice graphical log
	lg = lg1