Copy-ready commands for everyday Git work. Replace the words in <angle brackets> with your own values.
Set up and start
Command
What it does
git config --global user.name "Your Name"
Sets the name on your commits
git config --global user.email "you@example.com"
Sets the email on your commits
git init
Turns the current folder into a repository
git clone <url>
Copies a remote repository to your computer
Save changes
Command
What it does
git status
Shows changed, staged and untracked files
git diff
Shows unstaged changes line by line
git add <file>
Stages one file for the next commit
git add .
Stages every change in the current folder
git commit -m "message"
Saves the staged changes as a commit
git log --oneline
Shows a short history of commits
Branches
Command
What it does
git branch
Lists local branches
git switch -c <name>
Creates a branch and moves to it
git switch <name>
Moves to an existing branch
git merge <name>
Merges that branch into the current one
git branch -d <name>
Deletes a merged branch
Sync with a remote
Command
What it does
git fetch
Downloads new commits without changing your files
git pull
Fetches and merges the remote changes
git push
Uploads your commits
git push -u origin <branch>
Pushes a new branch and remembers it
Undo and fix
Command
What it does
git restore <file>
Discards unstaged changes in a file (cannot be undone)
git restore --staged <file>
Unstages a file but keeps the changes
git commit --amend
Edits the last commit message or adds to it
git revert <commit>
Makes a new commit that undoes an earlier one
git stash
Sets your changes aside temporarily
git stash pop
Brings the stashed changes back
Be careful with commands that rewrite history, such as git reset --hard and git push --force. They can destroy work, especially on branches other people use.