Git and GitHub Cheat Sheet
by Codex
1. Ó Initial Setup & Configuration 2. + Creating Repositories 3. ✓ Basic Workflow
Configure Your Identity Initialize New Repository Check Status
# Set your name for commits # Create a new Git repository in current # See which files are modified, staged, or
git config --global [Link] "Your Name" folder untracked
git init git status
# Set your email for commits
git config --global [Link] # Create repo in a new directory # Short format status
"you@[Link]" git init project-name git status -s
# This creates a .git folder with repository
data Add Files to Staging Area
View Your Configuration
# See all configuration settings # Stage a specific file for commit
git config --list git add [Link]
Clone Existing Repository
# See specific setting # Copy a remote repository to your computer # Stage all changes in current directory
git config [Link] git clone [Link] git add .
git config [Link] # Clone into a specific folder name # Stage all changes in entire repository
git clone [Link] git add -A
my-folder # Stage all files with specific extension
Additional Setup
# Clone a specific branch git add *.js
# Set default text editor
git config --global [Link] "vim" git clone -b branch-name # Interactive staging (choose what to stage)
[Link] git add -p
# Enable colored output
git config --global [Link] auto
# Set default branch name Commit Changes
git config --global [Link] main # Save staged changes with a message
git commit -m "Add new feature"
# Stage all tracked files and commit
git commit -am "Update documentation"
# Modify the last commit (add forgotten
files)
git commit --amend
# Amend without changing the commit message
git commit --amend --no-edit
4. Î Viewing History & Changes 5. Ñ Branching 6. Merging Branches
View Commit Log Why Branches? Basic Merge
# Show full commit history # Branches let you work on features # First, switch to branch you want to merge
git log independently INTO
# Compact one-line view # Main branch = production code git checkout main
git log --oneline # Feature branches = new development # Then merge the feature branch into main
# Show visual branch graph git merge feature-login
git log --graph --oneline --all Working with Branches # This combines the changes from
# Show last 5 commits # List all local branches (* = current feature-login into main
git log -5 branch)
# Show commits by specific author git branch Merge Options
git log --author="John" # List all branches (local and remote) # Create a merge commit even if fast-forward
# Show commits with specific word in message git branch -a possible
git log --grep="bug fix" # Create a new branch git merge --no-ff feature-branch
# Show commits for specific file git branch feature-login # Combine all commits into one
git log [Link] # Switch to a branch git merge --squash feature-branch
git checkout feature-login
# Create and switch to new branch (shortcut) Handling Merge Conflicts
View Differences
git checkout -b feature-payment # If merge conflicts occur, Git will tell
# Show unstaged changes
git diff # Modern way to switch branches you
git switch feature-login # 1. Open conflicted files
# Show staged changes (ready to commit)
git diff --staged # Create and switch (modern syntax) # 2. Look for <<<<<<< ======= >>>>>>>
git switch -c feature-payment markers
# Compare two commits # 3. Edit to keep the code you want
git diff commit1 commit2 # Switch back to previous branch
git switch - # 4. Remove the conflict markers
# Compare two branches # 5. Stage the resolved files
git diff branch1 branch2 git add [Link]
Delete Branches # 6. Complete the merge with a commit
# Delete a branch (safe - prevents if git commit
Show Commit Details
unmerged) # Or abort the merge if needed
# Show details of a specific commit
git branch -d feature-login git merge --abort
git show commit-hash
# Force delete a branch
# Show details of latest commit
git branch -D feature-login
git show HEAD
# Rename current branch
git branch -m new-name
7. Undoing & Restoring 8. Å Remote Repositories 9. Pushing to Remote
Unstage Files View Remotes Push Changes
# Remove file from staging area (keep # List remote repositories # Push commits to remote repository
changes) git remote git push
git restore --staged [Link] # List with URLs # Push to specific remote and branch
# Old way to unstage git remote -v git push origin main
git reset HEAD [Link] # Show detailed info about a remote # Push and set upstream (first time)
git remote show origin git push -u origin feature-branch
Discard Changes # After -u, you can just use: git push
# Discard changes in working directory Add/Remove Remotes
git restore [Link] # Add a new remote repository Push All
# Discard all changes (dangerous!) git remote add origin # Push all branches
git restore . [Link] git push --all
# Old way to discard changes # Remove a remote # Push tags to remote
git checkout -- [Link] git remote remove origin git push --tags
# Rename a remote
git remote rename old-name new-name
Undo Commits Delete Remote Branch
# Undo last commit, keep changes staged # Delete a branch from remote repository
git reset --soft HEAD~1 Authentication git push origin --delete branch-name
# Undo last commit, unstage changes # For HTTPS: Use personal access token as
git reset --mixed HEAD~1 password
# For SSH: Set up SSH keys in GitHub Force Push (Dangerous!)
# Undo last commit, discard all changes
settings # Overwrite remote with local (use
(dangerous!)
# Check if SSH is working: carefully!)
git reset --hard HEAD~1
ssh -T git@[Link] git push --force
# HEAD~1 = one commit back, HEAD~2 = two
# Safer force push (fails if remote has
commits back
changes)
git push --force-with-lease
Revert Commits
# Create new commit that undoes a previous
commit
git revert commit-hash
# This is safer than reset for public
branches
10. Fetching & Pulling 11. Stashing (Temporary Storage) 12. ² Collaboration Workflow
Fetch vs Pull Why Stash? Typical Team Workflow:
# Fetch = download changes, don’t merge # Save work temporarily without committing # 1. Clone the repository
# Pull = fetch + merge (fetch and apply # Useful when switching branches with git clone [Link]
changes) uncommitted work
# 2. Create a feature branch
Fetch Changes Basic Stashing git checkout -b feature-new-button
# Download changes from remote (safe) # Save current changes temporarily
git fetch git stash
# 3. Make changes and commit
# Fetch from specific remote # Stash with a descriptive message git add .
git fetch origin git stash save "work in progress on login" git commit -m "Add new button feature"
# Fetch from all remotes # Include untracked files in stash
git fetch --all git stash -u
# 4. Push your branch to remote
# Remove references to deleted remote git push -u origin feature-new-button
branches
View Stashes
git fetch --prune
# List all stashed changes # 5. Create Pull Request on GitHub
# After fetch, you can review changes before git stash list # - Go to repository on GitHub
merging
# Show contents of latest stash # - Click "Pull requests" > "New pull
git stash show request"
Pull Changes # Show detailed diff of a stash # - Select your branch and submit for review
# Download and merge changes in one step git stash show -p stash@{0}
git pull # 6. After approval, update your main
# Pull from specific remote and branch branch
Apply Stashes
git pull origin main git checkout main
# Apply latest stash and keep it in list
# Pull with rebase instead of merge git stash apply git pull origin main
git pull --rebase
# Apply specific stash
# Always pull before pushing to avoid git stash apply stash@{2} # 7. Delete the feature branch (cleanup)
conflicts! git branch -d feature-new-button
# Apply latest stash and remove from list
git stash pop
Syncing a Fork
Delete Stashes # Add original repo as upstream
# Delete specific stash git remote add upstream
git stash drop stash@{0} [Link]
# Delete all stashes # Fetch changes from original
git stash clear git fetch upstream
# Switch to main branch
git checkout main
# Merge upstream changes
git merge upstream/main
# Push updates to your fork
git push origin main
13. { Useful Extras 14. ] Quick Reference 15. Best Practices
Tagging Releases Common Terms DO:
# Create a tag (like v1.0, v2.0) Repository (repo): Project folder tracked by Git • Commit often with clear messages
git tag v1.0.0 Commit: Snapshot of changes • Pull before you push
# Create annotated tag with message Branch: Independent line of development • Use branches for new features
git tag -a v1.0.0 -m "Release version 1.0" HEAD: Pointer to current commit • Write meaningful commit messages
Origin: Default name for remote repository • Review changes before committing
# Push tag to remote
Main/Master: Default primary branch • Keep commits focused (one feature/fix)
git push origin v1.0.0
Stage: Prepare files for commit
# List all tags Clone: Copy repository to local machine
git tag Fork: Personal copy of someone’s repository DON’T:
Pull Request (PR): Request to merge changes • Commit sensitive data (passwords, API keys)
• Use git push --force on shared branches
.gitignore File • Make huge commits with many unrelated changes
# Create .gitignore to exclude files from Essential Shortcuts • Commit directly to main branch
Git # Create alias for common commands • Leave merge conflict markers in code
# Common patterns: git config --global [Link] status
*.log # Ignore all log files git config --global [Link] checkout
node modules/ # Ignore folder git config --global [Link] branch Good Commit Messages:
.env # Ignore environment variables git config --global [Link] commit Add user authentication feature
*.tmp # Ignore temporary files Fix bug in payment calculation
# Now you can use: git st, git co, git br,
# Apply gitignore to already tracked files: Update README with setup instructions
git ci
git rm --cached filename Refactor database connection logic
git rm -r --cached .
Bad Commit Messages:
Useful Commands fix
# See who changed each line of a file updated stuff
git blame [Link] changes
# Search for text in repository asdfasdf
git grep "search term"
# Clean untracked files (dry run first!)
git clean -n
git clean -f
♥ Remember: Git is about collaboration and version control. Commit often, write clear messages, and always pull before you push!
? Need Help? Use git help <command> or visit [Link]