Git Command Reference

Setup & Config

CommandWhat it doesCommon Flag/Example
git config --global user.name "Your Name"Sets the name you want to be associated with your commits--global sets the config for your user account
git config --global user.email "you@example.com"Sets the email you want to be associated with your commits--global sets the config for your user account
git config --global core.editor "vim"Sets the default text editor for Git--global sets the config for your user account
git initInitializes a new Git repository
git clone <repository>Clones an existing repository
git remote add origin <url>Adds a remote repository
git remote -vShows all remote repositories
git config --listLists all Git configuration settings
git config --global core.autocrlf trueConfigures line endings for Windows--global sets the config for your user account
git config --global core.autocrlf falseConfigures line endings for Unix/Linux--global sets the config for your user account

Snapshotting

CommandWhat it doesCommon Flag/Example
git add <file>Adds a file to the staging area
git add .Adds all changes to the staging area
git add -AAdds all changes including deletions
git commit -m "message"Commits staged changes with a message-m allows you to type a commit message directly
git commit -a -m "message"Commits all tracked files with a message-a stages all tracked files
git commit --amendAmends the most recent commit
git statusShows the status of the working directory and staging area
git diffShows differences between working directory and staging area
git diff --cachedShows differences between staging area and last commit
git diff HEAD~1Shows differences between current and previous commit

Branching & Merging

CommandWhat it doesCommon Flag/Example
git branchLists all branches in the repository
git branch -aLists all local and remote branches
git branch <new-branch>Creates a new branch
git branch -m <old-name> <new-name>Renames a branch
git checkout <branch>Switches to the specified branch
git checkout -b <new-branch>Creates and switches to a new branch
git merge <branch>Merges the specified branch into the current branch
git merge --no-ff <branch>Merges without fast-forwarding
git merge --squash <branch>Squashes commits into one
git branch -d <branch>Deletes a merged branch
git branch -D <branch>Deletes a branch regardless of its merge status

Remote

CommandWhat it doesCommon Flag/Example
git fetch <remote>Downloads objects and refs from another repository
git fetch --allFetches all remotes
git pull <remote> <branch>Fetches and merges a remote branch into the current branch
git pull --rebaseFetches and rebases instead of merging
git push <remote> <branch>Updates the remote branch with your local changes
git push --set-upstream origin <branch>Sets upstream for the current branch
git push --force-with-leaseForce pushes with safety
git push --tagsPushes all tags to the remote
git push origin --delete <branch>Deletes a remote branch
git remote update originUpdates remote references

Inspection & History

CommandWhat it doesCommon Flag/Example
git logShows commit logs
git log --onelineShows commit logs in a compact format
git log --graphShows commit logs with a graph
git log --oneline --graph --allShows all branches with a graph
git log --author="John Doe"Shows commits by a specific author
git log --since="2023-01-01"Shows commits since a specific date
git show <commit>Shows detailed information about a commit
git show --name-only <commit>Shows only file names changed in a commit
git blame <file>Shows line-by-line commit information for a file
git reflogShows reference logs

Undoing changes

CommandWhat it doesCommon Flag/Example
git reset --hard HEAD~1Resets the current branch to the previous commit--hard resets working directory and index
git reset --soft HEAD~1Resets the current branch but keeps changes staged--soft keeps changes in staging area
git reset HEAD <file>Unstages a file from the staging area
git revert <commit>Reverts the specified commit
git revert --no-commit <commit>Reverts without committing
git restore <file>Restores working directory files
git restore --staged <file>Unstages a file from the staging area
git checkout -- <file>Discards changes in the working directory
git checkout HEAD -- <file>Restores a file to its state in the last commit
git clean -fdRemoves untracked files and directories-f forces removal, -d removes directories

Stashing

CommandWhat it doesCommon Flag/Example
git stashStashes the changes in a dirty working directory away
git stash save "message"Stashes changes with a message
git stash listLists the stash entries
git stash showShows the changes in the stash
git stash show -pShows the changes in the stash in patch format
git stash popApplies the most recent stashed changes and removes them from the stash list
git stash applyApplies the most recent stashed changes without removing them
git stash apply stash@{n}Applies a specific stash entry
git stash dropDrops the most recent stash entry
git stash clearRemoves all stash entries

Rewriting

CommandWhat it doesCommon Flag/Example
git rebase <branch>Applies commits of the current branch on top of another base tip
git rebase -i HEAD~3Interactive rebase of the last 3 commits-i enables interactive mode
git rebase --onto <new-base> <old-base> <branch>Rebases a branch onto a new base
git rebase --abortAborts the current rebase operation
git cherry-pick <commit>Applies the changes introduced by some existing commits
git cherry-pick -n <commit>Cherry-picks without committing
git cherry-pick <commit1> <commit2>Cherry-picks multiple commits
git commit --amendAmends the most recent commit
git commit --amend --no-editAmends the most recent commit without changing the message
git rebase -i --autosquashInteractive rebase with autosquash

Tags

CommandWhat it doesCommon Flag/Example
git tag <tagname>Creates a tag with the specified name
git tag -a <tagname> -m "message"Creates an annotated tag with a message-a creates an annotated tag
git tag -lLists all tags
git tag -l "v1.*"Lists tags matching a pattern
git push origin <tagname>Pushes a tag to the remote repository
git push origin --tagsPushes all tags to the remote repository
git checkout <tagname>Checks out a tag
git checkout -b <branchname> <tagname>Creates a branch from a tag
git tag -d <tagname>Deletes a tag
git tag -v <tagname>Verifies a tag