Git Commands Quick Reference Guide
This quick reference guide covers essential Git commands for repository setup, staging, branching, merging, and working with remote repositories, making it a handy tool for developers to streamline their workflow.
Setup & Init
| Command | Description |
|---|---|
| git init | Initialise a new local repository in the current directory |
| git clone <url> | Clone a remote repository into a new local directory |
| git config --global user.name "Name" | Set the name attached to all commits globally |
| git config --global user.email "email" | Set the email address attached to all commits globally |
Staging & Snapshots
| Command | Description |
|---|---|
| git status | Show the working tree status — staged, unstaged, and untracked files |
| git add <file> | Stage a specific file, ready to be included in the next commit |
| git add . | Stage all changed and new files in the current directory |
| git reset <file> | Unstage a file while preserving its changes in the working directory |
| git diff | Show unstaged changes between the working directory and the index |
| git diff --staged | Show changes staged for the next commit vs the last commit |
| git commit -m "message" | Commit all staged changes with a descriptive message |
| git commit --amend | Replace the last commit with a new one — useful for fixing messages or adding missed files |
Branching & Merging
| Command | Description |
|---|---|
| git branch | List all local branches; the current branch is highlighted |
| git branch <name> | Create a new branch at the current commit |
| git checkout <branch> | Switch to an existing branch |
| git checkout -b <name> | Create a new branch and switch to it immediately |
| git switch <branch> | Switch branches (modern alternative to checkout) |
| git merge <branch> | Merge the specified branch into the current branch |
| git branch -d <name> | Delete a branch that has already been merged |
| git rebase <branch> | Reapply commits on top of another branch for a cleaner history |
Remote Repositories
| Command | Description |
|---|---|
| git remote -v | List all configured remote connections with their URLs |
| git remote add <name> <url> | Add a new remote connection under a short alias |
| git fetch <remote> | Download changes from the remote without merging them locally |
| git pull | Fetch and merge changes from the tracked remote branch |
| git push <remote> <branch> | Upload local commits to a remote branch |
| git push -u origin <branch> | Push and set the upstream tracking branch in one step |
| git remote rm <name> | Remove a remote connection by its alias |
History & Inspection
| Command | Description |
|---|---|
| git log | Show the full commit history for the current branch |
| git log --oneline | Show a compact one-line summary of each commit |
| git log --graph | Display the branch and merge history as an ASCII graph |
| git show <commit> | Show the metadata and content changes of a specific commit |
| git blame <file> | Show who last modified each line of a file and when |
Undoing Changes
| Command | Description |
|---|---|
| git revert <commit> | Create a new commit that undoes the changes from a specific commit |
| git reset --soft HEAD~1 | Undo the last commit, keeping changes staged |
| git reset --hard HEAD~1 | Undo the last commit and discard all changes permanently |
| git restore <file> | Discard unstaged changes in a file, reverting to the last commit |
| git clean -fd | Remove untracked files and directories from the working tree |
Stashing
| Command | Description |
|---|---|
| git stash | Temporarily shelve uncommitted changes to work on something else |
| git stash pop | Re-apply the most recently stashed changes and remove them from the stash |
| git stash list | List all stashed changesets |
| git stash drop | Discard the most recent stash entry |
person people found this useful.