git cheatsheetgit commandsversion controlgithubdeveloper reference

Git Cheatsheet: Essential Commands Every Developer Should Know

A practical Git command reference covering setup, branching, merging, rebasing, undoing changes, and collaboration workflows with GitHub and GitLab.

8 min read

Related Tool

Git Commands Cheatsheet

Open tool

Git is the most widely used version control system in the world. Whether you work alone or in a team, understanding Git commands is essential. This cheatsheet covers the commands you will use daily, along with less common but critical commands for resolving problems.

Initial Setup

Before using Git, configure your identity. These values appear in every commit you make.

git config --global user.name "Your Name" sets your display name.

git config --global user.email "you@example.com" sets your email.

git config --global core.editor "code --wait" sets VS Code as your default editor (adjust for your preferred editor).

Starting a Repository

git init creates a new Git repository in the current directory.

git clone url downloads an existing repository to your local machine.

Staging and Committing

Git uses a staging area (the index) where you assemble changes before committing.

git status shows which files are modified, staged, or untracked.

git add filename stages a specific file.

git add . stages all changes in the current directory.

git commit -m "message" creates a commit with the staged changes.

git commit --amend modifies the most recent commit (use only on commits not yet pushed).

Viewing History

git log shows the commit history with full details.

git log --oneline shows a compact one-line-per-commit view.

git log --graph --oneline shows a branch and merge graph.

git diff shows unstaged changes.

git diff --staged shows staged changes not yet committed.

git show commitHash shows the details and diff of a specific commit.

Branching

git branch lists local branches.

git branch branchname creates a new branch.

git checkout branchname switches to a branch (older syntax).

git switch branchname switches to a branch (newer syntax).

git switch -c branchname creates and switches to a new branch.

git branch -d branchname deletes a merged branch.

git branch -D branchname force deletes a branch regardless of merge status.

Merging and Rebasing

git merge branchname merges the specified branch into the current branch.

git merge --no-ff branchname creates a merge commit even for fast-forward merges.

git rebase branchname replays your commits on top of another branch.

git rebase --interactive HEAD~3 opens an editor to reorder, squash, or edit the last 3 commits.

Rebase rewrites history and should not be used on commits that have been pushed and shared. Merge preserves history and is safe on shared branches.

Remote Repositories

git remote -v lists remote connections.

git remote add origin url adds a remote named "origin".

git fetch downloads changes from remote without merging.

git pull downloads and merges changes from remote.

git push origin branchname uploads the branch to remote.

git push --set-upstream origin branchname sets the tracking remote on first push.

Undoing Changes

git restore filename discards unstaged changes to a file.

git restore --staged filename unstages a file without discarding changes.

git reset HEAD~1 moves the branch pointer back one commit, keeping changes staged.

git reset --hard HEAD~1 moves the branch pointer back and discards changes permanently.

git revert commitHash creates a new commit that undoes a previous commit (safe for shared history).

Stashing

git stash saves current changes without committing so you can switch context.

git stash pop restores the most recent stash.

git stash list shows all saved stashes.

git stash drop stash@{n} removes a specific stash.

Tagging

git tag v1.0 creates a lightweight tag on the current commit.

git tag -a v1.0 -m "Version 1.0" creates an annotated tag with a message.

git push origin v1.0 pushes a tag to remote.

git push origin --tags pushes all tags.

Useful Aliases

Git supports command aliases. For example: git config --global alias.lg "log --oneline --graph --all" creates a command "git lg" that runs the full log command.

Using the DevHexLab Git Cheatsheet

Open the reference at /tools/reference/git-cheatsheet. Browse commands by category, copy the exact syntax, and use it alongside the terminal when you need a quick lookup.