Git & GitHub Command Cheat Sheet: From Basics to Advanced

Whether you’re a beginner just getting started with Git or an experienced developer who needs quick reference, this Git & GitHub Cheat Sheet has you covered.

YASH PATEL1/13/20263 min read12 views
GitHubGit Commands
Git & GitHub Command Cheat Sheet: From Basics to Advanced

🔹 Basic Workflow

=> These are the everyday Git commands you’ll use to track changes and sync with GitHub:

  • git init → Initialize a new Git repository
  • git clone [url] → Download a project from GitHub
  • git add [file] → Stage a specific file for commit
  • git add . → Stage all changes for commit
  • git commit -m "message" → Commit staged changes
  • git push origin [branch] → Push commits to remote
  • git pull origin [branch] → Pull latest changes

🔹 Branch Masters

=> Branching lets you work on features independently without affecting main code:

  • git branch → List all branches
  • git branch [name] → Create new branch
  • git checkout [branch] → Switch to branch
  • git merge [branch] → Merge branch into current
  • git branch -d [branch] → Delete branch locally
  • git push origin --delete [branch] → Delete remote branch

🔹 History Explorers

=> Explore and understand the history of your codebase:

  • git log --oneline → Compact commit history
  • git log --graph --oneline --all → Visual commit tree
  • git show [commit] → Show changes in specific commit
  • git diff → Show unstaged changes
  • git diff --staged → Show staged changes
  • git blame [file] → See who changed each line

🔹 Undo Artists

=> Made a mistake? No worries, Git’s got your back:

  • git reset --soft HEAD^ → Undo commit, keep changes staged
  • git reset --hard HEAD^ → Remove last commit completely
  • git checkout -- [file] → Discard changes in working directory
  • git revert [commit] → Create new commit that undoes changes
  • git clean -fd → Remove untracked files & directories

🔹 GitHub Collaborators

=> For open-source contributions or working in teams:

  • git fork → Create a personal copy of repository
  • git remote add upstream [url] → Add original repo as upstream
  • git fetch upstream → Get updates from original repo
  • git push -u origin [branch] → Push & set upstream branch
  • git pull --rebase upstream main → Update fork with rebase

🔹 Stash & Cleanup

=> When you need to pause work without committing:

  • git stash → Temporarily store changes
  • git stash pop → Restore most recent stash
  • git stash list → View all stashes
  • git stash drop → Delete most recent stash
  • git prune → Remove unreachable objects

🔹 Tagging & Releases

=> Tags are great for marking releases:

  • git tag [name] → Create lightweight tag
  • git tag -a [version] -m "message" → Annotated tag
  • git push origin --tags → Push all tags to remote
  • git tag -d [name] → Delete local tag
  • git push origin --delete tag [name] → Delete remote tag

🔹 Advanced Tools

=> Powerful commands for debugging and history management:

  • git bisect → Binary search to find buggy commit
  • git rebase -i [commit] → Interactive rebase (rewrite history)
  • git cherry-pick [commit] → Apply specific commit to current branch
  • git reflog → Show reference log (lifesaver for recovery)
  • git worktree add → Add additional working trees

🔹 Configuration

=> Personalize Git to fit your workflow:

  • git config --global user.name "Name" → Set global username
  • git config --global user.email "email" → Set global email
  • git config --global core.editor "code --wait" → Use VS Code as editor
  • git config --global alias.co checkout → Create shortcuts
  • git config --list → Show all configurations

# Final Thoughts

Git is powerful, but it can feel overwhelming when you’re just starting out. Bookmark this cheat sheet and use it as a quick reference whenever you’re stuck.