Git-Distributed Version Control System part-5

Git-Distributed Version Control System part-5

Git-Distributed Version Control System part-5

Git-Distributed Version Control System part-5

Continue learning Git-Distributed Version Control System part-5 in this advanced guide. Explore branching, merging, and squashing with real-world Git workflow examples.

 Git – Merge Rebase

Git MERGE vs REBASE: Everything You Need to Know

Git Merge

git merge is used to combine the changes of one branch into another.

 Syntax:

bash
git checkout main
git merge feature-branch

How it works:

  • It creates a new “merge commit” that joins the histories of both branches.

  • Keeps the full history of both branches.

Example:

bash
# Assume you're on main
git checkout main
git merge feature-branch

This will combine feature-branch into main.

Advantages:

  • Maintains complete history.

  • Safe and simple.

Disadvantages:

  • History can get messy with too many merge commits.

Git Rebase

git rebase is used to move or reapply commits from one branch onto another base.

Syntax:

bash
git checkout feature-branch
git rebase main

How it works:

  • It rewrites the commit history of your branch as if it was created on top of the latest main branch.

  • No merge commit is created.

Example:

bash
# Assume you're on feature-branch
git checkout feature-branch
git rebase main

Now, feature-branch will look like it started from the tip of main.

Advantages:

  • Cleaner and linear history.

  • Ideal for reviewing or sharing.

Disadvantages:

  • Can be dangerous if used on public/shared branches (it rewrites history).

Merge vs Rebase – Key Differences

Featuregit mergegit rebase
HistoryPreserved, with merge commitsRewritten, linear
Merge commitYesNo
Safer for teamworkYesNo (can be risky on shared branches)
Use caseSafe merging of branchesClean commit history before merging

When to Use:

  • Use merge when working in teams or preserving history is important.

  • Use rebase when you want a clean, linear history (e.g., before merging your feature branch).

Tip:

Before merging a feature branch to main, it’s common to:

bash
git checkout feature-branch
git rebase main # To update with latest main commits
git checkout main
git merge feature-branch # Now the merge will be clean
 

Git – Cherry-pick

Git – cherry-pick Command

git cherry-pick is a powerful Git command that allows you to apply a specific commit from one branch onto another branch. It’s like saying:

“I want this one commit from over there, but not the whole branch.”

Understanding Cherry Picking in Git | 2023 | by Amir Mustafa | Medium

Why Use cherry-pick?

  • You made a useful change on one branch (like feature-x) but you want only that specific commit on another branch (like main) without merging the entire branch.

  • Useful for hotfixes, backporting, or selective updates.

Syntax

bash
git cherry-pick <commit-hash>

You can also cherry-pick a range of commits:

bash
git cherry-pick <commit1>^..<commit3>

 Example

  1. You are on the main branch.

  2. You want to copy a commit from feature-branch.

bash
git checkout main
git cherry-pick a1b2c3d4

Here, a1b2c3d4 is the commit hash from feature-branch. Git will apply that commit onto your current branch (main).

Handling Conflicts

If the commit causes a conflict:

bash
# Resolve the conflict in files
git add <resolved-file>
git cherry-pick --continue

To abort the cherry-pick if things go wrong:

bash
git cherry-pick --abort

Tips

  • Always test after cherry-picking — it can cause issues if dependencies from earlier commits are missing.

  • Use git log to find the commit hash you want.

Summary

FeatureDescription
Commandgit cherry-pick <commit-hash>
PurposeApply specific commit(s) from another branch
Best Use CaseHotfixes, small feature duplication, selective changes
RiskMay cause merge conflicts or break code if dependencies missing
 

Git-Distributed Version Control System part-4   

Previous                         

Git-Distributed Version Control System part-6

Next