Bird
Raised Fist0
Gitdevops~10 mins

Feature branch workflow in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Feature branch workflow
Start on main branch
Create feature branch
Work on feature branch
Commit changes
Push feature branch to remote
Open Pull Request
Code Review & Approve
Merge feature branch into main
Delete feature branch
Update local main branch
End
This flow shows how a developer creates a separate branch for a feature, works on it, pushes it, and merges it back to main after review.
Execution Sample
Git
git checkout -b feature/login
# work on login feature
git add .
git commit -m "Add login feature"
git push origin feature/login
git checkout main
git merge feature/login
This code creates a feature branch, commits changes, pushes it, then merges it back to main.
Process Table
StepCommandActionBranchResult
1git checkout -b feature/loginCreate and switch to feature branchfeature/loginSwitched to new branch 'feature/login'
2git add .Stage changesfeature/loginChanges staged for commit
3git commit -m "Add login feature"Commit staged changesfeature/loginCommit created with message 'Add login feature'
4git push origin feature/loginPush feature branch to remotefeature/loginBranch 'feature/login' pushed to remote
5git checkout mainSwitch back to main branchmainSwitched to branch 'main'
6git merge feature/loginMerge feature branch into mainmainFeature branch merged into main
7git branch -d feature/loginDelete local feature branchmainDeleted branch 'feature/login'
8git push origin --delete feature/loginDelete remote feature branchmainDeleted remote branch 'feature/login'
💡 Feature branch merged and deleted, main branch updated with feature
Status Tracker
VariableStartAfter Step 1After Step 4After Step 6Final
Current Branchmainfeature/loginfeature/loginmainmain
Feature Branch Exists LocallyNoYesYesYesNo
Feature Branch Exists RemotelyNoNoYesYesNo
Main Branch UpdatedNoNoNoYesYes
Key Moments - 3 Insights
Why do we create a new branch instead of working directly on main?
Creating a feature branch isolates your work so main stays stable. See Step 1 where we switch to 'feature/login' branch before making changes.
What happens if you forget to push your feature branch?
Your changes stay local and others can't see them. Step 4 shows pushing the branch to remote, which shares your work.
Why delete the feature branch after merging?
To keep the repository clean and avoid confusion. Steps 7 and 8 show deleting local and remote feature branches after merge.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the current branch after Step 4?
Afeature/login
Bmain
Cdevelop
DNone
💡 Hint
Check the 'Current Branch' row in variable_tracker after Step 4
At which step is the feature branch merged into main?
AStep 3
BStep 6
CStep 2
DStep 8
💡 Hint
Look at the 'Action' column in execution_table for merging
If you skip Step 7 (deleting local branch), what happens?
ARemote branch is deleted automatically
BMain branch is deleted
CLocal feature branch remains, possibly causing confusion
DFeature branch is merged again
💡 Hint
Refer to Step 7 in execution_table and variable_tracker for branch existence
Concept Snapshot
Feature Branch Workflow:
- Start on main branch
- Create feature branch: git checkout -b feature/name
- Work and commit on feature branch
- Push feature branch: git push origin feature/name
- Open PR and merge into main
- Delete feature branch locally and remotely
- Update local main branch
Keeps main stable and isolates features.
Full Transcript
The Feature Branch Workflow starts by creating a new branch from main to work on a feature separately. You switch to this new branch, make changes, stage and commit them. Then you push the feature branch to the remote repository so others can see your work. After review, you merge the feature branch back into main. Finally, you delete the feature branch locally and remotely to keep the repository clean. This workflow helps keep the main branch stable and organized.

Practice

(1/5)
1. What is the main purpose of using a feature branch in Git?
easy
A. To merge all changes immediately without review
B. To delete the main branch permanently
C. To keep new work separate from the main code until it's ready
D. To create backups of the repository

Solution

  1. Step 1: Understand the role of feature branches

    Feature branches isolate new development work from the main codebase.
  2. Step 2: Identify the correct purpose

    This isolation allows safe development without affecting the stable main branch.
  3. Final Answer:

    To keep new work separate from the main code until it's ready -> Option C
  4. Quick Check:

    Feature branch purpose = isolation [OK]
Hint: Feature branches isolate new work from main code [OK]
Common Mistakes:
  • Thinking feature branches delete main branch
  • Assuming feature branches merge immediately
  • Confusing feature branches with backups
2. Which Git command correctly creates and switches to a new feature branch named feature-login?
easy
A. git branch feature-login
B. git push feature-login
C. git merge feature-login
D. git checkout -b feature-login

Solution

  1. Step 1: Understand branch creation and switching

    The command git checkout -b branch-name creates and switches to the new branch in one step.
  2. Step 2: Identify the correct command

    git checkout -b feature-login is the command that creates and switches to the new branch.
  3. Final Answer:

    git checkout -b feature-login -> Option D
  4. Quick Check:

    Create and switch branch = git checkout -b [OK]
Hint: Use git checkout -b to create and switch branch [OK]
Common Mistakes:
  • Using git branch without switching
  • Confusing git merge with branch creation
  • Trying to push before creating branch
3. Given the commands below, what will be the current branch after execution?
git checkout -b feature-ui
 git commit -m "Add UI"
 git checkout main
 git merge feature-ui
medium
A. feature-ui
B. main
C. detached HEAD
D. No branch (error)

Solution

  1. Step 1: Trace branch switching commands

    The commands switch to feature-ui, then back to main.
  2. Step 2: Understand merge and current branch

    After merging feature-ui into main, the current branch remains main.
  3. Final Answer:

    main -> Option B
  4. Quick Check:

    Last checkout branch = current branch [OK]
Hint: Last git checkout sets current branch [OK]
Common Mistakes:
  • Assuming merge switches branch
  • Confusing commit branch with current branch
  • Thinking merge detaches HEAD
4. You created a feature branch and made commits, but when you try to merge it into main, Git says "Already up to date." What is the likely problem?
medium
A. You are on the wrong branch when merging
B. Your working directory has uncommitted changes
C. The feature branch has diverged from main
D. You forgot to push the feature branch to remote

Solution

  1. Step 1: Analyze the error message context

    "Already up to date" means Git sees no new changes to merge.
  2. Step 2: Check the branch where merge is run

    If you run git merge feature-branch while not on main, it merges into the wrong branch or shows no effect.
  3. Final Answer:

    You are on the wrong branch when merging -> Option A
  4. Quick Check:

    Merge branch context matters [OK]
Hint: Always checkout main before merging feature branch [OK]
Common Mistakes:
  • Merging while on feature branch instead of main
  • Assuming push affects local merge
  • Ignoring branch status before merge
5. Your team uses feature branches. You finished a feature and want to merge it into main. Which sequence of commands correctly follows the feature branch workflow?
hard
A. git checkout main; git merge feature-branch; git push origin main; git branch -d feature-branch
B. git merge feature-branch; git checkout main; git push origin main
C. git checkout feature-branch; git push origin main; git merge main
D. git push origin feature-branch; git checkout main; git merge feature-branch

Solution

  1. Step 1: Switch to main branch before merging

    You must be on main to merge the feature branch into it.
  2. Step 2: Merge, push, and clean up

    After merging, push changes to remote and delete the feature branch locally to keep repo clean.
  3. Final Answer:

    git checkout main; git merge feature-branch; git push origin main; git branch -d feature-branch -> Option A
  4. Quick Check:

    Checkout main, merge, push, delete branch [OK]
Hint: Always merge feature into main, then push and delete branch [OK]
Common Mistakes:
  • Merging without switching to main
  • Pushing wrong branch
  • Not deleting feature branch after merge