Git Cheatsheet
Getting Started
git init
Initialize a new Git repository
git clone <url>
Clone a remote repository
git config --global user.name "Your Name"
Set your name
Daily Workflow
git status
Show working directory status
git add <file>
Stage a file for commit
git add .
Stage all changes
git commit -m "Message"
Commit staged changes
git push
Push commits to remote repository
git pull
Pull changes from remote repository
git fetch
Fetch changes without merging
Branching
git branch
List all branches
git branch <name>
Create new branch
git checkout <branch>
Switch to branch
git checkout -b <name>
Create and switch to new branch
git merge <branch>
Merge branch into current branch
git branch -d <branch>
Delete branch (must be merged)
git branch -D <branch>
Force delete branch
Undoing Changes
git checkout -- <file>
Discard changes to file
git reset HEAD <file>
Unstage a file
git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --hard HEAD~1
Undo last commit, discard changes
git revert <commit>
Create new commit that undoes previous commit
Viewing History
git log
Show commit history
git log --oneline
Show compact commit history
git log --graph
Show commit history with graph
git show <commit>
Show details of a commit
git diff
Show unstaged changes
git diff --staged
Show staged changes
git diff <commit1> <commit2>
Show differences between commits
Working with Remotes
git remote -v
List remote repositories
git remote add <name> <url>
Add remote repository
git push origin <branch>
Push branch to remote
git pull origin <branch>
Pull branch from remote
git remote rm <name>
Remove remote repository
Stashing
git stash
Save current changes temporarily
git stash pop
Apply and remove stashed changes
git stash list
List all stashes
git stash apply <stash>
Apply specific stash
git stash drop <stash>
Remove specific stash
Tagging
git tag
List all tags
git tag <name>
Create lightweight tag
git tag -a <name> -m "Message"
Create annotated tag
git push --tags
Push all tags to remote
git tag -d <name>
Delete local tag
Useful Aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
Troubleshooting
Common Issues:
- Merge conflicts: Use
git status to see conflicts, edit files manually, then git add and git commit
- Detached HEAD: Use
git checkout <branch> to reattach to a branch
- Force push: Use
git push --force-with-lease instead of --force for safety
git clean -fd
Remove untracked files and directories
git fsck --full
Check repository integrity
git reflog
Show history of all repository changes