Skip to content

Advanced Git Concepts

Undoing Changes in the Working Directory (git checkout --)

Section titled “Undoing Changes in the Working Directory (git checkout --)”

Purpose: The git checkout -- command reverts changes in the working directory that have not yet been staged or committed.

Usage:

Terminal window
git checkout -- filename

What It Does:

  • Discards changes in filename that have been made in the working directory.
  • Restores the file to its state in the last commit.

Example:

Terminal window
# Revert changes in 'example.txt' to its last committed state
git checkout -- example.txt

Note: This command does not affect changes that have already been staged or committed.

Removing Files from Staging (git reset HEAD)

Section titled “Removing Files from Staging (git reset HEAD)”

Purpose: The git reset HEAD command removes files from the staging area, but keeps the changes in the working directory.

Usage:

Terminal window
git reset HEAD filename

What It Does:

  • Unstages filename but does not discard the changes made to it.
  • The changes remain in the working directory.

Example:

Terminal window
# Unstage changes to 'example.txt'
git reset HEAD example.txt

Note: This command is useful if you accidentally added files to the staging area and want to modify them further before committing.

Purpose: The git commit --amend command allows you to modify the last commit, including updating the commit message or adding new changes.

Usage:

Terminal window
git commit --amend

What It Does:

  • Opens the default text editor to modify the commit message.
  • Includes any staged changes in the amended commit.

Example:

Terminal window
# Add changes to the staging area
git add additional-file.txt
# Amend the last commit to include these changes
git commit --amend

Note: Amending a commit changes its hash, which can affect other collaborators if the commit has already been pushed.

Purpose: The git revert command creates a new commit that undoes the changes introduced by a previous commit.

Usage:

Terminal window
git revert commit-hash

What It Does:

  • Reverts the changes from the specified commit and creates a new commit to apply the reversal.

Example:

Terminal window
# Revert the changes introduced by the commit with hash 'a1b2c3d'
git revert a1b2c3d

Note: This command is useful for undoing changes in a way that maintains the project history.

Purpose: Rebasing is a Git operation that integrates changes from one branch into another by changing the base of the branch.

Concept:

  • Rebase: Reapply commits from a branch onto a new base commit.
  • Rebase vs. Merge: Rebasing rewrites history by creating new commits, while merging combines branches without rewriting history.
  • Rebasing: Rewrites history by moving the entire branch to start from a new base commit.
  • Merging: Integrates branches without changing their history. Merging keeps the commit history as is and creates a new merge commit.

Rebase Example:

Terminal window
# Rebase feature-branch onto main
git checkout feature-branch
git rebase main

Merge Example:

Terminal window
# Merge feature-branch into main
git checkout main
git merge feature-branch

Purpose: The git rebase command re-applies commits from your current branch onto a new base commit.

Usage:

Terminal window
git rebase base-branch

What It Does:

  • Moves the current branch to start from base-branch.
  • Re-applies commits from the current branch on top of base-branch.

Example:

Terminal window
# Rebase the 'feature-branch' onto the 'main' branch
git checkout feature-branch
git rebase main

Interactive Rebase: For more control over commits, use interactive rebase:

Terminal window
git rebase -i base-branch

What It Allows:

  • Reorder, squash, or edit commits during the rebase process.

Purpose: During a rebase, conflicts can arise if changes from the branch and the base branch cannot be automatically merged.

Steps:

  1. Resolve Conflicts: Edit the conflicted files and resolve conflicts. Mark them as resolved.

  2. Stage Resolved Files:

    Terminal window
    git add resolved-file.txt
  3. Continue Rebase:

    Terminal window
    git rebase --continue
  4. Abort Rebase (if necessary):

    Terminal window
    git rebase --abort

Example:

Terminal window
git checkout feature-branch
git rebase main
# If conflicts arise, resolve them in conflicted files
git add conflicted-file.txt
git rebase --continue

Using the Stash to Save Work in Progress (git stash)

Section titled “Using the Stash to Save Work in Progress (git stash)”

Purpose: The git stash command temporarily saves changes in your working directory and staging area to a stack, allowing you to work on something else without committing.

Usage:

Terminal window
git stash

What It Does:

  • Stashes your uncommitted changes.
  • Restores your working directory to match the HEAD commit.

Example:

Terminal window
# Save current changes and revert working directory to last commit
git stash

Naming Stashes: You can also provide a message for better identification:

Terminal window
git stash save "message describing the stash"

Applying Stashed Changes (git stash apply)

Section titled “Applying Stashed Changes (git stash apply)”

Purpose: The git stash apply command restores the most recently stashed changes to your working directory.

Usage:

Terminal window
git stash apply

What It Does:

  • Applies the changes from the most recent stash.

Example:

Terminal window
# Apply the most recent stash
git stash apply

Applying a Specific Stash:

Terminal window
git stash apply stash@{stash-number}

Listing and Dropping Stashes (git stash list, git stash drop)

Section titled “Listing and Dropping Stashes (git stash list, git stash drop)”

Listing Stashes: The git stash list command shows all stashed changes.

Usage:

Terminal window
git stash list

What It Shows:

  • Lists stashes with their identifiers and messages.

Example:

Terminal window
git stash list
# Output: stash@{0}: WIP on branch: message

Dropping Stashes: To remove a stash from the list, use git stash drop.

Usage:

Terminal window
git stash drop stash@{stash-number}

Example:

Terminal window
# Drop the stash with index 0
git stash drop stash@{0}

Clearing All Stashes: To remove all stashes:

Terminal window
git stash clear

What It Does:

  • Clears all stashed changes from the stash stack.

These commands and techniques help manage changes effectively in various scenarios, whether you need to undo changes, manage branches, or collaborate with others.