Git Commands with Examples: Branch, Merge, Rebase, Stash and More
Everyday Git commands with examples: commit, diff, undo with restore, reset and revert, branches, merge conflicts, rebase, stash, tags, cherry-pick and squash.
ON THIS PAGE
Git commands are easier to remember when they are grouped by the problem they solve. This reference covers daily Git work by task, from committing and inspecting history to undoing changes, resolving conflicts, rebasing, stashing, tagging and squashing, with examples and screenshots from a small Docker project in a Vagrant VM. For why version control exists, the Git workflow and how to set up GitHub over HTTPS or SSH, read the first part: Git Introduction.
Basic commands
$ git clone <url>
$ git clone -b develop --single-branch https://github.com/dipen674/FDD_Website.git
$ git status
$ git add README.md
$ git add .
$ git commit -m "Add install steps to README"
$ git commit -am "Fix typo"
$ git commit --amend
$ git pull origin main
$ git push origin main| Command | What it does |
|---|---|
git clone <url> | Copies a remote repository to your machine |
git clone -b develop --single-branch <url> | Clones only one branch (here develop) |
git status | Shows changed, staged and untracked files |
git add <file> / git add . | Stages one file / all changes in the current folder |
git commit -m "msg" | Saves the staged changes as a commit |
git commit -am "msg" | Stages all tracked files and commits in one step (new files still need git add) |
git commit --amend | Changes the last commit's message, or adds forgotten staged changes to it |
git pull origin <branch> | Fetches and merges changes from the remote branch |
git push origin <branch> | Sends your commits to the remote branch |
Remotes
$ git remote -v
$ git remote add origin <url>
$ git remote set-url origin <new-url>
$ git remote remove originHistory and inspection
$ git log
$ git log --oneline --decorate --graph
$ git show <commit_id>
$ git ls-filesgit log --oneline --decorate --graphshows one line per commit, with branch names and a graph of merges.git show <commit_id>shows the message and the exact changes of one commit.git ls-fileslists the files Git is tracking.
Comparing changes with git diff
git diff shows what changed between two states: the working directory, the staging area, the last commit, other commits or a remote branch.
$ git diff
$ git diff --staged
$ git diff HEAD
$ git diff -- README.md
$ git diff <commit_id> HEAD
$ git diff HEAD~1 HEAD
$ git diff origin/main| Command | Compares |
|---|---|
git diff | Working directory with the staging area (unstaged changes) |
git diff --staged | Staging area with the last commit (--cached is the same option) |
git diff HEAD | Working directory with the last commit (staged and unstaged) |
git diff -- <path> | Limits any comparison to one file or path |
git diff <commit_id> HEAD | An older commit with the latest commit |
git diff HEAD~1 HEAD | The previous commit with the latest one (HEAD^ also means "one commit before HEAD") |
git diff origin/main | Your local branch with the remote main, as of the last fetch |
HEAD is a pointer to the commit you are on now. origin/HEAD points to the default branch of the remote.
Undoing changes: restore, reset and revert
The right command depends on where the change currently lives.
| Situation | Command |
|---|---|
| Discard edits in a file that is not staged | git restore <file> (older form: git checkout -- <file>) |
| Unstage a file but keep the edits | git restore --staged <file> (older form: git reset <file>) |
| Undo a commit that is already pushed | git revert <commit_id> |
| Throw away commits that are not pushed | git reset --hard <commit_id> |
git revert
git revert creates a new commit that reverses an earlier one. History is preserved, so it is safe on shared branches.
$ git revert HEAD
$ git revert <commit_id>Git opens an editor with a default revert message. Edit it if needed, then save and close the editor.
git reset
git reset moves the branch pointer back to an older commit. The mode determines what happens to the changes from the dropped commits:
| Mode | Commits after the target | Staging area | Working directory |
|---|---|---|---|
--soft | removed from history | changes stay staged | kept |
--mixed (default) | removed from history | cleared | kept |
--hard | removed from history | cleared | changes lost |
$ git reset --hard <old_commit_id>Branches
The Gitflow branch model (main, develop, feature, release, hotfix) is explained in Git Introduction. The commands below list, create, switch, rename and delete branches:
$ git branch
$ git branch -a
$ git switch develop
$ git switch -c feature/login
$ git branch -m old-name new-name
$ git branch -m new-name
$ git branch -d feature/login
$ git push origin --delete feature/login| Command | What it does |
|---|---|
git branch | Lists local branches; * marks the current one |
git branch -a | Lists local and remote branches |
git switch <branch> | Switches to an existing branch (older form: git checkout <branch>) |
git switch -c <branch> | Creates a branch and switches to it (older form: git checkout -b <branch>) |
git branch -m <old> <new> | Renames a branch from another branch |
git branch -m <new> | Renames the current branch |
git branch -d <branch> | Deletes a merged local branch (-D forces it) |
git push origin --delete <branch> | Deletes a branch on the remote |
Merging
git merge <branch> brings the commits of <branch> into the branch you are on.
If the current branch has no new commits of its own, Git performs a fast-forward merge: it moves the branch pointer forward and creates no merge commit. Example: develop has commits C and D on top of main:
A -- B (main)
\
C -- D (develop)
$ git switch main
$ git merge develop
A -- B -- C -- D (main, develop)With --no-ff (no fast-forward), Git always creates a merge commit, even when a fast-forward is possible:
$ git merge --no-ff developA -- B ------------ M (main)
\ /
C -- D ----- (develop)M is a merge commit that bundles the changes from develop, so the history records that these commits came from a separate branch. Many teams use --no-ff for exactly this reason.
Merge conflicts
A merge conflict happens when Git cannot combine two changes on its own. Common causes:
- Two branches change the same line of the same file.
- One branch edits a file while another branch deletes it.
In my lab, a conflict appeared when two branches changed the same LABEL line in a Dockerfile:

