Git-Distributed Version Control System part-3

Git-Distributed Version Control System part-3

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)

What is Git Reset?. Git reset is a command in the Git… | by Meghasharmaa | Medium

Syntax:

css
git reset [mode] [commit]

Modes of git reset:

ModeStaging AreaWorking DirectoryUse Case
--soft       Keeps KeepsUndo commits but keep staged changes
--mixed Resets KeepsDefault. Undo commits + unstage files
--hardResets ResetsDangerous! Completely reset everything

 Examples:

1.  Unstage files (mixed is default):

bash
git reset

This removes files from staging area, but keeps changes in working directory.

2.  Undo last commit (keep changes staged):

bash
git reset --soft HEAD~1

Use when you committed by mistake but want to keep your changes staged.

3. Undo last commit (unstage changes):

bash
git 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:

bash
git 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:

bash
git reset --hard 6a1cfa2

 Recover from git reset --hard (if needed):

If you made a mistake:

bash
git 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.

Git Revert. In Git, git revert is a command used to… | by Meghasharmaa | Medium

Syntax:

bash
git 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 main or master.

  • Useful when you want to undo a commit without rewriting history (unlike git reset).

 Example:

Revert a single commit:

bash
git 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:

bash
git revert a1b2c3d --no-edit

 Revert the last commit:

bash
git revert HEAD

 Revert multiple commits (in sequence):

bash
git 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:

bash
git status
git add <resolved_files>
git revert --continue

git revert vs git reset:

Featuregit revertgit 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 – .gitignore 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.

gitignore examples to ignore files, folder & pattern | GoLinuxCloud

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 node_modules folder
node_modules/
# Ignore log files
*.log# Ignore environment file
.env# Ignore build folder
build/

# Ignore macOS system file
.DS_Store

Important Notes:

  • The .gitignore file is placed in the root directory of your Git project.

  • If a file is already being tracked by Git, adding it to .gitignore won’t stop tracking it.

    • You must first remove it from staging:

      bash
      git rm --cached filename

How to create and use .gitignore:

  1. Create a .gitignore file in the project’s root folder.

  2. Add the patterns of files/folders you want Git to ignore.

  3. 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:

Click Here

 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:

bash
git diff

This shows the difference between your working directory and the staging area (index).

Common Use Cases of git diff:

CommandDescription
git diffShows unstaged changes (Working Directory → Staging Area)
git diff --staged or git diff --cachedShows staged changes (Staging Area → Last Commit)
git diff HEADShows all changes since the last commit (both staged and unstaged)
git diff branch1 branch2Shows differences between two branches
git diff commit1 commit2Shows changes between two commits
git diff filenameShows 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:

bash
git 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-2 

Previous

Git-Distributed Version Control System part-4

Next