Pages

GIT: Chapter 9 — Advanced Git Operations (Stash, Reset, Revert, Clean)

Chapter 9 — Advanced Git Operations (Stash, Reset, Revert, Clean)


9.1 Introduction

While basic Git operations manage standard development workflows, advanced commands provide mechanisms for temporary context switching, history correction, workspace cleanup, and controlled rollback.

This chapter explores:

  • Git stash for temporary work preservation

  • Reset for pointer manipulation

  • Revert for safe history undo

  • Clean for workspace sanitization

  • Practical recovery scenarios

Mastery of these commands significantly improves repository control and developer productivity.


9.2 Git Stash — Concept

Git stash temporarily saves uncommitted changes without creating a commit.

Use cases:

  • Switching branches mid-task

  • Pulling updates without committing partial work

  • Experimentation without history pollution

Conceptual flow:

Working changes → stash → clean workspace

9.3 Creating a Stash

git stash

Git stores:

  • Modified tracked files

  • Staged changes

Workspace reverts to last commit.


9.4 Stash with Message

git stash push -m "login feature WIP"

Improves stash identification.


9.5 Listing Stashes

git stash list

Output example:

stash@{0}: On main: login feature WIP
stash@{1}: On main: ui fixes

9.6 Applying Stash

git stash apply

Applies latest stash but retains it.

Apply specific stash:

git stash apply stash@{1}

9.7 Pop Stash

git stash pop

Applies and removes stash simultaneously.


9.8 Dropping Stash

git stash drop stash@{0}

Remove specific stash.

Clear all:

git stash clear

9.9 Stashing Untracked Files

Default stash ignores untracked files.

Include them:

git stash -u

Include ignored files:

git stash -a

9.10 Git Reset — Concept

Reset moves branch pointer and optionally modifies index and working directory.

Three reset modes:

ModePointerIndexWorking directory
Soft
Mixed
Hard

9.11 Soft Reset

git reset --soft HEAD~1

Effects:

  • Moves HEAD back

  • Keeps staged changes

  • Commit undone but content preserved

Useful for commit message correction.


9.12 Mixed Reset (Default)

git reset HEAD~1

Effects:

  • Moves HEAD

  • Unstages changes

  • Preserves working files

Commonly used to unstage commits.


9.13 Hard Reset

git reset --hard HEAD~1

Effects:

  • Moves HEAD

  • Resets index

  • Discards working changes

Destructive operation.


9.14 Reset to Specific Commit

git reset --hard commit-hash

Used for repository state rollback.


9.15 Git Revert — Concept

Revert creates a new commit that reverses changes of a previous commit.

Key difference from reset:

  • Preserves history

  • Safe for shared branches


9.16 Basic Revert

git revert commit-hash

Git:

  1. Computes inverse diff

  2. Creates new commit

History remains intact.


9.17 Reverting Multiple Commits

git revert HEAD~3..HEAD

Sequential revert commits created.


9.18 Reverting Merge Commits

git revert -m 1 merge-commit

-m specifies mainline parent.

Required due to multi-parent structure.


9.19 Reset vs Revert

ResetRevert
Rewrites historyPreserves history
Local correctionCollaborative undo
Pointer movementNew commit creation
Potentially destructiveSafe

Rule of thumb:

  • Private branch → reset

  • Shared branch → revert


9.20 Git Clean — Concept

Clean removes untracked files from working directory.

Use cases:

  • Build artifact removal

  • Temporary file cleanup

  • Workspace normalization


9.21 Preview Clean Operation

git clean -n

Shows files that will be removed.


9.22 Execute Clean

git clean -f

Deletes untracked files.


9.23 Clean Directories

git clean -fd

Removes untracked directories.


9.24 Remove Ignored Files

git clean -fx

Deletes ignored files (e.g., build outputs).


9.25 Interactive Clean

git clean -i

Provides selection interface.


9.26 Practical Recovery Scenario

Undo last commit but keep changes

git reset --soft HEAD~1

Undo commit and unstage

git reset HEAD~1

Undo commit completely

git reset --hard HEAD~1

Undo pushed commit safely

git revert commit-hash

9.27 Combining Stash and Pull

git stash
git pull
git stash pop

Common real-world workflow.


9.28 Common Mistakes

MistakeConsequence
Hard reset on shared branchHistory loss
Cleaning without previewAccidental deletion
Stash overloadContext confusion
Reset without reflog awarenessRecovery complexity

9.29 Recovery via Reflog

If reset removes commits:

git reflog
git reset --hard lost-hash

Reflog enables commit restoration.


9.30 Best Practices

  • Prefer revert for shared history

  • Use soft reset for commit refinement

  • Always preview clean

  • Annotate stashes

  • Use reflog as safety mechanism


9.31 Summary

This chapter covered:

  • Stash mechanics and lifecycle

  • Reset modes and pointer manipulation

  • Revert as collaborative-safe undo

  • Clean for workspace maintenance

  • Recovery workflows

  • Operational best practices

These commands provide granular repository control beyond standard commit workflows and are essential for professional Git usage.

No comments:

Post a Comment