Day 17 of 90 Days of DevOps Challenge: Mastering Git: Stash, Merge, Rebase, Fetch, Tags & Handling Conflicts


What is git stash
?
It is a Git command that allows you to temporarily save your uncommitted changes (both tracked and untracked) without committing them to your current branch. It’s useful when switching branches, but you don’t want to lose or commit your work-in-progress.
Example: You're editing files in the feature/login-auth
branch but need to fix a bug in another branch.
git stash # Save your current changes temporarily
Switch branches, make your fix, then come back and reapply your changes:
git checkout feature/login-auth
git stash apply # Reapply stashed changes
This keeps your working directory clean while allowing you to multitask.
NOTE: By default, untracked and ignored files are not stashed unless explicitly told.
Variants
Stash including untracked files:
git stash -u # or --include-untracked
Stash only staged changes:
git stash --keep-index
Create a named stash:
git stash save "work-in-progress: fixing navbar"
Apply and remove the latest stash:
git stash pop
List all stashes:
git stash list
Delete a specific stash:
git stash drop
Delete all stashes:
git stash clear
What is git merge
?
Combines the histories of two branches.
Creates a new merge commit if branches have diverged.
Maintains the actual history and shows how branches merged over time.
Example:
git checkout feature
git merge main
This merges main
into feature
, creating a new merge commit if needed.
Advantages:
Preserves complete project history.
Clear visibility of merge points.
Disadvantages:
- History can become cluttered with extra merge commits.
What is git rebase
?
Moves (or "replays") your commits on top of another base branch.
Rewrites commit history to appear as if you created changes after the target branch.
Example:
git checkout feature
git rebase main
This replay commits from feature
on top of main
, creating a linear history.
Advantages:
Cleaner, linear commit history.
Easier to read and review.
Disadvantages:
Rewriting history can be dangerous if done on shared branches.
Should be used cautiously (especially with
force push
).
git fetch
command
Fetching gets the latest changes from the remote repository but doesn’t apply them to your local working directory.
git fetch
To apply the fetched changes:
git merge origin/main
Note:
git pull = git fetch + git merge
What is a Merge Conflict?
A Git conflict occurs when Git is unable to automatically reconcile differences between two commits during a merge, rebase, or pull operation. This usually happens when two or more branches have modified the same part of a file, and Git can’t decide which change to keep.
Why Do Conflicts Occur?
Conflicts typically arise when:
Two developers edit the same line in the same file.
A file is deleted in one branch and modified in another.
Changes are made to adjacent lines that Git cannot merge automatically.
Example Scenario:
You have two branches:
main
andfeature-branch
.Both branches edit the same line in
app.py
.
# On main branch
print("Hello from main")
# On feature-branch
print("Hello from feature branch")
Now, when you try:
git merge feature-branch
Git throws a merge conflict because it cannot determine which line to keep.
How Git Shows Conflicts
Git marks the conflicting areas like this in the file:
<<<<<<< HEAD
print("Hello from main")
=======
print("Hello from feature branch")
>>>>>>> feature-branch
You must manually edit the file to resolve the conflict.
How to Resolve Git Conflicts
Open the conflicted file and choose the correct version (or combine both).
Remove the conflict markers (
<<<<<<<
,=======
,>>>>>>>
).Stage the resolved file:
git add app.py
Finalize the merge:
git commit
A merge conflict occurs when Git can't automatically reconcile differences between two commits. It usually happens when changes affect the same line in a file.
Example: Both you and a teammate modified the same function. On merging, Git will ask you to resolve the conflict manually.
git pull # May result in a conflict
Steps to resolve:
Open the conflicted files.
Edit them to keep the desired code.
Mark as resolved:
git add filename
- Then complete the merge:
git commit
What is a Git Tag?
Tags are used to mark specific points in history, commonly for release versions.
git tag v1.0.0 # Create a tag
git tag # List all tags
git checkout v1.0.0 # Checkout that tag version
Tags are especially useful in CI/CD pipelines for versioning and deployments.
Git Merge vs Git Rebase
Feature | git merge | git rebase |
Commit History | Preserved | Rewritten (cleaner) |
Use Case | Collaborative work | Linear history before merge |
Git Pull vs Git Fetch
Command | Action |
git pull | Fetch + merge into the working tree |
git fetch | Fetch only (requires separate merge) |
Final Thoughts
Mastering these Git commands has really improved the way I manage code. It’s not just about typing the right commands, but about understanding when and why to use them. Whether it's git stash
for temporarily shelving work or choosing between rebase
and merge
for cleaner histories, each command has a specific role in real-world workflows.
And with that, I’ve completed GitHub – another essential DevOps tool
Tomorrow, I’ll be diving into Bitbucket – excited for what’s next!
Happy learning and Git-ing!
Subscribe to my newsletter
Read articles from Vaishnavi D directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
