Git Commands - Cheat Sheet

Git Commands - Cheat Sheet

Git Commands - Cheat Sheet

1. Configuration & Setup
git versionChecks installed Git version. git config --global user.name "[name]"Sets your identity for all commits. git config --global user.email "[email]"Sets commit email address. git config --listLists all applied configuration settings. git initInitializes a new, empty local Git repository. git clone [url]Downloads a repository from a remote server.

💡 Setup Pro-Tips

  • Identity: Match your email to your GitHub profile for proper contribution tracking.
  • Verification: Use git config --list to verify all settings are applied correctly.
2. The Core Workflow
git statusShows which files are modified, staged, or untracked. git add [file]Adds a specific file to the staging area. git add .Adds all changed files in the directory to staging. git add -p"Patch" mode: Stage parts of a file interactively. git commit -m "[message]"Records the snapshot with a message. git commit --amendModifies the last commit to fix typos or add files.

🔥 Workflow Tricks

  • Patch Mode: Allows you to review changes line-by-line before staging.
  • Snapshots: Each commit creates a permanent snapshot of the project.
3. Branching & Merging
git branchLists all local branches in the repository. git branch [name]Creates a new branch. git checkout [branch-name]Switches to a specified branch. git switch [branch-name]Modern alternative to switch branches. git checkout -b [name]Create and switch to a branch immediately. git merge [branch]Merges specified branch into the current one. git branch -d [name]Deletes a branch safely.

🌿 Branch Logic

  • Switching: git switch is the modern replacement for switching branches.
  • Safety: Safe mode (-d) prevents deleting work that hasn't been merged.
4. Remote Syncing
git remote -vLists all linked remote repositories. git remote add origin [url]Connects your local repo to a remote server. git fetchDownloads changes from remote without merging. git pullDownloads changes and immediately merges them. git push origin [branch]Uploads your commits to the remote branch.

☁️ Cloud Sync

  • Integration: git pull combines fetch and merge.
  • Upstream: Linking to "origin" is the industry standard for remote connections.
5. Inspection & History
git log --oneline --graphVisual history graph of all branches. git diffShows differences between working dir and staging. git diff --stagedShows differences between staging and last commit. git blame [file]Shows who modified each line and when. git show [id]Shows metadata and changes for a specific commit.

🔍 Analysis Notes

  • History: git log is your primary tool for tracking project evolution.
  • Comparison: git diff helps verify code before staging or committing.
6. Recovery & Emergency Undo
git stashTemporarily save uncommitted changes. git stash popRestore and remove the most recent stash. git reset --soft HEAD~1Undo commit but keep changes in staging. git reset --hard HEAD~1Undo commit and delete all changes permanently. git revert [id]Undo a commit by creating a new inverse commit. git restore [file]Discard local changes to a specific file.

⚠️ Danger Zone

  • Hard Reset: IRREVERSIBLE. Deletes changes from both staging and disk.
  • Stash: Use to quickly switch tasks without committing unfinished code.

Categories: : AWS, AWS Certified Cloud Practitioner, Certification, Cheatsheet, DevOps, GitHub, Version Control