The Staging Area (Recap)

The Staging Area (also called the Index) is an intermediate area between your Working Directory and the Git Repository.

It allows you to select exactly which changes should be included in the next commit.

The Git workflow follows three stages:

Advertisement
 
Working Directory
       │
git add
       ▼
Staging Area (Index)
       │
git commit
       ▼
Local Repository
 
  • Working Directory → Where you create and edit files.
  • Staging Area → Stores the changes you've selected using git add.
  • Repository → Stores committed snapshots of your project.

Interview Answer

"The staging area acts as a bridge between the working directory and the Git repository. After modifying files, I use git add to move selected changes into the staging area, and then git commit to permanently save those staged changes into the local repository."


Why is the Staging Area Important?

The staging area allows you to:

  • Select specific files to commit.
  • Create logical commits.
  • Review changes before committing.
  • Exclude unfinished work from a commit.

Git Workflow

 
Edit Files
     │
     ▼
Working Directory
     │
git add
     ▼
Staging Area
     │
git commit
     ▼
Git Repository
 

Real-Time Example

While working on multiple files, I may stage only the completed changes using git add and leave unfinished files in the working directory. This helps create clean, logical commits.


git stash

The git stash command temporarily saves your uncommitted changes and restores your working directory to a clean state.

It allows you to switch branches or work on another task without committing incomplete work.


Interview Answer

"I use git stash whenever I have unfinished work that I don't want to commit yet. It temporarily saves my uncommitted changes, allowing me to switch branches or handle urgent tasks. Later, I restore the changes using git stash pop."


Save Current Changes

 
git stash
 

This command:

  • Saves uncommitted changes.
  • Cleans the working directory.
  • Leaves the repository unchanged.

Restore the Latest Stash

 
git stash pop
 

This command:

  • Restores the latest stash.
  • Removes it from the stash list.

Typical Workflow

 
git stash

git checkout correct-branch

git stash pop
 

git stash Workflow

 
Working Directory
        │
        ▼
git stash
        │
        ▼
Clean Working Directory
        │
Switch Branch
        │
        ▼
git stash pop
        │
        ▼
Changes Restored
 

When Should You Use git stash?

Use it when:

  • Switching branches.
  • Handling urgent production issues.
  • Pulling the latest code.
  • Moving uncommitted work to another branch.

Benefits

  • No unnecessary commits.
  • Safe temporary storage.
  • Easy branch switching.
  • Keeps Git history clean.

Real-Time Example

While developing a feature, I received an urgent production issue. Instead of committing incomplete work, I used git stash, switched to the hotfix branch, fixed the issue, and later restored my unfinished feature using git stash pop.


git reset (--soft, --mixed, --hard)

The git reset command moves the current branch pointer (HEAD) to an earlier commit.

The reset mode determines what happens to your staged and working directory changes.


Interview Answer

"I use git reset to undo commits during local development. Depending on the situation, I choose --soft, --mixed, or --hard. I avoid using reset on shared commits because it rewrites Git history."


git reset --soft

Undo the last commit while keeping changes staged.

 
git reset --soft HEAD~1
 

Result:

  • Commit removed.
  • Changes remain staged.
  • Ready for recommit.

git reset --mixed

Undo the last commit while keeping changes unstaged.

 
git reset --mixed HEAD~1
 

Result:

  • Commit removed.
  • Changes remain in the working directory.
  • Files become unstaged.

git reset --hard

Undo the last commit and delete all changes permanently.

 
git reset --hard HEAD~1
 

Result:

  • Commit removed.
  • Staged changes removed.
  • Working directory cleaned.

Comparison

Command Result
git reset --soft Undo commit, keep changes staged
git reset --mixed Undo commit, keep changes unstaged
git reset --hard Undo commit and permanently delete changes

Important Note

HEAD~1 means:

One commit before the current HEAD.


Reset Workflow

 
Commit
   │
   ▼
git reset
   │
   ▼
Soft → Staged

Mixed → Working Directory

Hard → Deleted
 

Best Practices

Use:

  • --soft → To modify commit messages or combine commits.
  • --mixed → To continue editing files.
  • --hard → Only when changes are no longer needed.

Never use --hard on important work unless you're absolutely sure.


