Git command guide banner showing common Git commands
Version control guide

Git Command Guide: Basics, Branches, Merges and Safe Undo Commands

Git is the version control tool used by developers, sysadmins and support teams to track changes, work safely on branches and roll back mistakes. This guide explains the Git commands you actually need for everyday work.

On this page
Quick answer

What is Git?

Git is a distributed version control system. It records snapshots of your project so you can see what changed, who changed it, when it changed and why. It is useful for code, configuration files, documentation and any project where you need reliable change history.

Simple explanation: Git is like a save system for projects, but with branches, history, teamwork and controlled rollback built in.
First time setup

Configure Git before your first commit

Set your name and email once. Git stores these details with commits so the history is readable.

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

Check your settings with:

git config --list
Daily workflow

The Git workflow you will use most

Check status

git status

Shows changed, staged and untracked files.

See changes

git diff

Shows unstaged changes before you add them.

Stage files

git add file.txt
git add .

Add one file or stage all current changes.

Commit changes

git commit -m "Explain the change"

Create a snapshot with a useful message.

Branches

Git branches and merging

Branches let you work on changes without touching the main line until you are ready.

TaskCommand
List branchesgit branch
Create branchgit branch feature-login
Switch branchgit switch feature-login
Create and switchgit switch -c feature-login
Merge branch into current branchgit merge feature-login
Delete merged branchgit branch -d feature-login
Remote repos

Clone, pull and push

Remote repositories are hosted copies of your project, commonly on GitHub, GitLab, Bitbucket or an internal Git server.

git clone https://example.com/project.git
cd project
git pull --ff-only
git push origin main
CommandUse
git remote -vShow configured remotes.
git fetchDownload remote updates without merging.
git pull --ff-onlyUpdate your branch only if it can fast-forward cleanly.
git push origin branch-namePush your branch to the remote.
Safe fixes

Safe Git undo commands

Undo commands can be dangerous when used casually. Start with the safest commands before reaching for destructive resets.

Unstage a file

git restore --staged file.txt

Keeps your file changes but removes them from the staging area.

Discard local file changes

git restore file.txt

Reverts a file to the last committed version. Use carefully.

Undo last commit, keep changes staged

git reset --soft HEAD~1

Useful when the commit message was wrong or you forgot a file.

Revert a shared commit

git revert COMMIT_HASH

Creates a new commit that reverses an old one. Safer for shared branches.

Rule of thumb: use git revert for commits already pushed to a shared branch. Be careful with git reset --hard, as it discards work.
Practical examples

Git examples with output

These examples show the command and the kind of output you might see. The exact filenames, commit hashes and branch names will differ on your own machine.

Check what has changed with git status

$ git status
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  modified:   index.html
  modified:   styles.css

Untracked files:
  notes.txt

no changes added to commit

git status is usually the first command to run. It shows your current branch, changed files, staged files and untracked files.

Stage and commit a file

$ git add index.html styles.css
$ git commit -m "Update homepage layout"
[main 8f3a21c] Update homepage layout
 2 files changed, 34 insertions(+), 8 deletions(-)

git add selects changes for the next commit. git commit saves those staged changes into the project history.

View recent commit history

$ git log --oneline -5
8f3a21c Update homepage layout
4b91d0e Add Git cheat sheet
c73a220 Fix quiz navigation
ad92e17 Add SSH guide images
91ac742 Initial site structure

git log --oneline is useful when you want a compact history view without the full commit details.

Compare file changes before committing

$ git diff styles.css
diff --git a/styles.css b/styles.css
@@ -22,7 +22,7 @@
-.card { padding: 12px; }
+.card { padding: 18px; }

-.card h3 { font-size: 16px; }
+.card h3 { font-size: 18px; }

git diff shows unstaged changes. It is one of the safest ways to check exactly what you are about to stage or commit.

Create and switch to a new branch

$ git switch -c fix-search-layout
Switched to a new branch 'fix-search-layout'

Branches let you work on changes without touching the main branch until you are ready to merge.

Push a new branch to the remote

$ git push -u origin fix-search-layout
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Writing objects: 100% (7/7), 1.42 KiB | 1.42 MiB/s, done.
To github.com:example/project.git
 * [new branch]      fix-search-layout -> fix-search-layout
branch 'fix-search-layout' set up to track 'origin/fix-search-layout'.

The -u option sets upstream tracking, so future git push and git pull commands know which remote branch to use.

Pull safely with fast-forward only

$ git pull --ff-only
Updating 4b91d0e..8f3a21c
Fast-forward
 index.html | 12 +++++++++---
 styles.css |  8 +++++---
 2 files changed, 14 insertions(+), 6 deletions(-)

--ff-only avoids surprise merge commits. It only updates your branch if Git can move it forward cleanly.

Temporarily save unfinished work with stash

$ git stash push -m "WIP before urgent fix"
Saved working directory and index state On main: WIP before urgent fix
$ git stash list
stash@{0}: On main: WIP before urgent fix

git stash is useful when you need to switch branches but do not want to commit unfinished work.

Undo a staged file without deleting changes

$ git restore --staged config.php
Unstaged changes after reset:
M       config.php

This removes the file from the staging area but keeps the edits in your working directory.

Revert a shared commit safely

$ git revert 8f3a21c
[main 2d10abc] Revert "Update homepage layout"
 2 files changed, 8 insertions(+), 34 deletions(-)

git revert creates a new commit that reverses an older commit. It is safer than rewriting history on a shared branch.

Common Git workflow chart

StepCommandWhat it does
1. Check stategit statusShows branch, changed files and staged files.
2. Review changesgit diffShows what changed before staging.
3. Stage changesgit add fileSelects files for the next commit.
4. Commitgit commit -m "Message"Saves a snapshot of staged changes.
5. Pushgit pushUploads commits to the remote repository.
FAQ

Git FAQ

What is the difference between Git and GitHub?

Git is the version control tool. GitHub is a hosting platform for Git repositories. You can use Git without GitHub.

What does git add do?

git add stages changes. Staging means choosing what will go into the next commit.

What does git commit do?

git commit records a snapshot of staged changes with a message.

Should I use git pull or git fetch?

git fetch downloads remote changes without merging. git pull fetches and then integrates changes into your branch.

Related

Git quick reference

Use the Git cheat sheet for fast command lookup while working in the terminal.

Safe undo

Safer Git undo commands

GoalCommandNotes
Discard changes in one filegit restore file.txtRemoves uncommitted edits.
Unstage a filegit restore --staged file.txtKeeps the file changes.
Undo a commit safelygit revert COMMITCreates a new commit that reverses the old one.
See recent commitsgit log --oneline --decorate -n 10Useful before undoing anything.

Use git revert on shared branches. Be much more careful with reset, especially if commits have already been pushed.

FAQ

Frequently Asked Questions

What is the difference between git add and git commit?

git add stages changes, while git commit records the staged changes in repository history.

How do I see changed files in Git?

Use git status for a summary and git diff for detailed changes.

How do I undo a commit safely?

Use git revert COMMIT to create a new commit that reverses the selected commit.

Should I use git reset on shared branches?

Avoid rewriting shared history unless you know exactly what you are doing and your team agrees.

$ practise_next --topic linux

Practise this next

Turn the guide into practice with a related quiz, builder, cheat sheet or learning path.