While working in a team environment with other engineers, conflicts are bound to happen, in this we will learn, how to git rebase, Cherry-pick, and Conflict Resolution
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP
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 fix up the last commit (it creates a new commit, so only amend what you haven't pushed) and real-life use cases.
πΉ Git Hooks β Automating tasks with pre-commit and post-commit hooks.
ποΈ Learning Resources:
- Git Rebase Guide (including interactive part) β Atlassian Git Tutorials
- Git Cherry-pick Explained β Official Git Documentation
- Understanding Merge Conflicts β Atlassian Tutorial
- Git Hooks for Automation β Git Hooks Guide
Initial Tasks:
β
Task 1: Set up a Git repository or use an existing one for practicing advanced Git commands.
β
Task 2: Create a new branch (git checkout -b advanced-git-practice) and make multiple commits.
β
Task 3: Push your branch to a remote repository (git push origin advanced-git-practice).
β
Task 4: Simulate a collaborative workflow: Create a second branch (feature-x), make commits, and merge it into the main branch.
β
Task 5: Create an intentional merge conflict between two branches and attempt to resolve it.
β
Task 6: Use git log --oneline --graph --all to visualize the commit history and branch structure.
π₯ Challenges
πΉChallenge 1: Perform an interactive rebase to modify commit history (rename, squash, reorder commits).
πΉ Challenge 2: Use git cherry-pick to apply a specific commit from another branch to your current branch.
πΉ Challenge 3: Create a merge conflict scenario and manually resolve it using git merge and git rebase.
πΉ Challenge 4: Undo a commit using git reset (soft, mixed, and hard) and git revert β understand the differences.
πΉ Challenge 5: Amend the last commit message and add a forgotten file to the last commit using git commit --amend.
πΉ Challenge 6: Set up Git hooks (pre-commit or post-commit) to automate a simple check before committing changes.
πΉ Challenge 7: Rebase a feature branch on top of the main branch without creating unnecessary merge commits.
πΉ Challenge 8: Create a branch, make multiple commits, then squash them into a single commit using git rebase -i.
Solutions:
β Challenge 1: Perform an interactive rebase to modify commit history
We can use interactive rebase (git rebase -i) to rename, squash, and reorder commits in your Git history.
Step 1: View Recent Commits
git log --oneline -n 5
Example output:
a1b2c3d (HEAD -> feature-branch) Added new feature
e4f5a6b Fixed API issue
c7d8e9f Updated documentation
0a1b2c3 Initial commit
Step 2: Start an Interactive Rebase
git rebase -i HEAD~3
This opens an editor:
# oldest commit first (the reverse of git log)
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue
pick a1b2c3d Added new featureStep 3: Modify Commits
1β£ Rename a Commit Message
Change pick to reword:
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue
reword a1b2c3d Added new feature2β£ Squash Commits
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue
squash a1b2c3d Added new feature # melds into the commit above it3β£ Reorder Commits
pick a1b2c3d Added new feature
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issueStep 4: Continue and Push Changes
# only needed if the rebase stopped (reword, conflicts)
git rebase --continue
# history changed, so the push must be forced; --force-with-lease refuses
# to overwrite commits someone else pushed in the meantime
git push --force-with-lease origin feature-branchβ Challenge 2: Use git cherry-pick
Step 1: View Commits
git checkout feature-branch
git log --oneline --graph -n 5
Step 2: Switch to Target Branch
git checkout main
Step 3: Apply Specific Commit
git cherry-pick e4f5a6b
Step 4: Resolve Conflicts (if any)
git add <resolved-file>
git cherry-pick --continue
# To abort:
git cherry-pick --abort
Step 5: Push Changes
git push origin main
β Challenge 3: Merge Conflict Scenario
Step 1: Setup Repository
git init -b main merge-conflict-demo
cd merge-conflict-demo
echo "Line 1" > conflict.txt
git add conflict.txt
git commit -m "Initial commit"
Step 2: Feature Branch
git checkout -b feature-branch
echo "Feature branch change" > conflict.txt
git commit -am "Modified conflict.txt in feature-branch"
Step 3: Main Branch Change
git checkout main
echo "Main branch change" > conflict.txt
git commit -am "Modified conflict.txt in main"
Step 4: Merge and Resolve
git merge feature-branch
# Resolve conflict manually in conflict.txt
# Then:
git add conflict.txt
git commit -m "Resolved merge conflict"
Step 5: Rebase Method
# undo the merge commit so we can try the rebase route instead
git checkout main
git reset --hard HEAD~1
# replay the feature branch on top of main and resolve the same conflict
git checkout feature-branch
git rebase main
# edit conflict.txt, then:
git add conflict.txt
git rebase --continue
# main can now fast-forward: no merge commit
git checkout main
git merge --ff-only feature-branchStep 6: Push Changes
git remote add origin <your-repo-url> # the demo repo has no remote yet
git push -u origin mainβ
Challenge 4: Undo Commit with reset and revert
Step 1: Sample Commits
git init -b main undo-demo
cd undo-demo
echo "First commit" > file.txt
git add file.txt
git commit -m "First commit"
echo "Second commit" >> file.txt
git commit -am "Second commit"
echo "Third commit" >> file.txt
git commit -am "Third commit"
git log --oneline -n 3
Option 1: Using reset
1β£ Soft Reset
# undo the last commit, keep its changes staged
git reset --soft HEAD~12β£ Mixed Reset
# undo the last commit, keep its changes in the working tree but unstaged (the default)
git reset --mixed HEAD~13β£ Hard Reset
# undo the last commit and throw its changes away
git reset --hard HEAD~1
# (try each reset on a fresh copy of the demo repo: three resets in a row
# would walk back past the first commit)Option 2: Using revert
# add a new commit that undoes HEAD; history is kept, so it is safe on shared branches
git revert HEADStep 3: Push Changes
git push --force-with-lease origin main # after reset (history rewritten)
# or
git push origin main # after revertβ Challenge 5: Amend Last Commit
Step 1: Check Last Commit
git log --oneline -n 1
Step 2: Amend Commit Message
git commit --amend -m "Updated commit message"
Step 3: Add Forgotten File
echo "New content" > forgotten.txt
git add forgotten.txt
git commit --amend --no-edit
Step 4: Push Changes
git push --force-with-lease origin main
β Challenge 6: Git Hooks
Step 1: Navigate
cd .git/hooks
Step 2: Pre-Commit Hook
nano pre-commit
Add:
#!/bin/bash
if git diff --cached --check | grep -q "trailing whitespace"; then
echo "β Commit rejected: Trailing whitespace found!"
exit 1
fi
echo "β
Pre-commit check passed!"
exit 0
Step 3: Make Executable
chmod +x pre-commit
Step 4: Test It
cd ../.. # back to the repository root
echo "Hello, world! " >> file.txt
git add file.txt
git commit -m "Test commit" # rejected: trailing whitespaceStep 5: Post-Commit Hook (Optional)
nano post-commit
Add:
#!/bin/bash
echo "β
Commit successful! Commit Hash: $(git rev-parse HEAD)"
Make executable:
chmod +x post-commit
Bonus: Share Hooks
mkdir -p .githooks
mv .git/hooks/pre-commit .githooks/
git config core.hooksPath .githooks
β Challenge 7: Rebase Feature Branch
Step 1: Checkout
git checkout feature-branch
Step 2: Update main
git fetch origin
git checkout main
git pull origin main
Step 3: Rebase
git checkout feature-branch
git rebase main
Step 4: Resolve Conflicts
git add <resolved-file>
git rebase --continue
# or
git rebase --abort
Step 5: Push Changes
git push --force-with-lease origin feature-branch
β Challenge 8: Squash Commits
Step 1: Create Branch
git checkout -b feature-branch
Step 2: Make Commits
echo "First change" > file.txt
git add file.txt
git commit -m "First commit"
echo "Second change" >> file.txt
git commit -am "Second commit"
echo "Third change" >> file.txt
git commit -am "Third commit"
Step 3: Rebase
git rebase -i HEAD~3
Step 4: Change To:
pick 9ab7890 First commit
squash def5678 Second commit
squash abc1234 Third commitStep 5: Edit Commit Message
Combined all feature changes into one commit
Step 6: Push
git push --force-with-lease origin feature-branch
Step 7: Verify
git log --oneline -n 3
All three commits are now combined into a single commit.
Bonus Read π‘ : Whatβs inside the .git folder?
This .git folder is what makes a directory a Git repository. If deleted, you lose version control. π
π .git/
βββ π branches/ # Legacy, usually empty in modern Git
βββ π hooks/ # Client-side hooks for automating tasks (pre-commit, post-merge etc.)
βββ π info/ # Contains exclude file for local ignores
β βββ π exclude # Like .gitignore but not committed
βββ π logs/ # Records history of ref updates
β βββ π refs/
β β βββ π heads/
β β β βββ π main # Log of changes to main branch
β β βββ π remotes/
β β βββ π origin/
β β βββ π main # Log of changes to remote tracking branch
β βββ π HEAD # Log of changes to HEAD
βββ π objects/ # Stores all Git objects (commits, trees, blobs)
β βββ π info/
β βββ π pack/ # Stores packed objects for efficiency
βββ π refs/ # Pointers to commit objects
β βββ π heads/
β β βββ π main # Pointer to latest commit on main
β βββ π remotes/
β β βββ π origin/
β β βββ π main # Tracks remote origin/main
β βββ π tags/ # Tag references
βββ π config # Project-specific Git configuration
βββ π description # Description of the repo (used by GitWeb)
βββ π HEAD # Points to the current branch ref
βββ π index # Staging area (cached file snapshot)
βββ π COMMIT_EDITMSG # Stores commit message during commit
βββ π FETCH_HEAD # Records the branch fetched from a remote
βββ π ORIG_HEAD # Points to previous HEAD (useful after rebase, merge, etc.)
βββ π MERGE_HEAD # Present during a merge; points to the commit being mergedExplanation of Each File and Folder:
- branches/ (π)
- This was historically used to store branch references but is now obsolete.
- hooks/ (π)
- Contains shell scripts that automate actions (e.g., pre-commit, post-commit, pre-push).
- Default ones are .sample files to guide users.
- info/ (π)
- Contains an exclude file for repository-specific ignored files (like .gitignore but local).
- logs/ (π)
- Tracks movements of HEAD and branches.
- Helps in recovering lost commits (git reflog).
- objects/ (π)
- Stores actual data as blobs (file contents), trees (directories), commits, and tags.
- Uses SHA-1 hashed directories for efficient storage.
- pack/ stores compressed data to optimize space.
- refs/ (π)
- Stores branch (heads/), remote (remotes/), and tag (tags/) references.
- These files contain commit hashes pointing to the latest commit.
- COMMIT_EDITMSG (π)
- Stores the last commit message (useful when reusing the commit message).
- config (π)
- Stores repository-level configurations like remotes, user info, etc.
- description (π)
- Used by GitWeb (a web-based Git viewer), but not required for regular use.
- HEAD (π)
- Points to the latest commit of the currently checked-out branch.
- Example: ref: refs/heads/main (means currently on main branch).
- index (π)
- Stores the staging area (aka "index"), tracking whatβs staged for commit.
- packed-refs (π)
- If Git optimizes refs, this file consolidates them instead of storing them in separate files.
β‘ Quick Reference
Keep the momentum going β everything you need on LearnXOps in one place:
- π Courses β Video courses & AI-built courses
- π Ebooks β Community ebooks & your own
- π§ͺ All Labs & Challenges β DevOps, Cloud, SRE & more
- βΈοΈ Kubernetes Labs & Playground β kind clusters, playground & challenges
- π€ AI Agent & Bot Labs β OpenClaw Β· Hermes Β· CrewAI Β· LangGraph Β· MCP

Member discussion