Git — Distributed Version Control for Your Source Code

Practical guide to Git — configuration, commits, branching, merge, rebase, remotes, stash and history on the command line, with examples.

Git is the distributed version control system that shapes modern software development: every clone of a repository carries the complete history and works fully offline. Rather than storing individual differences, Git records snapshots of your entire project and links them into a traceable history. Branches let you try out ideas without risk, while merge and rebase bring them back together. This guide walks you through the commands you reach for daily – from initial configuration through commits and branching to remotes, stash and history.

Configuration

git config --global user.name "<name>" — Set your global username for all repositories.

git config --global user.name "John Doe"

git config --global user.email "<email>" — Set your global email for all repositories.

git config --global user.email "john@example.com"

git config --list — List all configuration settings.

git config --global core.editor "<editor>" — Set the default text editor for commit messages.

git config --global core.editor "vim"

git config --global init.defaultBranch <name> — Set the default branch name for new repositories.

git config --global init.defaultBranch main

Repository Basics

git init — Initialize a new Git repository in the current directory.

git clone <url> — Clone a remote repository to your local machine.

git clone https://github.com/user/repo.git

git clone <url> <directory> — Clone a repository into a specific directory.

git clone https://github.com/user/repo.git my-project

git clone --depth <n> <url> — Create a shallow clone with limited commit history.

git clone --depth 1 https://github.com/user/repo.git

git status — Show the working tree status including staged, unstaged, and untracked files.

Staging & Commits

git add <file> — Stage a specific file for the next commit.

git add index.html

git add . — Stage all changes in the current directory and subdirectories.

git add -p — Interactively stage parts of files (hunks).

git commit -m "<message>" — Create a commit with a message.

git commit -m "feat: add login page"

git commit --amend — Modify the most recent commit (message or content).

git commit --amend --no-edit — Add staged changes to the last commit without changing the message.

git reset HEAD <file> — Unstage a file while keeping changes in the working directory.

git reset HEAD index.html

Branching

git branch — List all local branches. The current branch is marked with an asterisk.

git branch -a — List all branches including remote-tracking branches.

git branch <name> — Create a new branch without switching to it.

git branch feature/login

git checkout <branch> — Switch to an existing branch.

git checkout main

git checkout -b <branch> — Create a new branch and switch to it immediately.

git checkout -b feature/login

git switch <branch> — Switch to an existing branch (modern alternative to checkout).

git switch main

git switch -c <branch> — Create and switch to a new branch.

git switch -c feature/login

git branch -d <branch> — Delete a branch that has been fully merged.

git branch -d feature/login

git branch -D <branch> — Force delete a branch even if it has unmerged changes.

git branch -D feature/login

Merging & Rebasing

git merge <branch> — Merge the specified branch into the current branch.

git merge feature/login

git merge --no-ff <branch> — Merge with a merge commit even if fast-forward is possible.

git merge --no-ff feature/login

git merge --abort — Abort the current merge and restore the pre-merge state.

git rebase <branch> — Rebase the current branch onto the specified branch.

git rebase main

git rebase --abort — Abort a rebase in progress and return to the original state.

git cherry-pick <commit> — Apply a specific commit from another branch.

git cherry-pick a1b2c3d

Remote Repositories

git remote -v — List all remote repositories with their URLs.

git remote add <name> <url> — Add a new remote repository.

git remote add origin https://github.com/user/repo.git

git fetch <remote> — Download objects and refs from a remote without merging.

git fetch origin

git pull — Fetch and merge changes from the remote tracking branch.

git pull --rebase — Fetch and rebase local commits on top of remote changes.

git push <remote> <branch> — Push local commits to a remote branch.

git push origin main

git push -u <remote> <branch> — Push and set the upstream tracking branch.

git push -u origin feature/login

git push --tags — Push all local tags to the remote repository.

Stash

git stash — Temporarily save uncommitted changes and clean the working directory.

git stash push -m "<message>" — Stash changes with a descriptive message.

git stash push -m "WIP: login form"

git stash list — List all stashed entries.

git stash pop — Apply the most recent stash and remove it from the stash list.

git stash apply stash@{<n>} — Apply a specific stash without removing it.

git stash apply stash@{0}

git stash drop stash@{<n>} — Remove a specific stash entry.

git stash drop stash@{0}

git stash clear — Remove all stash entries.

Log & History

git log — Show the commit history for the current branch.

git log --oneline — Show commit history in compact one-line format.

git log --graph --oneline --all — Show a visual ASCII graph of all branches.

git log -n <count> — Show only the last N commits.

git log -n 5

git log --author="<name>" — Filter commits by author.

git log --author="John"

git show <commit> — Show details and diff of a specific commit.

git show a1b2c3d

git blame <file> — Show who last modified each line of a file.

git blame index.html

Diff & Comparison

git diff — Show unstaged changes in the working directory.

git diff --staged — Show changes that are staged for the next commit.

git diff <branch1> <branch2> — Compare two branches.

git diff main feature/login

git diff <commit1> <commit2> — Compare two specific commits.

git diff a1b2c3d e4f5g6h

Undoing Changes

git checkout -- <file> — Discard changes in a file and restore it to the last commit.

git checkout -- index.html

git restore <file> — Discard changes in the working directory (modern alternative).

git restore index.html

git restore --staged <file> — Unstage a file while keeping changes.

git restore --staged index.html

git revert <commit> — Create a new commit that undoes the specified commit.

git revert a1b2c3d

git reset --soft HEAD~<n> — Undo last N commits, keeping changes staged.

git reset --soft HEAD~1

git reset --hard HEAD~<n> — Undo last N commits and discard all changes. Use with caution!

git reset --hard HEAD~1

git clean -fd — Remove untracked files and directories from the working tree. Irreversible – preview with git clean -nfd (dry run) first!

Tags

git tag — List all tags in the repository.

git tag <name> — Create a lightweight tag at the current commit.

git tag v1.0.0

git tag -a <name> -m "<message>" — Create an annotated tag with a message.

git tag -a v1.0.0 -m "Release version 1.0.0"

git tag -d <name> — Delete a local tag.

git tag -d v1.0.0

git push origin --delete <tagname> — Delete a remote tag.

git push origin --delete v1.0.0

Conclusion

Git looks overwhelming at first, but daily work rests on a manageable handful of commands: add, commit, push, pull, branch and merge. Add rebase, stash and undoing changes with reset and revert, and you stay in control of your history even on large projects. Adopt meaningful commit messages and a clear branching strategy – that turns Git from a tool into a reliable safety net.

Further Reading

  • gh – GitHub workflows straight from the command line