HOME
Git
General
git init
# Initialize a local Git repository
git clone <git_repo_url>
# Clone a git repo onto your local workspace
git pull
# Get latest code from remote
Commit and push
git status
# Check status of files
git add .
# Stage all files
git commit -m "message"
# Commit changes
git push origin main
# Push to remote
Branching
git branch -a
# View all branches
git branch <new_branch>
# Create new branch
git checkout <new_branch>
# Checkout the newly created branch
git push origin <new_branch>
# Push branch to remote
git branch -d <branch_name>
# Delete local branch
git push origin -d <branch_name>
# Delete remote branch
Merge
git checkout main
# Checkout main branch
git pull origin main
# Get latest code from remote
git pull origin <feature_branch>
# Merge feature branch
git push origin main
# Push main branch to remote
git branch -d <feature_branch>
# Delete branch
git push origin --delete <feature_branch>
# Delete branch from remote
git branch -a
# List all branches
git branch -r
# List all remote branches
Undo
git commit --amend -m "new message"
# Change commit message
git reset .
# Unstage all files
git reset <file>
# Unstage a file
git restore <file>
# Undo changes to a file
git reset HEAD~
# Undo the last commit
git reset --hard
# Discard all changes
git clean -n
# Preview files and directories that will be removed
git clean -fd
# Remove all untracked files and directories
Revert
git revert <commit_hash>
# Revert a commit
Stash
git stash
# Stash changes
git stash pop
# Apply stashed changes and remove from stash
git stash list
# List stashes
Bisect
git bisect start
# Start bisecting
git bisect bad
# Mark current commit as bad
git bisect good <commit_hash>
# Mark a commit as good
git bisect reset
# Exit bisect mode
Log
git log
# View commit history
git log --oneline
# View commit history in one line