Undo the Last Commit but Keep the Changes

Sometimes the commit is incorrect, but the code itself is fine.

In this case, undo only the commit.


Interview Answer

"When I need to undo the most recent commit but keep my code changes, I use git reset --soft HEAD~1. The commit is removed, but the changes remain staged so I can edit or recommit them."


Keep Changes Staged

 
git reset --soft HEAD~1
 

Keep Changes Unstaged

 
git reset --mixed HEAD~1
 

Example

Wrong commit:

 
git commit -m "Wrong message"
 

Undo it:

 
git reset --soft HEAD~1
 

Commit again:

 
git commit -m "Correct message"
 

Real-Time Example

I accidentally used an incorrect Jira ticket number in my commit message. Instead of creating another commit, I performed a soft reset and recommitted the same changes with the correct commit message.


git revert (Undoing a Pushed Commit)

If a commit has already been pushed to a shared repository, do not use git reset.

Instead, use git revert.


Interview Answer

"For commits that have already been pushed to the remote repository, I use git revert. It creates a new commit that reverses the previous changes without rewriting Git history, making it safe for team collaboration."


Syntax

 
git revert <commit-hash>
 

Push the new commit:

 
git push origin <branch-name>
 

How It Works

Original History

 
A → B → C → D
 

After Revert

 
A → B → C → D → E
 

Where:

  • E reverses the changes introduced by D.

Benefits

  • Safe for shared branches.
  • Does not rewrite history.
  • Easy to track reverted changes.
  • Recommended for team environments.

Real-Time Example

During a production deployment, one commit introduced a bug. Instead of resetting the shared branch, I reverted the problematic commit, creating a new commit that safely removed the issue while preserving the complete project history.


git reset vs git revert

Although both commands undo changes, they work differently.


Comparison

Feature git reset git revert
Moves HEAD ✔ Yes ✘ No
Creates New Commit ✘ No ✔ Yes
Rewrites History ✔ Yes ✘ No
Safe for Shared Branches ✘ No ✔ Yes
Best Used For Local commits Pushed commits

When to Use

Use git reset

When:

  • Commits are local.
  • History has not been shared.
  • You want to remove commits completely.

Use git revert

When:

  • Commits are already pushed.
  • Working with a team.
  • History must remain intact.

Visual Comparison

Reset

 
A → B → C → D

↓

A → B → C
 

Commit D disappears.


Revert

 
A → B → C → D → E
 

Commit E reverses D.


Real-Time Example

For my local feature branch, I often use git reset while cleaning up commits before raising a Pull Request. However, after a commit has been pushed and shared with the team, I always use git revert to safely undo changes without affecting other developers.


Frequently Asked Questions (FAQs)

1. What is git stash?

git stash temporarily saves your uncommitted changes and restores your working directory to a clean state.

To restore the latest stash:

 
git stash pop
 

It is useful when switching branches or handling urgent work without committing incomplete changes.


2. How do you undo the last commit but keep the changes?

To keep the changes staged:

 
git reset --soft HEAD~1
 

To keep the changes unstaged:

 
git reset --mixed HEAD~1
 

Both commands remove the last commit while preserving your code changes.


3. What is the difference between git reset --soft, --mixed, and --hard?

  • git reset --soft → Removes the commit but keeps changes staged.
  • git reset --mixed → Removes the commit and keeps changes unstaged.
  • git reset --hard → Removes the commit and permanently deletes all uncommitted changes.

4. How do you undo a commit that has already been pushed?

Use:

 
git revert <commit-hash>
 

This creates a new commit that reverses the specified commit without rewriting Git history, making it safe for shared repositories.


5. What is the difference between git reset and git revert?

git reset moves the HEAD pointer backward and rewrites history, making it suitable only for local, unshared commits.

git revert creates a new commit that reverses a previous commit while preserving history, making it the preferred option for shared repositories.


6. When should you use git stash vs git cherry-pick?

Use git stash when you want to temporarily save uncommitted changes and restore them later.

Use git cherry-pick when you want to copy one or more existing commits from one branch to another without merging the entire branch.

Key Difference:

  • git stash → Temporarily saves uncommitted work.
  • git cherry-pick → Copies committed changes between branches.