If you've ever forgotten a Git command, accidentally committed the wrong file, or stared at a merge conflict wondering what went wrong, welcome to the club. ๐Ÿ’€

Git is one of those tools every developer uses, but nobody wants to memorize every command. The good news? You don't have to.

This cheat sheet covers the Git commands you'll actually need while building projects, collaborating with friends, contributing to open source, or managing your code on GitHub.

No complicated explanations. Just commands, practical examples, and a few warnings to save you from unnecessary pain.

First, what exactly are Git and GitHub?

They're related, but they're not the same thing.

  • Git: A version control system that tracks changes in your code.

  • GitHub: An online platform where you can host Git repositories and collaborate with other developers.

Think of Git as your project's history tracker and GitHub as a place to store and share that history online.

1. Install and configure Git

Before using Git, make sure it's installed on your machine.

bash
git --version

Displays your installed Git version.

Set your name and email. These details are recorded in your commits.

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Check your configuration:

bash
git config --list

Get help whenever you forget a command:

bash
git help commit
git commit -h

The first opens detailed documentation, while the second displays a shorter help page.

2. Create or download a repository

A repository, or repo, is a project Git tracks, including its history.

Start tracking an existing project:

bash
git init

Initializes Git in your current directory.

Clone an existing project:

bash
git clone https://github.com/username/project.git

Downloads a repository and its available history to your machine.

Clone into a custom folder:

bash
git clone https://github.com/username/project.git my-project

Check the repository status:

bash
git status

This is one of the most useful Git commands. It shows your current branch, modified files, staged changes, and other important information.

Pro tip: When you're unsure what state your project is in, run git status first.

3. The everyday Git workflow

Most of your daily Git work follows this simple pattern:

<CodeBlock language="mermaid" editable={false}>flowchart TD
A["Edit your code"] --> B["git status"]
B --> C["git add"]
C --> D["git diff --staged"]
D --> E["git commit"]
E --> F["git push"]
F --> G["Changes uploaded"]
</CodeBlock>

Here's what each step means:

Step

Command

Purpose

Check

git status

See what changed

Stage

git add

Select changes for the next commit

Review

git diff --staged

Inspect selected changes

Commit

git commit

Save a snapshot in local history

Push

git push

Upload commits to a remote repository

Remember: a commit saves your changes locally. A push sends those commits to a remote repository.

Stage your changes

Add one file:

bash
git add app.py

Add several files:

bash
git add app.py README.md

Stage all changes in the current directory:

bash
git add .

Interactively choose which parts of a file to stage:

bash
git add -p

The interactive option is useful when one file contains several unrelated changes and you want to commit only some of them.

Commit your changes

Create a commit with a descriptive message:

bash
git commit -m "Add login validation"

View the changes selected for your next commit:

bash
git diff --staged

Stage and commit changes to already tracked files in one step:

bash
git commit -am "Fix navigation bug"

Important: git commit -am does not automatically include newly created, untracked files. Use git add for those first.

Good commit messages describe what changed:

  • Add password validation

  • Fix mobile navigation

  • Update project documentation

Avoid messages like stuff, final final, or it works now. Future you deserves better. ๐Ÿ˜ญ

4. Check changes before committing

These commands help you understand exactly what you're about to save.

bash
git status

Shows the current state of your files.

bash
git diff

Displays changes that haven't been staged.

bash
git diff --staged

Displays changes that are staged for the next commit.

bash
git diff HEAD

Shows differences between your working tree and the current commit, including staged and unstaged tracked-file changes.

bash
git diff --stat

Shows a summary of changed files and the size of their changes.

bash
git diff -- path/to/file

Displays unstaged changes in a particular file.

5. View your project history

Git records commits so you can understand how your project evolved.

View commit history:

bash
git log

Show a compact history:

bash
git log --oneline

Show branches and merges visually:

bash
git log --oneline --graph --decorate --all

Inspect a particular commit:

bash
git show <commit-id>

Replace <commit-id> with an actual commit hash.

See who last changed each line of a file:

bash
git blame app.py

Useful for tracing the history of a particular line, though it doesn't necessarily tell you why the change was made.

6. Work with branches

Branches let you develop features or fix bugs separately from your main line of development.

Imagine you're building a website. Instead of experimenting directly on main, you create a separate branch for the feature.

List local branches:

bash
git branch

List local and remote-tracking branches:

bash
git branch -a

Create a new branch:

bash
git branch feature/login

This creates the branch but doesn't switch to it.

Create a branch and switch to it:

bash
git switch -c feature/login

Switch to an existing branch:

bash
git switch main

Rename the current branch:

bash
git branch -m new-name

Delete a merged local branch:

bash
git branch -d feature/login

Force-delete a local branch:

bash
git branch -D feature/login

Be careful with -D. It can delete a branch even when Git warns that it hasn't been merged.

Merge branches

First, switch to the branch that should receive the changes.

bash
git switch main
git merge feature/login

Git combines the changes from feature/login into main, potentially creating a merge commit.

If conflicts occur, resolve them in the affected files, stage the resolutions, and complete the merge as instructed by Git.

7. Connect Git to GitHub

A remote is a named reference to another repository, often one hosted on GitHub.

List configured remotes:

bash
git remote -v

Connect your local repository to GitHub:

bash
git remote add origin https://github.com/username/project.git

Replace the URL with your repository's actual URL.

Change the URL of an existing remote:

bash
git remote set-url origin https://github.com/username/project.git

Download remote updates without merging them into your current branch:

bash
git fetch origin

Fetch and integrate changes from the remote branch:

bash
git pull origin main

Upload your current branch to GitHub:

bash
git push

This works when your branch already has an upstream configured.

Push a new branch and configure its upstream:

bash
git push -u origin feature/login

After setting the upstream, you can generally use git push and git pull without specifying the remote and branch every time.

Fetch vs. pull vs. push

Command

What it does

git fetch

Downloads remote updates without integrating them into your current branch

git pull

Fetches updates and integrates them into your current branch

git push

Uploads your local commits to a remote repository

If someone else has updated your project, git fetch is a useful first step when you want to inspect those changes before integrating them.

8. Undo mistakes without panicking

This is where Git becomes especially useful. But some commands can permanently discard work, so choose carefully.

Unstage a file

Accidentally added a file to staging?

bash
git restore --staged app.py

This removes it from staging but keeps your changes in the file.

Discard unstaged changes in a file

bash
git restore app.py

Warning: This discards unstaged changes to the file. Make sure you don't need them.

Undo a commit but keep your changes

bash
git reset --soft HEAD~1

Moves the current branch back one commit and keeps the changes staged.

To undo the commit while keeping changes in your working tree, unstaged:

bash
git reset --mixed HEAD~1

--mixed is the default reset mode.

Undo a commit by creating a new reversing commit

bash
git revert <commit-id>

This creates a new commit that reverses the selected commit's changes. It's generally safer than rewriting history that other people may already have pulled.

Discard local changes and reset tracked files

bash
git reset --hard HEAD

Danger: This discards staged and unstaged changes to tracked files. Uncommitted work can be lost.

It does not remove untracked files. Avoid running destructive commands unless you understand what will be discarded.

Remove untracked files

Preview what would be removed:

bash
git clean -n

Actually remove untracked files:

bash
git clean -f

Use these carefully. They can delete files Git isn't tracking, including work you haven't committed.

9. Temporarily save unfinished work with stash

Imagine you're halfway through a feature and need to switch branches to fix an urgent bug. You aren't ready to commit your current changes.

That's what git stash is for.

Save your changes:

bash
git stash push -m "Work in progress"

List saved stashes:

bash
git stash list

Apply the latest stash and keep it saved:

bash
git stash apply

Apply the latest stash and remove it from the stash list:

bash
git stash pop

Apply a specific stash:

bash
git stash apply stash@{0}

Delete a particular stash:

bash
git stash drop stash@{0}

Delete all saved stashes:

bash
git stash clear

Be cautious with git stash clear, since it removes every stash entry.

By default, stash primarily saves tracked-file changes. To include untracked files, use:

bash
git stash push -u -m "Include new files"

10. Fix common Git problems

Problem: Your local branch and remote branch have diverged

You may see a message saying that the branches have diverged and you need to specify how to reconcile them.

First, check your current state:

bash
git status
git fetch origin

Then choose an integration strategy appropriate for your project.

Merge remote changes:

bash
git pull --no-rebase

Rebase your local commits on top of the remote branch:

bash
git pull --rebase

If you have uncommitted work, commit or stash it first. If a conflict occurs during a rebase, resolve the files, stage the resolutions, and continue with:

bash
git rebase --continue

To cancel an in-progress rebase:

bash
git rebase --abort

Problem: You committed to the wrong branch