Git marks the conflict in the file:
- Between
<<<<<<< HEADand=======is the current change (the branch you are on). - Between
=======and>>>>>>> developis the incoming change (the branch you are merging).
To resolve the conflict, edit the file to the content you want and delete all three marker lines. VS Code also offers inline actions such as Accept Current Change and Accept Incoming Change.

Stage the file and commit to complete the merge:
$ git add Dockerfile
$ git commit -m "Resolve version label conflict"To return to the state before the merge, run git merge --abort. Merging frequently keeps conflicts small.
Rebase
git rebase moves your branch so that it starts from the tip of another branch. Git replays each of your commits on top of the new base, creating new commits. The result is a linear history without merge commits.
$ git switch feature/login
$ git rebase mainIf a conflict appears, resolve the file as you would for a merge conflict, stage it, and continue or abort:
$ git add <file>
$ git rebase --continue
$ git rebase --abort--continue proceeds to the next commit. --abort stops the rebase and restores the branch to its previous state.
Stash
git stash saves uncommitted changes and leaves a clean working directory. Use it to switch tasks or ship an urgent fix when the current work is not ready to commit.
$ git stash
$ git stash push -m "half-done compose changes"
$ git stash -u
$ git stash list
$ git stash show stash@{1}
$ git stash apply stash@{1}
$ git stash pop
$ git stash drop stash@{1}
$ git stash clear
$ git stash branch newchanges| Command | What it does |
|---|---|
git stash | Saves changes to tracked files and cleans the working directory |
git stash push -m "msg" | Same, with a message (git stash save "msg" is the older, deprecated form) |
git stash -u | Also stashes untracked files |
git stash list | Lists all stashes with index and message |
git stash show stash@{1} | Shows a summary of one stash (add -p for the full diff) |
git stash apply stash@{1} | Applies a stash and keeps it in the list |
git stash pop | Applies the latest stash and removes it from the list |
git stash drop [stash@{1}] | Deletes the latest stash, or the one given |
git stash clear | Deletes all stashes |
git stash branch <name> | Creates a branch from the commit where the stash was made, applies the stash, and drops it if that works |
The newest stash is always stash@{0}. When you stash again, older entries move down to stash@{1}, stash@{2} and so on. This stash@{n} form is called reflog syntax.

