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.
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 statusOutput example:
* feature/login
feature/dashboard
main2. Switch to an Existing Branch
git checkout feature/dashboard
# Switch to dashboard branch
# Or use simplified command
git switch feature/dashboardVerify switch result:
git branch
# Confirm current branch is feature/dashboard3. 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/loginNote: 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 tracking7. View Remote Branch Status
git branch -r
# View all remote branches
git remote show origin
# View remote repository branch info8. 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 remoteCommon 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 changes2. Resolve Merge Conflicts
git status
# View conflicted files
# Manually edit conflicted files (markers <<<<<<< ======= >>>>>>>)
git add <conflicted file>
git commit
# Commit merge result3. Delete Merged Local Branches
git branch -d feature/dashboard
# Delete merged branch
git branch -D feature/login
# Force delete unmerged branchThese operations are core daily Git skills; mastering them enables efficient multi-task development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
