liminfo

Git Reference

Free reference guide: Git Reference

54 results

About Git Reference

The Git Command Reference is a comprehensive, searchable cheat sheet for the most commonly used Git commands, organized into nine categories: Basics (git init, clone, add, commit, status, diff, rm, mv), Branch management (git branch, checkout, switch, merge, rename, delete), Remote operations (git remote, fetch, pull, push with upstream tracking), Stash (git stash, pop, list, apply, drop), Log and history inspection (git log --oneline, --graph, -p, git show, git blame), Undo operations (git reset --soft/--hard, git revert, git restore, git clean), Rebase (git rebase, interactive rebase, abort/continue, cherry-pick), Tags (lightweight and annotated tags, push tags, delete), and Config (global user settings, editor, command aliases).

Git is the world's most widely used version control system, and every software developer, technical writer, data scientist, and DevOps engineer encounters its commands daily. This reference is particularly useful for developers switching between projects, recalling the exact flags for git log --oneline --graph --all, remembering the correct syntax for git push -u origin when setting upstream tracking, or quickly distinguishing between git reset --soft and --hard when undoing recent commits.

Entries are organized so that beginners can start with the Basics category and progressively explore more advanced topics like Rebase, Stash, and cherry-pick. Each entry includes the command with its most common flags, a one-line description of the effect, and a ready-to-run shell example. The reference also covers the newer git switch and git restore commands introduced in Git 2.23 alongside their classic git checkout equivalents, helping developers who use different Git versions.

Key Features

  • Basics: git init, clone, add (staged), commit -m, status, diff/diff --staged, rm, mv
  • Branch: git branch (list/create/delete/rename), checkout -b, switch, merge
  • Remote: git remote -v/add, fetch (no merge), pull, push, push -u for upstream tracking
  • Stash: git stash, stash pop, stash list, stash apply (keep), stash drop
  • Log: git log --oneline, --graph, -p (diff), git show, git blame for line-by-line authorship
  • Undo: git reset --soft (keep changes), --hard (discard), git revert (new commit), git restore, git clean -fd
  • Rebase: git rebase, rebase -i (interactive squash/edit), abort/continue, cherry-pick
  • Tags and Config: lightweight vs annotated tags, push --tags, global user.name/email, aliases

Frequently Asked Questions

What is the difference between git fetch and git pull?

git fetch downloads changes from the remote repository into your local remote-tracking branches (e.g. origin/main) but does not modify your working branch. git pull is a combination of git fetch followed by git merge (or git rebase with --rebase flag), immediately integrating the remote changes into your current branch.

What is the difference between git reset --soft, --mixed, and --hard?

git reset --soft HEAD~1 moves the HEAD pointer back but keeps all changes staged. git reset (--mixed, the default) moves HEAD back and unstages the changes but keeps them in the working directory. git reset --hard moves HEAD back and completely discards both staged and working directory changes — use with caution as the discarded changes are not easily recoverable.

When should I use git revert instead of git reset?

Use git revert when you need to undo a commit that has already been pushed to a shared remote repository. git revert creates a new commit that reverses the changes, preserving the full history. Use git reset only for local commits that have not been pushed, as force-pushing after reset rewrites history and causes problems for collaborators.

What does git stash do and when should I use it?

git stash temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to the last commit. Use it when you need to quickly switch branches without committing half-finished work. Run git stash pop to reapply the most recent stash, or git stash apply stash@{0} to apply a specific stash without removing it from the list.

What is git cherry-pick and how does it differ from git merge?

git cherry-pick applies the changes introduced by a specific commit onto your current branch, creating a new commit with a new hash. It is used when you want a specific fix or feature from one branch without merging the entire branch. git merge incorporates all commits from the source branch into the target branch.

What is interactive rebase (git rebase -i) used for?

git rebase -i HEAD~n opens an interactive editor showing the last n commits. You can squash multiple commits into one, reorder commits, edit commit messages, drop unwanted commits, or split a commit. This is commonly used to clean up a messy feature branch before merging it into the main branch, producing a cleaner, more readable history.

What is the difference between git checkout and git switch?

git switch is a newer command (Git 2.23+) designed specifically for switching branches, making the intent clearer. git switch main switches to an existing branch; git switch -c new-branch creates and switches to a new branch. git checkout can switch branches but also has many other uses (checking out files, detached HEAD), which made it confusing for beginners. Both commands coexist; git switch is preferred for branch operations.

How do I set up Git for the first time?

The minimum configuration is setting your name and email globally: git config --global user.name "Your Name" and git config --global user.email "you@example.com". These values appear in every commit you make. Optionally set a default editor with git config --global core.editor "vim" and create shortcuts with git config --global alias.co checkout.