Tools Games AI
[ Ad Placement: Top Article Banner ]

Git Commands Master Sheet

The Absolute Essentials

Whether you are a solo developer or working in a large team, these are the fundamental Git commands you will use every single day. Memorizing these will save you hours of debugging.

  • git init - Initializes a brand new Git repository in your current directory. It creates a hidden .git folder where all your version history is stored.
  • git clone <url> - Downloads a complete copy of an existing repository from a remote server (like GitHub or GitLab) to your local machine, automatically setting up the remote tracking.
  • git status - Shows the current state of your working directory and staging area. Always run this before committing to ensure you aren't accidentally staging unwanted files like .env.
  • git add . - Stages all modified, deleted, and new files in the current directory. You can also use git add <file> to stage specific files.
  • git commit -m "Your descriptive message" - Takes a snapshot of your staged changes. A good commit message uses the imperative mood (e.g., "Add login button" instead of "Added login button").
  • git push origin main - Uploads your local branch commits to the remote repository. Replace main with your branch name.

Branching & Merging Strategies

Branching allows you to work on new features or bug fixes in an isolated environment without breaking the stable production codebase.

  • git checkout -b feature-name - Creates a new branch named 'feature-name' and immediately switches your working directory to it.
  • git branch - Lists all local branches. The active branch will have an asterisk (*) next to it. Add -a to see remote branches as well.
  • git checkout main - Switches your working directory back to the 'main' branch.
  • git merge feature-name - Merges the changes from 'feature-name' into your current active branch. If there are conflicts, Git will pause and ask you to resolve them manually.
  • git rebase main - Applies your current branch's commits on top of the latest commits from the main branch. This creates a cleaner, linear history but should never be used on public, shared branches.

Advanced Rescues & Undo Techniques

Made a massive mistake? Don't panic. Git is a time machine, and you can almost always recover your work if you haven't deleted the .git folder.

  • git stash - Temporarily shelves your uncommitted changes so you can work on a different branch. Use git stash pop to retrieve them later.
  • git restore --staged <file> - Removes a file from the staging area without losing your modifications.
  • git reset --soft HEAD~1 - Undoes your last commit, but keeps all your changes staged. Perfect for when you forgot to include a file or made a typo in the commit message.
  • git reset --hard HEAD~1 - WARNING: Completely undoes your last commit and permanently deletes the uncommitted changes. Use with extreme caution!
  • git reflog - The ultimate safety net. It shows a log of every action you've taken (even deleted commits or hard resets). You can use the hashes found here to restore lost work.
  • git cherry-pick <commit-hash> - Applies the changes from a specific commit (even from a completely different branch) onto your current branch.
[ Ad Placement: Bottom Article Banner ]