0
0
Gitdevops~5 mins

Feature branch workflow in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When working on a new feature, you want to keep your changes separate from the main code. The feature branch workflow helps you do this by creating a separate branch for your work. This way, the main code stays stable while you develop.
When you want to add a new feature without affecting the main code immediately
When multiple developers work on different features at the same time
When you want to test your changes before merging them into the main branch
When you want to keep your work organized and easy to manage
When you want to review code changes before they become part of the main project
Commands
Create and switch to a new branch called 'feature-login' to start working on the login feature separately.
Terminal
git checkout -b feature-login
Expected OutputExpected
Switched to a new branch 'feature-login'
-b - Creates a new branch and switches to it immediately
Stage all the changes you made in your working directory to prepare them for commit.
Terminal
git add .
Expected OutputExpected
No output (command runs silently)
Save your staged changes with a clear message describing the new login feature work.
Terminal
git commit -m "Add login feature implementation"
Expected OutputExpected
[feature-login abc1234] Add login feature implementation 3 files changed, 45 insertions(+), 2 deletions(-)
-m - Adds a commit message inline
Switch back to the main branch to prepare for merging your feature branch.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merge the changes from your feature branch into the main branch to include the new login feature.
Terminal
git merge feature-login
Expected OutputExpected
Updating def5678..abc1234 Fast-forward login.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+)
Key Concept

If you remember nothing else from this pattern, remember: use separate branches to keep new work isolated until it's ready to join the main code.

Common Mistakes
Committing changes directly on the main branch
This can cause unstable code in the main branch and affect others working on the project.
Always create and work on a feature branch before merging changes into main.
Not switching back to main before merging
Merging while on the feature branch merges main into feature, not the other way around.
Switch to main branch first, then run the merge command.
Not committing changes before switching branches
Uncommitted changes can cause conflicts or be lost when switching branches.
Always commit or stash your changes before switching branches.
Summary
Create a new branch for your feature with 'git checkout -b feature-name'.
Make changes, stage them with 'git add', and commit with 'git commit -m'.
Switch back to the main branch using 'git checkout main'.
Merge your feature branch into main with 'git merge feature-name'.