Git-Distributed Version Control System part-3
Git-Distributed Version Control System part-3
Continue learning Git-Distributed Version Control System part-2 in this advanced guide. Explore branching, merging, and squashing with real-world Git workflow examples.
Git Reset Command
git reset Command Explained (with examples)
The git reset command is used to undo changes in your Git repository. It moves the current HEAD (your current branch tip) to a specified state. You can use it to:
-
Unstage files (remove from staging area)
-
Move commits (undo commits)
-
Change working directory contents (optional)

Syntax:
cssgit reset [mode] [commit]
Modes of git reset:
| Mode | Staging Area | Working Directory | Use Case |
|---|---|---|---|
--soft |
Keeps | Keeps | Undo commits but keep staged changes |
--mixed |
Resets | Keeps | Default. Undo commits + unstage files |
--hard |
Resets | Resets | Dangerous! Completely reset everything |
Examples:
1. Unstage files (mixed is default):
bashgit reset
This removes files from staging area, but keeps changes in working directory.
2. Undo last commit (keep changes staged):
bashgit reset --soft HEAD~1
Use when you committed by mistake but want to keep your changes staged.
3. Undo last commit (unstage changes):
bashgit reset --mixed HEAD~1
Undo the last commit and put the changes back to working directory (not staged).
4. Full reset – delete commit and changes:
bashgit reset --hard HEAD~1
Removes the last commit, staging, and working directory changes.
This is not reversible unless you have backup or reflog!
HEAD~1 meaning?
HEAD~1 = one commit before the current one.
You can also use commit hashes, like:
bashgit reset --hard 6a1cfa2
Recover from git reset --hard (if needed):
If you made a mistake:
bashgit reflog
git reset --hard <commit_id_from_reflog>
When to Use git reset
-
Fix a bad commit
-
Unstage or re-stage files
-
Start over from a previous commit
Git Revert command
The git revert command is used to undo a previous commit by creating a new commit that reverses the changes made in the original one — without rewriting the commit history.

Syntax:
bashgit revert <commit>
Key Points:
-
It does not delete the original commit.
-
It creates a new commit with opposite changes.
-
It is safe for shared/public branches like
mainormaster. -
Useful when you want to undo a commit without rewriting history (unlike
git reset).
Example:
Revert a single commit:
bashgit revert a1b2c3d
This will create a new commit that undoes the changes of commit a1b2c3d.
Interactive message (default):
When you run git revert, it opens your text editor to enter a message for the new revert commit:
sql
Revert "Add login feature"
This reverts commit a1b2c3d.
You can also use --no-edit to skip this:
bashgit revert a1b2c3d --no-edit
Revert the last commit:
bashgit revert HEAD
Revert multiple commits (in sequence):
bashgit revert HEAD~2..HEAD
This reverts the last 3 commits (HEAD, HEAD1, HEAD2).
Conflict Handling:
If the commit you’re reverting has merge conflicts, Git will notify you and you’ll need to manually resolve them before completing the revert:
bashgit status
git add <resolved_files>
git revert --continue
git revert vs git reset:
| Feature | git revert |
git reset |
|---|---|---|
| Safe for shared branches | Yes | No (rewrites history) |
| Deletes commits | No (adds new commit) | Yes (can remove commits permanently) |
| Keeps history | Yes (traceable) | No (can lose history) |
| Used in public branches | Recommended | Avoid in public branches |
When to Use git revert:
-
You want to undo changes without breaking collaboration.
-
You’re working on a shared/public branch.
-
You want a clear history of corrections.
Git – git ignore file
What is .gitignore in Git?
The .gitignore file tells Git which files and folders to ignore – meaning Git won’t track, stage, or commit them to the repository.

Why is .gitignore used?
It helps avoid committing:
-
Temporary files (e.g.,
.log,.tmp) -
Build output (e.g.,
/dist,/build) -
Sensitive information (e.g.,
.env) -
System/IDE files (e.g.,
.DS_Store,.vscode/)
Sample .gitignore file:
bash
# Ignore log files# Ignore node_modules folder
node_modules/
*.log# Ignore environment file
.env# Ignore build folder
build/
# Ignore macOS system file
.DS_Store
Important Notes:
-
The
.gitignorefile is placed in the root directory of your Git project. -
If a file is already being tracked by Git, adding it to
.gitignorewon’t stop tracking it.-
You must first remove it from staging:
bash
git rm --cached filename
-
How to create and use .gitignore:
-
Create a
.gitignorefile in the project’s root folder. -
Add the patterns of files/folders you want Git to ignore.
-
Save and commit it:
bash
git add .gitignore
git commit -m "Add .gitignore file"
Bonus – Get Prebuilt Templates:
If you’re working on a specific language or framework (like Python, Node.js, Java, etc.), you can get ready-to-use .gitignore templates from:
Git -diff command
What is git diff?
The git diff command shows the difference between:
-
files in your working directory and staging area
-
or between commits
-
or between branches
It helps you see what has changed before you commit or push.
Basic Syntax:
bashgit diff
This shows the difference between your working directory and the staging area (index).
Common Use Cases of git diff:
| Command | Description |
|---|---|
git diff |
Shows unstaged changes (Working Directory → Staging Area) |
git diff --staged or git diff --cached |
Shows staged changes (Staging Area → Last Commit) |
git diff HEAD |
Shows all changes since the last commit (both staged and unstaged) |
git diff branch1 branch2 |
Shows differences between two branches |
git diff commit1 commit2 |
Shows changes between two commits |
git diff filename |
Shows changes in a specific file |
Example Output:
diff- console.log("Old line");
+ console.log("New line");
-
Lines prefixed with
-are removed -
Lines prefixed with
+are added
Visual Example:
Let’s say you changed a file named app.js but haven’t staged it yet. Running:
bashgit diff app.js
will show what’s different between the saved file and the current version in Git.
Helpful Tip:
Use this command before committing to review your changes and avoid mistakes.
Git-Distributed Version Control System part-4