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
git merge
is used to combine the changes of one branch into another.
Syntax:
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:
# 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:
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:
# 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
Feature | git merge | git rebase |
---|---|---|
History | Preserved, with merge commits | Rewritten, linear |
Merge commit | Yes | No |
Safer for teamwork | Yes | No (can be risky on shared branches) |
Use case | Safe merging of branches | Clean 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:
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.”
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 (likemain
) without merging the entire branch.Useful for hotfixes, backporting, or selective updates.
Syntax
git cherry-pick <commit-hash>
You can also cherry-pick a range of commits:
git cherry-pick <commit1>^..<commit3>
Example
You are on the
main
branch.You want to copy a commit from
feature-branch
.
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:
# Resolve the conflict in files
git add <resolved-file>
git cherry-pick --continue
To abort the cherry-pick if things go wrong:
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
Feature | Description |
---|---|
Command | git cherry-pick <commit-hash> |
Purpose | Apply specific commit(s) from another branch |
Best Use Case | Hotfixes, small feature duplication, selective changes |
Risk | May cause merge conflicts or break code if dependencies missing |
Git – Stash
Git – stash
Command Explained
git stash
is a handy Git command used to temporarily save your uncommitted changes without committing them. It lets you switch branches or pull new changes without losing your work.
Why Use git stash
?
Imagine you’re working on a feature but need to:
Switch to another branch quickly
Pull latest code from remote
Work on a different task temporarily
Instead of committing half-done code, you can stash it and come back to it later.
Basic Syntax
git stash
This command:
Saves your uncommitted changes
Cleans your working directory (restores to last commit)
Example Workflow
# Step 1: You're working on files
nano file1.txt
nano file2.txt
# Step 2: Stash the changesgit stash
# Step 3: Switch branchesgit checkout main
# Step 4: Return to your work latergit checkout feature-branch
git stash apply
Useful Variants
Command | Purpose |
---|---|
git stash | Stash all changes (tracked files only) |
git stash -u or --include-untracked | Stash untracked files too |
git stash list | Show all stashed changes |
git stash apply | Reapply last stash (keeps it in stash list) |
git stash pop | Reapply and remove last stash |
git stash drop | Delete a specific stash |
git stash clear | Delete all stashes |
Example with Messages
git stash save "WIP: fixing bug in login"
Later, you can see it with:
git stash list
Undoing a Stash
git stash pop # Applies and removes stash
git stash apply # Applies but keeps stash
git stash drop stash@{0} # Manually delete a stash
Important Notes
Stashing is local; it does not affect the remote repository.
Be careful with untracked/ignored files — use
-u
or-a
if needed.
Summary
Feature | Description |
---|---|
What it does | Temporarily saves uncommitted changes |
Useful for | Switching branches without losing work |
Risk | Forgetting to reapply or losing stash if not used properly |
Common use | git stash , git stash apply , git stash list |
Git – WorkTree
Git – worktree
Explained
git worktree
allows you to check out multiple branches at the same time in different directories from a single Git repository. It’s like having multiple working directories for the same project — great for testing, bug fixing, or working on different features without constant switching.
Why Use git worktree
?
Normally, Git allows only one branch checked out at a time per clone. With git worktree
, you can:
Work on multiple branches simultaneously.
Avoid switching back and forth.
Reduce confusion when handling hotfixes or parallel feature development.
Basic Syntax
git worktree add <path> <branch-name>
Example Workflow
Let’s say you’re on main
branch and want to work on a new branch feature-x
without switching from main
:
# Step 1: Create a new worktree with a new branch
git worktree add ../feature-x-dir feature-x
# Step 2: Go to the new directorycd ../feature-x-dir
# Now you’re on `feature-x` branch in a new folder, and can work freelyNow:
Your main project (original folder) is still on
main
Your new folder (
feature-x-dir
) is working withfeature-x
Common Commands
Command | Purpose |
---|---|
git worktree list | Shows all active worktrees |
git worktree add <dir> <branch> | Adds a new worktree |
git worktree remove <dir> | Removes the specified worktree |
git worktree prune | Cleans up stale/invalid worktrees |
Notes
You can use existing branches or create a new one.
If the branch doesn’t exist, Git will create it from your current HEAD.
Worktrees are perfect for parallel development, testing, and hotfixes.
Real Example
# Add a new worktree for hotfix branch
git worktree add ../hotfix-login hotfix-login
# Work on the fix in ../hotfix-login directorycd ../hotfix-login
Worktree Storage
Git tracks all worktrees in:
.git/worktrees/
Avoid manually editing files in this directory.
Summary
Feature | Description |
---|---|
What it does | Lets you use multiple branches at once in different directories |
Use Case | Parallel feature dev, hotfixes, testing |
Key Commands | git worktree add , git worktree list , remove |
Bonus | Cleaner than cloning repo again |