Reference
Git Command Reference
Common workflows grouped by task
Getting Started
Clone an existing repo or start a new one locally.
git clone <url> Clone a repository ? Downloads a repository from a remote URL to your local machine. Creates a new folder with the repo's contents.
git init Initialize a new repository ? Initializes a new Git repository in the current directory. Creates a hidden .git folder to track changes.
git status Check repository status ? Shows which files have been modified, staged, or are untracked. Essential for knowing what Git sees.
Branching
Work on features or fixes in isolation without affecting the main code.
git branch <name> Create a new branch ? Creates a new branch but doesn't switch to it. Like making a copy of your code to experiment with.
git checkout <branch> Switch to a branch ? Switches to an existing branch. Your working directory updates to match that branch's state.
git switch <branch> Switch to a branch (modern) ? Modern way to switch branches. Cleaner than checkout — just switches, doesn't do other things.
git checkout -b <name> Create and switch to new branch ? Creates a new branch and switches to it in one step. Shortcut for branch + checkout.
git switch -c <name> Create and switch to new branch (modern) ? Modern way to create and switch to a new branch. -c flag stands for create.
git branch -d <branch> Delete a branch ? Deletes a branch that's already been merged. Use -D (force delete) to delete unmerged branches (careful).
Staging & Committing
Save your changes to Git's history in organized snapshots.
git add <file> Stage a file ? Stages a specific file for commit. Think of it as saying 'I want to include this change in my next snapshot.'
git add . Stage all changes ? Stages all modified and new files in the current directory. Quick way to stage everything at once.
git commit -m "message" Commit staged changes ? Creates a snapshot of staged changes with a descriptive message. This is your save point in history.
git commit -am "message" Stage and commit in one step ? Stages all modified files (not new ones) and commits in one step. Only for files Git already knows about.
Syncing
Share changes with your team and get updates from the remote repository.
git fetch Fetch changes from remote ? Downloads changes from remote but doesn't merge them. Lets you see what's new before integrating.
git pull Fetch and merge changes ? Fetches AND merges remote changes into your current branch. Shortcut for fetch + merge.
git push Push changes to remote ? Uploads your local commits to the remote repository. Shares your work with the team.
git push -u origin <branch> Push and set upstream ? Pushes and sets upstream tracking. First time pushing a new branch. After this, just 'git push' works.
Undoing Changes
Mistakes happen. Here's how to fix them without breaking everything.
git checkout -- <file> Discard changes to a file ? Discards changes to a file since the last commit. Dangerous — you lose your work permanently.
git reset HEAD <file> Unstage a file ? Unstages a file but keeps your changes. Moves from 'staged' back to 'modified'.
git reset --soft HEAD~1 Undo last commit, keep changes ? Undoes the last commit but keeps your changes staged. Like saying 'I want to redo that commit.'
git reset --hard HEAD~1 Undo last commit, discard changes ? Undoes the last commit AND discards your changes. Dangerous — you lose everything since that commit.
Stashing
Temporarily save your work when you need to switch contexts.
git stash Stash current changes ? Saves your uncommitted changes in a stack and reverts your working directory to the last commit. Like putting work in a drawer.
git stash list List stashes ? Shows all your stashed changes with their index numbers. Lets you see what you have saved.
git stash pop Apply and remove stash ? Applies the most recent stash and removes it from the stack. Like taking your work out of the drawer.
git stash drop Remove a stash ? Removes a stash from the stack without applying it. Deletes it permanently.
Rebasing
Rewrite history to keep it linear. Advanced but powerful.
git rebase <branch> Rebase current branch onto another ? Moves your branch's commits to the tip of another branch. Rewrites history. Don't use on shared branches.
git rebase -i HEAD~3 Interactive rebase last 3 commits ? Interactive rebase of last 3 commits. Lets you reorder, squash, or edit commits. Powerful but dangerous.
git rebase --abort Abort a rebase in progress ? Cancels a rebase in progress and restores your branch to its original state. Safety net when things go wrong.