Complete Guide 4 min read

Essential Git Commands Every Developer Should Know

The most important Git commands for daily development. Branch, merge, stash, reset, and common workflows.

git commandsgit cheat sheetgit tutorialgit basicsgit workflow

Core Git Workflow Commands

git init: Initialise a new Git repository in the current directory.

git clone [url]: Download a repository to your local machine.

git add [file] or git add .: Stage changes for the next commit. . stages all changed files.

git commit -m "message": Create a commit with staged changes.

git push origin [branch]: Upload local commits to remote repository.

git pull: Fetch and merge changes from the remote into current branch.

Branching

git branch: List all local branches.

git branch [name]: Create a new branch.

git checkout [branch] or git switch [branch]: Switch to a branch.

git checkout -b [name]: Create and switch to a new branch in one command.

git merge [branch]: Merge a branch into the current branch.

git branch -d [branch]: Delete a branch after merging.

Undoing Changes

git restore [file]: Discard unstaged changes in a file.

git reset HEAD [file]: Unstage a file (keep changes in working directory).

git reset --soft HEAD~1: Undo last commit, keep changes staged.

git reset --hard HEAD~1: Undo last commit, discard all changes. IRREVERSIBLE.

git revert [commit]: Create a new commit that undoes a previous commit. Safer than reset for shared branches.

Stashing Work in Progress

git stash: Save current uncommitted changes and restore clean working directory.

git stash pop: Apply the most recent stash and remove it.

git stash list: List all stashes.

git stash drop: Delete the most recent stash without applying.

Viewing History

git log --oneline: Compact commit history.

git log --graph --oneline --decorate: Visual branch history.

git diff: Show unstaged changes.

git diff --staged: Show staged changes.

git blame [file]: Show who last changed each line of a file.

Frequently asked questions

What is the difference between git merge and git rebase?

Merge creates a new merge commit that combines two branches, preserving full history. Rebase rewrites commits from one branch onto another, creating a linear history. Merge is safer for shared branches; rebase is cleaner for local development.

How do I undo a git commit?

git reset --soft HEAD~1 undoes the last commit but keeps changes staged. git reset --hard HEAD~1 undoes the commit and discards all changes. Use git revert for commits already pushed to shared branches.

Try this tool on Lazyblink

Put this guide into practice with our free online tool — no signup required.

Open tool