Learning points:
๐น Git Rebase โ Deep Dive
๐น Git Cherry-pick โ Applying specific commits from one branch to another.
๐น Resolving Merge Conflicts โ Identifying and resolving conflicts efficiently.
๐น Rewriting History โ advanced use cases
๐น Amending Commits โ How to modify the last commit without changing history and real-life use cases.
๐น Git Hooks โ Automating tasks with pre-commit and post-commit hooks.
Advanced Git Challenges and Solutions
Prerequisites
Before diving into advanced Git operations, ensure you have:
Installed Git on your system.
A GitHub account with an existing repository (or set up a new one).
Cloned the repository locally using:
git clone <repo-url> cd <repo-name>
โ Task 1: Set Up a Git Repository
Scenario
You can either create a new repository on GitHub or use an existing one. If using an existing repository, navigate to its directory.
Commands
git init # Initialize a new Git repository (only for new repositories)
git remote add origin <repo-url> # Add remote repository if not added
git fetch --all # Ensure all branches are available locally
git branch -a # List all branches
โ Task 2: Create a New Branch and Make Commits
Scenario
Create a new branch named advanced-git-practice
, switch to it, and add commits.
Commands
git checkout -b advanced-git-practice # Create and switch to the new branch
touch file1.txt && echo "Initial content" > file1.txt
git add file1.txt
git commit -m "Added file1.txt with initial content"
echo "New changes" >> file1.txt
git commit -am "Updated file1.txt with new changes"
โ Task 3: Push Your Branch to Remote Repository
Scenario
Push the newly created advanced-git-practice
branch to the remote repository.
Commands
git push origin advanced-git-practice
โ Task 4: Simulate a Collaborative Workflow
Scenario
Create a new feature branch (feature-x
), add commits, and merge it into main
.
Commands
git checkout -b feature-x
echo "Feature X content" > feature-x.txt
git add feature-x.txt
git commit -m "Added feature-x.txt"
git checkout main
git merge feature-x # Merge feature branch into main
git push origin main # Push updated main branch
โ Task 5: Create and Resolve a Merge Conflict
Scenario
Modify the same part of a file in two branches (conflict-branch
and main
) to create a merge conflict.
Commands
git checkout -b conflict-branch
echo "Conflicting change from conflict-branch" > conflict-file.txt
git add conflict-file.txt
git commit -m "Change in conflict-file.txt from conflict-branch"
git checkout main
echo "Conflicting change from main" > conflict-file.txt
git add conflict-file.txt
git commit -m "Change in conflict-file.txt from main"
git merge conflict-branch # This will trigger a conflict
Resolving Conflict
Open the conflicted file (
conflict-file.txt
).Manually edit and resolve the conflicting sections.
Mark the conflict as resolved and commit:
git add conflict-file.txt git commit -m "Resolved merge conflict in conflict-file.txt" git push origin main
โ Task 6: Visualizing Commit History
Scenario
Use Git log to view the commit graph.
Commands
git log --oneline --graph --all
This will show the commit history in a tree-like structure, helping you understand the branching and merging history.
Challenge 1: Perform an interactive rebase to modify commit history (rename, squash, reorder commits).
Goal: Learn how to edit commit history interactively.
Answer:
# Start interactive rebase for the last N commits
# Replace N with the number of commits you want to modify
git rebase -i HEAD~N
Explanation:
This command opens an interactive editor where you can:
pick: Keep commit as is.
reword: Change commit message.
squash: Merge commit with the previous one.
edit: Modify the commit content.
drop: Remove commit.
Challenge 2: Use git cherry-pick
to apply a specific commit from another branch to your current branch.
Goal: Learn how to pick and apply a commit selectively.
Answer:
git checkout feature-branch
git cherry-pick <commit-hash>
Explanation:
git cherry-pick
applies a commit from another branch to the current branch.<commit-hash>
is the ID of the commit you want to apply.
Challenge 3: Create a merge conflict scenario and manually resolve it using git merge
and git rebase
.
Goal: Understand merge conflicts and how to resolve them.
Answer:
git checkout -b feature-branch
echo "Feature change" > file.txt
git add file.txt
git commit -m "Feature branch change"
git checkout main
echo "Main branch change" > file.txt
git add file.txt
git commit -m "Main branch change"
git merge feature-branch
Explanation:
This setup creates a merge conflict in
file.txt
.Git will stop at the conflict, and you need to manually edit the file to resolve it.
Use
git add file.txt
andgit commit
to complete the merge.
Alternatively, if using rebase:
git checkout feature-branch
git rebase main
This moves
feature-branch
commits on top ofmain
.Resolve conflicts manually and use
git rebase --continue
.
Challenge 4: Undo a commit using git reset
(soft, mixed, and hard) and git revert
.
Goal: Learn different ways to undo changes in Git.
Answer:
# Soft reset: Keeps changes in staging area
git reset --soft HEAD~1
# Mixed reset: Keeps changes in working directory, unstages them
git reset --mixed HEAD~1
# Hard reset: Deletes changes permanently
git reset --hard HEAD~1
# Revert commit safely
git revert <commit-hash>
Explanation:
reset
modifies history (not recommended if commits are pushed).revert
creates a new commit that undoes the changes safely.
Challenge 5: Amend the last commit message and add a forgotten file using git commit --amend
.
Goal: Modify the most recent commit without creating a new one.
Answer:
git add forgotten-file.txt
git commit --amend -m "Updated commit message with forgotten file"
Explanation:
--amend
allows modifying the last commitโs message and content.
Challenge 6: Set up Git hooks (pre-commit or post-commit) to automate a simple check before committing changes.
Goal: Automate checks before a commit is created.
Answer:
mkdir -p .git/hooks
echo -e "#!/bin/sh\necho 'Checking before commit'" > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
Explanation:
This sets up a
pre-commit
hook that runs before committing changes.You can add checks like linting, formatting, or security scans.
Challenge 7: Rebase a feature branch on top of the main branch without creating unnecessary merge commits.
Goal: Keep a clean Git history using rebase.
Answer:
git checkout feature-branch
git rebase main
Explanation:
- This moves
feature-branch
commits to the top ofmain
, avoiding merge commits.
Challenge 8: Create a branch, make multiple commits, then squash them into a single commit using git rebase -i
.
Goal: Learn commit squashing to keep a clean history.
Answer:
git checkout -b new-feature
# Make multiple commits
echo "First change" > file.txt
git add file.txt
git commit -m "First commit"
echo "Second change" > file.txt
git add file.txt
git commit -m "Second commit"
# Squash commits
git rebase -i HEAD~2
Explanation:
This opens an interactive editor where you can squash commits into one.
Change
pick
tosquash
(ors
) for the commits you want to merge.
๐ Topics Covered Today:
โ
Interactive Rebase (git rebase -i
)
โ
Cherry-Picking Commits (git cherry-pick
)
โ
Merge Conflicts and Resolution (git merge
, git rebase
)
โ
Undoing Commits (git reset
, git revert
)
โ
Amending Commits (git commit --amend
)
โ
Git Hooks (.git/hooks/pre-commit
)
โ
Rebasing a Feature Branch (git rebase
)
โ
Squashing Commits (git rebase -i
)