Last Updated: May 1, 2025
Setup & Configuration
| Item | Description |
|---|---|
git config --global user.name Name | Set your commit author name globally |
git config --global user.email email | Set your commit author email globally |
git config --global core.editor vim | Set default text editor for commit messages |
git config --list | Show all current configuration settings |
git config --global init.defaultBranch main | Set default branch name to main |
git init | Create a new Git repository in current directory |
git clone url | Clone an existing repository from a remote URL |
git clone url --depth 1 | Shallow clone — only the latest commit |
git remote add origin url | Link local repository to a remote |
git remote -v | Show all remote repositories with URLs |
Daily Workflow
git statusShow working tree status: modified, staged, untracked
git add fileStage a specific file for the next commit
git add .Stage all changes in the current directory
git add -pInteractively stage hunks of changes (very useful!)
git commit -m messageCommit staged changes with a message
git commit -am messageAdd all tracked files and commit in one step
git push origin mainPush commits to the remote main branch
git pull origin mainFetch and merge remote changes into local
git fetch originDownload remote changes without merging
git log --oneline --graph --allCompact visual history of all branches
Undoing Changes
git restore fileDiscard changes in working directory
git restore --staged fileUnstage a file (keep the changes)
git reset HEAD~1Undo last commit, keep changes as unstaged
git reset --soft HEAD~1Undo last commit, keep changes staged
git reset --hard HEAD~1Undo last commit AND discard all changes
git revert commitCreate a new commit that undoes a specific commit
git stashTemporarily save uncommitted changes
git stash popRestore the most recently stashed changes
git stash listList all stashed changes
git clean -fdRemove untracked files and directories
Inspecting History
git logFull commit history with author, date, and message
git log --onelineCompact one-line-per-commit view
git log -p fileShow commits that changed a specific file
git show commitShow full details of a specific commit
git diffShow unstaged changes since last commit
git diff --stagedShow staged changes about to be committed
git blame fileShow who last modified each line of a file
git tagList all tags
git tag -a v1.0 -m messageCreate an annotated tag
git shortlog -snShow commit count per author
Pro Tip: Use `git status` frequently — it tells you exactly what state your repository is in before every command.