Git Commands Cheat Sheet
Essential Git commands for modern version control. Includes examples, syntax, and tips for committing, branching, and merging.
git clone
Downloads a copy of an existing remote repository to your local machine.
Syntax
git clone <url>Example
git clone https://github.com/user/project.gitPro Tip
You can append a folder name to clone it directly into a specific folder: `git clone <url> new-folder`
Common Mistake
Running this command inside an existing git repository folder. Always clone into an empty directory.
git status
Shows the state of your working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git.
Syntax
git statusExample
git statusPro Tip
Run this command constantly! It is the best way to avoid accidentally committing the wrong files.
Common Mistake
Ignoring untracked files in the output.
git add
Adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit.
Syntax
git add <file-name>
git add .Example
git add index.html
git add .Pro Tip
Use `git add -p` to stage changes in chunks (hunks) interactively.
Common Mistake
Using `git add .` without running `git status` first, accidentally committing secret `.env` files.
git commit
Captures a snapshot of the project's currently staged changes.
Syntax
git commit -m "message"Example
git commit -m "Fix navigation bar bug on mobile"Pro Tip
Use `git commit -am "message"` to skip the `git add` step for files that are already being tracked.
Common Mistake
Writing bad commit messages like 'fixed bug' or 'update'.
git push
Uploads local repository content to a remote repository.
Syntax
git push <remote> <branch>Example
git push origin mainPro Tip
Use `git push -u origin feature-branch` the very first time you push a new branch to set the upstream link.
Common Mistake
Pushing directly to the `main` branch instead of creating a Pull Request on a feature branch.
git pull
Fetches and downloads content from a remote repository and immediately updates the local repository to match.
Syntax
git pull <remote> <branch>Example
git pull origin mainPro Tip
`git pull` is actually a combination of two commands: `git fetch` followed by `git merge`.
Common Mistake
Running `git pull` when you have uncommitted changes. Git might abort the pull or create messy merge conflicts.
git branch
Lists, creates, or deletes branches.
Syntax
git branch
git branch <branch-name>Example
git branch feature-login
git branch -d feature-loginPro Tip
Run `git branch -a` to see both local and remote branches.
Common Mistake
Creating a branch and thinking you are instantly switched to it. (You must use `checkout`).
git checkout
Switches branches or restores working tree files.
Syntax
git checkout <branch-name>
git checkout -b <new-branch-name>Example
git checkout main
git checkout -b feature-loginPro Tip
In newer versions of Git, you can use `git switch` instead of `checkout` to change branches.
Common Mistake
Forgetting the `-b` flag when trying to create a *new* branch, resulting in a 'pathspec did not match any file' error.