GitHub Branch Merge Workflow
Table of Contents
Flowchart #
graph TD
A["""
git switch -c dev
git push -u origin dev
"""]
A --> B["""
git add .
git commit -m 'msg'
git push
"""]
B --> C["""
git switch main
git pull
"""]
C --> D["""
git merge dev
"""]
D --> E{Merge conflicts?}
E -- No --> F["""
git push
"""]
E -- Yes --> G["""
git status
git add src/app.js
git commit -m 'msg'
"""]
G --> F
F --> H["""
git branch -d dev
git push origin --delete dev
"""]
How to Merge Branch? #
Step 1 - Create a new
devbranchgit switch -c dev # Create and switch to the dev branch git push -u origin dev # Push dev to remote and set upstream (first push only)Step 2 - Make changes on the
devbranchEdit your files normally, then stage and commit:
git add . git commit -m "Your commit message"Push updates to the remote:
git push # Push to origin/dev (since upstream is set)Step 3 - Merge dev into main
git switch main # Switch to the main branch git pull # Update main (local branch) with the newest code from the remote repository, to ensure main is up-to-date git merge dev # Merge the dev branch into mainStep 4 - Handle merge conflicts (if any)
If No Conflicts, skip to Step 5.
If conflicts exist, resolve them:
git status # Shows files with conflictsConflicted files (e.g.
src/app.js) will be shown.Open the file (e.g.
src/app.js) to see a conflict block like this:<<<<<<< HEAD content from main branch ======= content from dev branch >>>>>>> devReplace the entire block with the final version you want (can be main version, dev version, or a custom combined version).
Then:
git add src/app.js git commit -m "Resolve merge conflicts in app.js" # Commit the fixStep 5 - Push merged result
git pushStep 6. (Optional) Clean up
devgit branch -d dev # Delete local dev branch git push origin --delete dev # Delete remote dev branch
When Do Merge Conflicts Occur? #
| Conditions | Result | |
|---|---|---|
| Case 1 | 1. Create dev from main2. Changes only in dev (none in main)3. Merge dev → main | No conflicts |
| Case 2 | 1. Create dev from main2. Changes only in main (none in dev)3. Merge dev → main | No conflicts |
| Case 3 | 1. Create dev from main2. Changes in both dev and main3. Merge dev → main | Conflicts occur only if both branches modify the same or overlapping lines. Otherwise, no conflict. |
Git Terminology #
| Local | Remote | |
|---|---|---|
| Repository | Local repository: the Git repository stored on your computer. Local repository name: the folder name on your computer. Not important for Git internals. | Remote repository: the Git repository stored on a server (e.g., GitHub). Remote repository name: actual project name on GitHub (e.g., myproject).Accessed via a remote name (default: origin).Remote name: a local nickname for a remote repository URL (default: origin). It does not have to match the repository name. |
| Branch | Local branch: branch in your local repo (e.g., dev).Local branch name: name of that local branch. | Remote branch: branch stored in the remote repo, shown as <remote-name>/<branch-name> (e.g., origin/main, origin/dev).Remote branch name: name of that remote branch (same representation). |