Git Practical Scenarios
This article shows common Git situations in a real team. These are the kinds of tasks developers handle every week in practical environments.
Scenario 1: A developer starts a feature branch
A developer needs to work on a new onboarding screen without disturbing the main branch.
git switch main
git pull origin main
git switch -c feature/onboarding-screen
Then they make the code changes and commit:
git add .
git commit -m "Add onboarding screen layout"
When ready:
git push -u origin feature/onboarding-screen
Then a pull request is opened for review and merge.
Why this is useful
It keeps the main branch cleaner and reduces the risk of unstable code being shared early.
Scenario 2: A teammate changes the same file
Two developers edit the same file at the same time.
Developer A changes a validation function. Developer B changes the same function for a related bug.
When both try to merge, a conflict may happen.
CONFLICT (content): Merge conflict in auth.go
The correct workflow is:
git pull origin main
git status
git add auth.go
git commit -m "Resolve conflict in auth validation"
Practical guidance
- read both versions carefully
- keep the behavior that is correct
- remove conflict markers
- test the result before finishing
Scenario 3: A hotfix must go to production immediately
A bug is discovered in production and must be fixed quickly.
git switch main
git pull origin main
git switch -c hotfix/fix-login-timeout
Then the fix is made and committed:
git add .
git commit -m "Fix login timeout validation"
Push and open a pull request for urgent review.
git push -u origin hotfix/fix-login-timeout
Why this matters
Hotfix branches keep urgent fixes separate from normal feature work and make release boundaries clearer.
Scenario 4: Cherry-picking a fix from main to a release branch
A production issue is fixed on the main branch, but the team also needs the same fix on a release branch for an older version currently being supported.
First, confirm the fix is committed on main:
git switch main
git log --oneline
Find the commit hash for the fix, for example:
abc1234 Fix validation bug in login flow
Now switch to the release branch:
git switch release/1.4
Cherry-pick the specific commit:
git cherry-pick abc1234
If there are conflicts, resolve them and continue:
git add .
git cherry-pick --continue
If the team decides not to keep the commit, it can be aborted:
git cherry-pick --abort
Why cherry-pick is useful
This is ideal when a fix is valid on main but should also be applied to a release branch without merging the entire feature branch or the whole development history.
Production note
Teams often use this pattern when they support multiple versions at the same time. It reduces risk because only the wanted fix is transferred.
Scenario 5: A commit was made by mistake
A developer commits a file that should not be included.
Undo the most recent commit but keep the local changes:
git reset --soft HEAD~1
Or undo the commit and keep the changes unstaged:
git reset --mixed HEAD~1
Or fully remove the last commit and all changes:
git reset --hard HEAD~1
Important rule
Use hard reset carefully. It removes local work permanently.
Scenario 6: A developer needs to temporarily store work
A developer is mid-way through a feature but must switch to another task quickly.
git stash
Then switch branches:
git switch main
Return the work later:
git stash pop
Why this helps
It prevents accidental commits and keeps the current work safe while the developer handles an urgent task.
Scenario 7: A feature branch is too old
A branch has not been updated for a long time. Before merging, it should be synced with main.
git switch feature/onboarding-screen
git fetch origin
git rebase origin/main
If conflicts appear, resolve them and continue:
git add .
git rebase --continue
Practical guidance
Rebasing is helpful when a branch is behind main and the team wants a cleaner history. Use it carefully on shared branches.
Scenario 8: A bad change needs a reversible rollback
A bug caused by a recent commit needs a safe rollback without rewriting history.
git revert <commit-hash>
This creates a new commit that reverses the previous change.
Why revert is preferred
It is safer than reset when the commit is already shared with other developers.
Common patterns in real teams
Most teams follow a pattern like this:
- work on feature branches
- commit often with clear messages
- push and open pull requests
- use CI checks before merge
- use release branches and cherry-pick for urgent production fixes
- prefer revert over reset when history is shared
Key takeaways
- Git is most useful when it is used as a workflow, not just as a command list.
- Cherry-pick is valuable when a fix should move to a different branch without the full branch history.
- Conflict resolution, rebase, revert, and stash are normal parts of real Git work.
- Production teams usually balance speed with safety by keeping branch boundaries clear and actions reversible.