Git Commands Cheatsheet
Basic Configuration
git config --global user.name "Your Name"
-> Set your Git username
git config --global user.email "you@example.com"
-> Set your Git email
git config --global core.editor code
-> Set default editor (e.g., VS Code)
git config --list
-> View all Git config settings
Repository Initialization
git init
-> Initialize a new Git repository
git clone <url>
-> Clone a repository from remote
Working with Files
git status
-> Check current file status
git add <file>
-> Stage specific file
git add .
-> Stage all changes
git restore <file>
-> Undo changes in working directory
git rm <file>
-> Remove file from Git
Committing Changes
git commit -m "msg"
-> Commit with message
git commit -am "msg"
-> Add and commit tracked files
Branches
git branch
-> List branches
git branch <name>
-> Create new branch
git checkout <name>
-> Switch to branch
git checkout -b <name>
-> Create and switch to branch
git merge <branch>
-> Merge into current branch
git branch -d <name>
-> Delete branch
Remote Repositories
git remote add origin <url>
-> Link local to remote
git remote -v
-> Show remote URLs
git push -u origin main
-> Initial push to set upstream
git push
-> Push changes
git pull
-> Pull and merge changes
git fetch
-> Fetch without merging
Viewing History
git log
-> Show commit history
git log --oneline
-> Compact commit history
git diff
-> Show unstaged changes
git diff --staged
-> Show staged changes
Undoing Changes
git reset <file>
-> Unstage file
git reset --hard
-> Discard all local changes
git revert <commit>
-> Undo a commit safely
git checkout -- <file>
-> Restore file to last commit
Stashing
git stash
-> Save local changes temporarily
git stash pop
-> Reapply last stash
git stash list
-> List saved stashes
Tags
git tag
-> List tags
git tag <v1.0>
-> Create a tag
git push origin <tag>
-> Push a tag to remote
Deleting Remote Branches
git push origin --delete <branch>
-> Delete branch on remote
Aliases
git config --global alias.st status
-> Create `git st` shortcut
git config --global alias.co checkout
-> Create `git co` shortcut
git config --global alias.ci commit
-> Create `git ci` shortcut