gitcheatsheetversion controlcommands

Git Cheatsheet: The Essential Commands (With Examples)

Git cheatsheet with the most-used commands: clone, branches, commits, merge, undo changes, stash and resolving conflicts. A quick reference for developers.

June 22, 2026·8 min read

Git is the version-control tool used by practically all software development. It has tons of commands, but day to day you repeat a handful. This cheatsheet gathers the essentials with examples, organized by task, to keep them always at hand.

Initial setup

git config --global user.name "Your Name"
git config --global user.email "you@email.com"
git config --global init.defaultBranch main

Set your identity (it appears in every commit) and the default branch.

Start a repository

git init                      # Create a new repo in the current folder
git clone <url>               # Clone a remote repo
git clone <url> my-folder     # Clone into a specific folder

The basic flow: status, add, commit

git status                    # What changed
git add file.js               # Stage a file for the commit
git add .                     # Stage ALL changes
git commit -m "message"       # Create the commit with what's staged
git commit -am "message"      # add + commit of already-tracked files

The mental cycle: modify → add (staging) → commit (save).

Branches

git branch                    # List branches
git branch new-branch         # Create a branch
git checkout new-branch       # Switch to that branch
git checkout -b new-branch    # Create AND switch in one step
git switch new-branch         # Modern alternative to checkout
git branch -d branch          # Delete an already-merged branch
git branch -D branch          # Force-delete a branch

Working in branches separates each task or feature from the stable code.

Sync with the remote

git pull                      # Fetch and merge remote changes
git push                      # Push your commits to the remote
git push -u origin my-branch  # Push and link the branch the first time
git fetch                     # Fetch changes WITHOUT merging
git remote -v                 # Show configured remotes

Merging changes

git merge other-branch        # Merge other-branch into the current one
git merge --abort             # Cancel a merge with conflicts

When there's a conflict, Git marks the disputed areas with <<<<<<<, ======= and >>>>>>>. Edit the file keeping the correct version, remove those markers, and run git add + git commit.

Undo changes (the most searched)

git restore file.js           # Discard unsaved changes to a file
git restore --staged file     # Unstage a file (without losing it)
git reset --soft HEAD~1       # Undo the last commit, KEEP the changes
git reset --hard HEAD~1       # Undo the last commit and DELETE the changes
git revert <hash>             # Create a commit that undoes another (safe on remote)

Golden rule: reset --hard deletes work; use it carefully. On shared branches use revert, which doesn't rewrite history.

Stash changes temporarily

git stash                     # Save your changes and clean the working tree
git stash list                # List what's stashed
git stash pop                 # Restore the latest stash and remove it
git stash apply               # Restore without removing it from the stash

Useful when you need to switch branches but don't want to commit yet.

View history and differences

git log --oneline             # Compact history, one line per commit
git log --graph --oneline     # With a branch graph
git diff                      # Unstaged changes
git diff --staged             # Staged changes
git show <hash>               # Detail of a specific commit

To compare two texts or versions outside Git, you can use the diff checker, which highlights the differences in your browser.

Commands that save the day

git reflog                    # History of EVERYTHING you did (recover "lost" commits)
git cherry-pick <hash>        # Apply a specific commit from another branch
git commit --amend            # Fix the last commit (message or content)
git clean -fd                 # Delete untracked files (careful!)

git reflog is the panic button: even if you think you lost a commit with a reset, it's almost always still there and you can recover it.

Best practices

  • Small, frequent commits with clear messages: each commit should tell "what" and "why".
  • One branch per task: keep main always stable.
  • Pull before you start so you don't work on old code.
  • Don't rewrite history (rebase, reset --hard, push --force) on shared branches.

Conclusion

You don't need to memorize Git's hundreds of commands: with this handful you cover 95% of daily work. Save this cheatsheet, and when you're unsure about a merge or want to undo something, come back to it. And remember: when something goes wrong, git reflog usually has the answer.

Try it without code

Diff Checker

Line-by-line text differences.

Open Diff Checker

Built by

Miguel Ángel Colorado Marin (MACM)

Full-Stack Developer · Guadalajara, España

I develop web apps, digital tools and full projects — from design to deployment.

Contact me