Advanced Git Concepts
Undoing Changes
Section titled “Undoing Changes”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:
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:
# Revert changes in 'example.txt' to its last committed stategit 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:
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:
# 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.
Amending Commits (git commit --amend
)
Section titled “Amending Commits (git commit --amend)”Purpose:
The git commit --amend
command allows you to modify the last commit, including updating the commit message or adding new changes.
Usage:
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:
# Add changes to the staging areagit add additional-file.txt
# Amend the last commit to include these changesgit commit --amend
Note: Amending a commit changes its hash, which can affect other collaborators if the commit has already been pushed.
Reverting Commits (git revert
)
Section titled “Reverting Commits (git revert)”Purpose:
The git revert
command creates a new commit that undoes the changes introduced by a previous commit.
Usage:
git revert commit-hash
What It Does:
- Reverts the changes from the specified commit and creates a new commit to apply the reversal.
Example:
# 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.
Rebasing
Section titled “Rebasing”Understanding Rebasing
Section titled “Understanding Rebasing”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 vs. Merging
Section titled “Rebasing vs. Merging”- 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:
# Rebase feature-branch onto maingit checkout feature-branchgit rebase main
Merge Example:
# Merge feature-branch into maingit checkout maingit merge feature-branch
Performing a Rebase (git rebase
)
Section titled “Performing a Rebase (git rebase)”Purpose:
The git rebase
command re-applies commits from your current branch onto a new base commit.
Usage:
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:
# Rebase the 'feature-branch' onto the 'main' branchgit checkout feature-branchgit rebase main
Interactive Rebase: For more control over commits, use interactive rebase:
git rebase -i base-branch
What It Allows:
- Reorder, squash, or edit commits during the rebase process.
Resolving Rebase Conflicts
Section titled “Resolving Rebase Conflicts”Purpose: During a rebase, conflicts can arise if changes from the branch and the base branch cannot be automatically merged.
Steps:
-
Resolve Conflicts: Edit the conflicted files and resolve conflicts. Mark them as resolved.
-
Stage Resolved Files:
Terminal window git add resolved-file.txt -
Continue Rebase:
Terminal window git rebase --continue -
Abort Rebase (if necessary):
Terminal window git rebase --abort
Example:
git checkout feature-branchgit rebase main# If conflicts arise, resolve them in conflicted filesgit add conflicted-file.txtgit rebase --continue
Stashing Changes
Section titled “Stashing Changes”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:
git stash
What It Does:
- Stashes your uncommitted changes.
- Restores your working directory to match the HEAD commit.
Example:
# Save current changes and revert working directory to last commitgit stash
Naming Stashes: You can also provide a message for better identification:
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:
git stash apply
What It Does:
- Applies the changes from the most recent stash.
Example:
# Apply the most recent stashgit stash apply
Applying a Specific Stash:
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:
git stash list
What It Shows:
- Lists stashes with their identifiers and messages.
Example:
git stash list# Output: stash@{0}: WIP on branch: message
Dropping Stashes:
To remove a stash from the list, use git stash drop
.
Usage:
git stash drop stash@{stash-number}
Example:
# Drop the stash with index 0git stash drop stash@{0}
Clearing All Stashes: To remove all stashes:
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.