Fundamentals 6 min read

Git Branch Switching & Commit Workflow: A Practical Case Study

This tutorial demonstrates a complete Git workflow for managing feature branches, including switching branches, adding code, committing changes, pushing to remote, merging to main, and handling stashes and conflicts with concrete command examples.

Lakehouse Research Base
Lakehouse Research Base
Lakehouse Research Base
Git Branch Switching & Commit Workflow: A Practical Case Study

Scenario

Assume you are developing a website and need to maintain two feature branches simultaneously: feature/login: login feature development feature/dashboard: dashboard feature development main: main branch for production deployment

Operation Command Summary

View and switch local branches

Add code and commit on different branches

Push local branches to remote repository

Handle stashing and conflicts when switching branches

Merge branches into main

Case Steps

1. View Current and All Local Branches

git branch
# View local branch list (current branch marked with *)
git branch -v
# View branches with last commit info
git status
# Confirm working tree status

Output example:

* feature/login
  feature/dashboard
  main

2. Switch to an Existing Branch

git checkout feature/dashboard
# Switch to dashboard branch
# Or use simplified command
git switch feature/dashboard

Verify switch result:

git branch
# Confirm current branch is feature/dashboard

3. Add Code and Commit on New Branch

Assume you add a new chart component on the dashboard branch:

# Create and edit new file
touch src/components/Chart.js
code src/components/Chart.js
# Edit file with VSCode (or other editor)
# Add and commit code
git add src/components/Chart.js
git commit -m "Add dashboard chart component"

4. Switch Back to Another Branch

git checkout feature/login
# Switch back to login branch
# Or
git switch feature/login

Note: If the current branch has uncommitted changes, you must stash or commit before switching.

5. Add Code on the Other Branch

Assume you add password encryption on the login branch:

# Modify file
code src/services/auth.js
# Add and commit code
git add src/services/auth.js
git commit -m "Add password encryption feature"

6. Push Local Branches to Remote Repository

# Push login branch
git push origin feature/login
# Push dashboard branch (first push)
git push -u origin feature/dashboard
# -u sets upstream tracking

7. View Remote Branch Status

git branch -r
# View all remote branches
git remote show origin
# View remote repository branch info

8. Merge Completed Feature into Main Branch

Assume dashboard feature is done and needs merging into main:

git checkout main
# Switch to main branch
git pull origin main
# Update main to latest state
git merge feature/dashboard
# Merge dashboard branch
git push origin main
# Push merge result to remote

Common Issues Handling

1. Keep Uncommitted Changes When Switching Branches

git stash
# Stash current changes
git checkout <target branch>
# Switch branch
git stash pop
# Restore stashed changes

2. Resolve Merge Conflicts

git status
# View conflicted files
# Manually edit conflicted files (markers <<<<<<< ======= >>>>>>>)
git add <conflicted file>
git commit
# Commit merge result

3. Delete Merged Local Branches

git branch -d feature/dashboard
# Delete merged branch
git branch -D feature/login
# Force delete unmerged branch

These operations are core daily Git skills; mastering them enables efficient multi-task development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

gitconflict resolutionversion-controlbranch managementmergingremote-repositorystashfeature branching
Lakehouse Research Base
Written by

Lakehouse Research Base

Focused on technical sharing in the data field, covering a tech stack that includes Hadoop, Spark, Flink, Kafka, Fluss, Paimon, Iceberg, StarRocks, ClickHouse, ES, Milvus, and more. Welcome to follow.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.