If you haven't pushed yet, you can often move the commit to the correct branch. The safest exact steps depend on whether the changes are already on the wrong branch and whether that branch has been shared.

Before resetting anything, inspect the recent history:

bash
git log --oneline -5
git status

Avoid blindly resetting or deleting branches when the commit contains work you need to keep.

Problem: Git says there are merge conflicts

Check which files need attention:

bash
git status

Open each conflicted file and look for markers such as:

bash
<<<<<<< HEAD
Your current changes
=======
Incoming changes
>>>>>>> feature/login

Edit the file so it contains the correct final version, remove the conflict markers, and stage the resolved file:

bash
git add path/to/file

Then finish the merge or rebase according to the operation you're performing.

Problem: You committed a file you shouldn't have

If the commit hasn't been pushed and it's your most recent commit, you can amend it after removing the unwanted file from staging or tracking, depending on the situation.

For example, to stop tracking a file while keeping it on your machine:

bash
git rm --cached .env

Add the file to .gitignore so Git stops picking it up in future changes.

Important: If .env contained passwords, API keys, or tokens and was pushed to a remote repository, removing it from a later commit is not enough. Revoke or rotate the exposed secrets immediately.

11. Use .gitignore to keep unwanted files out

A .gitignore file tells Git which untracked files or directories to ignore.

Create a file named .gitignore in your project root:

bash
# Dependencies
node_modules/

# Environment variables
.env
.env.*

# Build output
dist/
build/

# Python cache
__pycache__/
*.py[cod]

# Logs
*.log

# Editor settings
.vscode/

Adjust these patterns for your project. For example, some teams intentionally commit selected .vscode settings or environment templates.

Check whether Git is ignoring a particular path:

bash
git check-ignore -v .env

One important detail: .gitignore doesn't automatically stop tracking files that Git already tracks. If a file is already tracked, you must untrack it separately.

12. More useful commands worth knowing

Command

Purpose

git mv old.txt new.txt

Rename or move a tracked file

git rm file.txt

Remove a file and stage its deletion

git rm --cached file.txt

Stop tracking a file but keep it locally

git show HEAD

Inspect the latest commit

git log -5

Show the five most recent commits

git branch -vv

Show local branches and upstream information

git fetch --prune

Remove stale remote-tracking branch references

git tag

List tags

git tag v1.0.0

Create a lightweight tag

git push origin --tags

Push tags to the remote

git cherry-pick <commit-id>

Apply a commit onto your current branch

git rebase main

Replay current branch commits on top of main

git reflog

Inspect recent movements of local branch references

git remote show origin

Display information about a remote

git shortlog -sn

Summarize commit counts by author

Some commands, such as rebase and cherry-pick, can cause conflicts or require history cleanup. Learn how they work before using them on shared branches.

13. A practical workflow for your next project

Here's a simple example of starting a project and publishing your first feature branch.

Step 1: Clone the repository.

bash
git clone https://github.com/username/my-project.git
cd my-project

Step 2: Create a feature branch.

bash
git switch -c feature/profile-page

Step 3: Make your changes, then inspect them.

bash
git status
git diff

Step 4: Stage and review your changes.

bash
git add .
git diff --staged

Step 5: Commit the feature.

bash
git commit -m "Add profile page"

Step 6: Push the branch.

bash
git push -u origin feature/profile-page

Step 7: Open a pull request on GitHub.

A pull request lets you propose changes, review code, discuss improvements, and merge the feature into the target branch.

This workflow keeps your work separate from main and makes collaboration easier.

14. Quick reference: The commands to memorize first

If you're just getting started, focus on these before trying to learn everything else.

bash
git status
git add .
git commit -m "Your message"
git log --oneline
git switch -c feature/name
git switch main
git merge feature/name
git pull
git push
git stash
git restore --staged file.txt
git diff

These cover most of the basic tasks you'll encounter when building and maintaining projects.

Final thoughts

You don't need to memorize 50 commands to become comfortable with Git. Start with the everyday workflow, understand what staging and commits actually mean, and gradually learn branching, collaboration, and recovery commands as you need them.

And when something goes wrong? Don't immediately reach for git reset --hard. Run git status, inspect your history, and understand what Git is telling you before making things worse. ๐Ÿ˜ญ

Bookmark this cheat sheet for the next time you forget a command.

Further reading

Happy coding, and may your merge conflicts be few. ๐Ÿš€