After git stash branch, the changes live on the new branch. Commit them there and merge the branch into main when ready.
Tags
A tag is a name that points to one commit, usually a release such as v1.0.1. Deployment tools often use tags to select the version to deploy.
Git has two kinds of tags:
- Lightweight tag: a name that points to a commit, with no extra metadata.
- Annotated tag: also stores the author, date and a message. Use these for releases.
$ git tag v1.0
$ git tag -a v1.1 -m "Release 1.1"
$ git tag -a v0.8-alpha <commit_id> -m "Alpha build"
$ git tag -a -f v0.8-alpha <commit_id>
$ git tag --list
$ git diff v1.0 v1.1
$ git tag --delete v1.0
$ git push origin v1.1
$ git push origin --tags
$ git push origin --delete v1.1| Command | What it does |
|---|---|
git tag <name> | Creates a lightweight tag on the current commit |
git tag -a <name> -m "msg" | Creates an annotated tag (-m alone also makes it annotated) |
git tag -a <name> <commit_id> | Tags an older commit |
git tag -a -f <name> <commit_id> | Moves an existing tag to another commit |
git tag --list | Lists all tags |
git diff <tag1> <tag2> | Shows the changes between two tags |
git tag --delete <name> | Deletes a local tag |
git push origin <name> | Pushes one tag (tags are not pushed by git push alone) |
git push origin --tags | Pushes all tags |
git push origin --delete <name> | Deletes a tag on the remote (use refs/tags/<name> if a branch has the same name) |
Cherry-pick
git cherry-pick copies the changes of one or more specific commits onto the current branch. Merge and rebase bring in every commit of the other branch; cherry-pick takes only the commits you name. A typical use is pulling a single fix from a teammate's branch.
$ git cherry-pick <commit_id>
$ git cherry-pick <commit_id_1> <commit_id_2>
$ git cherry-pick -n <commit_id>-n (--no-commit) applies the changes without committing them, so you can adjust them first.
Picking several commits can cause conflicts. Resolve the files as for a merge conflict, then continue, skip the commit, or abort:
$ git add .
$ git cherry-pick --continue
$ git cherry-pick --skip
$ git cherry-pick --abortSquash
Squashing combines several commits into one. It cleans up a feature branch full of small commits ("fix", "fix again") that clutter the history. The code changes stay the same; only the commits are combined.
Squash a whole branch into one commit while merging:
$ git switch main
$ git merge --squash feature/login
$ git commit -m "Add login feature"git merge --squash stages all the changes without committing, so the final git commit is required.
To squash the last few commits on your own branch, use an interactive rebase:
$ git rebase -i HEAD~3In the editor, keep pick on the first commit and change the others to squash (or s). As with any rebase, do this only on commits you have not shared.
Key takeaways
git restoreundoes local edits,git revertsafely undoes pushed commits, andgit reset --hardis only for unpushed work.- Fast-forward merges keep history linear;
--no-ffkeeps a merge commit that shows where a branch came in. - Resolve a conflict by editing the file, removing the markers, then
git addand commit (or--continueduring rebase and cherry-pick). - Rebase and interactive squash rewrite history: use them only on your own branch.
git stash popapplies and removes a stash;git stash applykeeps it.- Use annotated tags for releases, and push them with
git push origin <tag>or--tags.
Keep reading
- Git Introduction: Version Control, Setup and Branching
Why version control matters, how local, centralized and distributed VCS differ, how Git works, how to connect to GitHub over HTTPS or SSH, and Gitflow branching.
- tmux Cheat Sheet: Sessions, Windows, Panes and Copy Mode
A tmux reference for remote work: sessions that survive SSH drops, windows, split panes, copy mode, synchronized panes, and a minimal tmux.conf.
- kubectl Cheat Sheet by Category
The most-used kubectl commands, grouped by task: cluster info, namespaces, pods, Deployments, Services, ConfigMaps, Secrets, storage, contexts and debugging.