
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.
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.
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 mainCheck your settings with:
git config --listThe Git workflow you will use most
Check status
git statusShows changed, staged and untracked files.
See changes
git diffShows 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.
Git branches and merging
Branches let you work on changes without touching the main line until you are ready.
| Task | Command |
|---|---|
| List branches | git branch |
| Create branch | git branch feature-login |
| Switch branch | git switch feature-login |
| Create and switch | git switch -c feature-login |
| Merge branch into current branch | git merge feature-login |
| Delete merged branch | git branch -d feature-login |
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| Command | Use |
|---|---|
git remote -v | Show configured remotes. |
git fetch | Download remote updates without merging. |
git pull --ff-only | Update your branch only if it can fast-forward cleanly. |
git push origin branch-name | Push your branch to the remote. |
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.txtKeeps your file changes but removes them from the staging area.
Discard local file changes
git restore file.txtReverts a file to the last committed version. Use carefully.
Undo last commit, keep changes staged
git reset --soft HEAD~1Useful when the commit message was wrong or you forgot a file.
Revert a shared commit
git revert COMMIT_HASHCreates a new commit that reverses an old one. Safer for shared branches.
git revert for commits already pushed to a shared branch. Be careful with git reset --hard, as it discards work.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
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 commitgit 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 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
8f3a21c Update homepage layout
4b91d0e Add Git cheat sheet
c73a220 Fix quiz navigation
ad92e17 Add SSH guide images
91ac742 Initial site structuregit log --oneline is useful when you want a compact history view without the full commit details.
Compare file changes before committing
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
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
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
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
Saved working directory and index state On main: WIP before urgent fixstash@{0}: On main: WIP before urgent fixgit stash is useful when you need to switch branches but do not want to commit unfinished work.
Undo a staged file without deleting changes
Unstaged changes after reset:
M config.phpThis removes the file from the staging area but keeps the edits in your working directory.
Revert a shared commit safely
[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
| Step | Command | What it does |
|---|---|---|
| 1. Check state | git status | Shows branch, changed files and staged files. |
| 2. Review changes | git diff | Shows what changed before staging. |
| 3. Stage changes | git add file | Selects files for the next commit. |
| 4. Commit | git commit -m "Message" | Saves a snapshot of staged changes. |
| 5. Push | git push | Uploads commits to the remote repository. |
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.
Git quick reference
Use the Git cheat sheet for fast command lookup while working in the terminal.
Safer Git undo commands
| Goal | Command | Notes |
|---|---|---|
| Discard changes in one file | git restore file.txt | Removes uncommitted edits. |
| Unstage a file | git restore --staged file.txt | Keeps the file changes. |
| Undo a commit safely | git revert COMMIT | Creates a new commit that reverses the old one. |
| See recent commits | git log --oneline --decorate -n 10 | Useful before undoing anything. |
Use git revert on shared branches. Be much more careful with reset, especially if commits have already been pushed.
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.