πŸ’‘
Hey β€” It's Sandip Das πŸ‘‹

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:


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:

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 feature

Step 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 feature

2⃣ Squash Commits

pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue
squash a1b2c3d Added new feature   # melds into the commit above it

3⃣ Reorder Commits

pick a1b2c3d Added new feature
pick c7d8e9f Updated documentation
pick e4f5a6b Fixed API issue

Step 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-branch

Step 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~1

2⃣ Mixed Reset

# undo the last commit, keep its changes in the working tree but unstaged (the default)
git reset --mixed HEAD~1

3⃣ 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 HEAD

Step 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 whitespace

Step 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 commit

Step 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 merged

Explanation of Each File and Folder:

  1. branches/ (πŸ“‚)
    • This was historically used to store branch references but is now obsolete.
  2. hooks/ (πŸ“‚)
    • Contains shell scripts that automate actions (e.g., pre-commit, post-commit, pre-push).
    • Default ones are .sample files to guide users.
  3. info/ (πŸ“‚)
    • Contains an exclude file for repository-specific ignored files (like .gitignore but local).
  4. logs/ (πŸ“‚)
    • Tracks movements of HEAD and branches.
    • Helps in recovering lost commits (git reflog).
  5. 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.
  6. refs/ (πŸ“‚)
    • Stores branch (heads/), remote (remotes/), and tag (tags/) references.
    • These files contain commit hashes pointing to the latest commit.
  7. COMMIT_EDITMSG (πŸ“„)
    • Stores the last commit message (useful when reusing the commit message).
  8. config (πŸ“„)
    • Stores repository-level configurations like remotes, user info, etc.
  9. description (πŸ“„)
    • Used by GitWeb (a web-based Git viewer), but not required for regular use.
  10. HEAD (πŸ“„)
  • Points to the latest commit of the currently checked-out branch.
  • Example: ref: refs/heads/main (means currently on main branch).
  1. index (πŸ“„)
  • Stores the staging area (aka "index"), tracking what’s staged for commit.
  1. 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:

Share this post