How to use feature branches in Git and GitHub
Git feature branches are a workflow that involves creating separate branches for each feature you want to build. When a feature is finished, it gets merged into the central
This enables multiple features to be built in parallel on different branches, without overlapping over each other.
It also allows you to maintain a stable
master branch in case the feature gets thrown away or starts to ruin everything.
Creating the branch
First make sure you’re on the
master branch (if not, run
git checkout master to switch to it).
Then, create a new branch from
master and switch onto it (replace
<feature_branch>with the name you choose for your feature branch).
git checkout -b <feature_branch>
From here, you can do all your work as you would usually. Commit and push new changes for the feature to this new branch.
Merging the branch to master
Once you’re done with the your work on the branch and want to merge it into
master, run these four commands:
git checkout master
git pull origin master
git merge <feature_branch_name>
git push origin master
- Switch to the
- Make sure you have the most up-to-date changes from
- Merge your feature branch (replace
<feature_branch>with your branch’s name)
- Push the changes to the remote host (for example, GitHub)
Afterwards, you’ll probably want to delete the feature branch. These two commands first delete the branch on the remote host, then delete the branch locally.
git push -d origin <feature_branch>
git branch -d <feature_branch>