Working directly on the main branch feels faster right up until it does not. A half-finished feature sits in the same code as the live site, an urgent fix has to be threaded through incomplete work, and two developers editing the same files overwrite each other rhythm. Feature branches solve this by giving every piece of work its own isolated line of history. You build the feature on its branch, where it can be broken, experimental, and unfinished without affecting anyone, and you merge it into main only when it is complete and reviewed.
This guide walks the whole workflow step by step: starting from an up-to-date main, creating and naming a branch, committing on it, pushing it, merging it back, and cleaning up afterwards. It assumes the everyday commands are familiar; if they are not, the Git basic commands guide covers each one individually before you combine them here.
Step 1: Start From a Fresh Main
Every feature branch should start from the latest version of main. A branch created from a week-old main begins life already behind, and the gap shows up later as extra conflicts and surprise changes landing in your merge. So the workflow always opens with the same short routine: switch to main, pull the newest commits, and confirm a clean state.
git switch main git pull git status
Line by line: switch moves you onto the main branch, updating your files to match it. Pull downloads any commits teammates have pushed and merges them into your local main, so your starting point is current. Status confirms there are no leftover uncommitted changes from earlier work; if it does show changes, commit them, stash them, or discard them deliberately before going further. Branching with a dirty tree carries those stray changes into the new branch, where they mix with the feature and complicate the eventual review. Thirty seconds of tidying here saves real confusion later.
Step 2: Create and Name the Branch
With main fresh, create the branch and switch to it in one command. The name matters more than it seems. A branch name is a label your whole team will read in lists, pull requests, and deployment tools, so it should say what the work is, briefly and consistently.
git switch -c feature/search-filters git branch
The c flag tells switch to create the branch as it moves you onto it, starting from your current commit, which thanks to step one is the tip of main. The second command lists branches so you can confirm you are on the new one, marked with an asterisk. For naming, a widely used convention prefixes the type of work: feature for new functionality, fix for bug repairs, and chore for maintenance such as dependency updates, followed by a short hyphenated description. Names like feature/search-filters and fix/login-redirect tell a reader the purpose at a glance, while names like johns-branch or temp2 say nothing a week later. Keep names lowercase, use hyphens instead of spaces or underscores, and resist including dates or issue numbers alone without a description, because the description is the part humans actually remember.
Step 3: Work and Commit on the Branch
Now do the work, in the ordinary loop of edit, stage, and commit, exactly as you would anywhere else. The difference is psychological and practical: on a feature branch, commits are cheap and private, so commit in small logical steps as the feature grows. Add the form, commit. Wire up the filtering logic, commit. Style the results list, commit. Each commit is a checkpoint you can return to, review, or drop individually.
git status git add search.html search.css git commit -m "Add search filter form" git add search.js git commit -m "Filter results by category and price"
Notice the staging discipline in this example. Files are added by name in groups that belong together, rather than sweeping everything into every commit, so each commit tells one part of the story. The messages describe the outcome, not the activity: add search filter form, not worked on search. If you make a mistake mid-feature, the branch is the safest place to fix it, using the restore and reset techniques from our guide to undoing changes in Git, because nothing here has been shared yet and history can be tidied freely. One caution: never let a feature branch live for weeks while main moves on without you. The longer the gap, the harder the final merge. For large features, pull main into your branch every day or two, resolving small differences while they are small.
Step 4: Push the Branch and Open It for Review
A branch that exists only on your machine is invisible to your team and unprotected if your disk fails. Push it to the remote early, ideally on the first day, and keep pushing as work progresses. Pushing a branch does not merge or publish anything to the live code; it simply stores a copy of your branch on the shared server, where teammates can see it and review tools can pick it up.
git push -u origin feature/search-filters git push
The first push carries extra information: the u flag links your local branch to the remote branch of the same name and remembers that link, and origin names the remote. After that first push, plain git push with no arguments knows where to send updates, which is why the second line is so short. On a hosting platform, pushing a new branch usually prompts the option to open a pull request, also called a merge request, which is the formal proposal to merge your branch into main. The pull request page shows the accumulated diff of the whole feature, gives reviewers a place to comment line by line, and often runs automated checks. Even when working alone, opening the request is a useful final self-review: seeing the complete change in one view regularly catches leftover debug output and files that wandered in by accident.
Step 5: Keep the Branch Current
While your feature is in progress, main keeps moving as teammates merge their own work. Before your branch can merge cleanly, it should include those updates. There are two standard ways to bring main into your branch: merging main into the feature branch, or rebasing the feature branch onto main. For a straightforward team workflow, merging is the safer default to learn first.
git switch feature/search-filters git fetch git merge main
Here you make sure you are standing on the feature branch, fetch downloads the latest remote state so your local main information is current, and merge main brings those commits into your branch. If the same lines were changed in both places, Git reports conflicts: open each conflicted file, find the marked sections, edit them into the correct combined version, then stage the file and commit to complete the merge. Resolving conflicts on your own branch, at a time you choose, is far calmer than discovering them during the final merge into main. Rebasing, the alternative, replays your feature commits one by one on top of the latest main, producing a straighter history. It is a valuable technique, but it rewrites your commits, so never rebase a branch that teammates have based work on, and learn it after the merge-based flow feels routine.
Step 6: Merge Into Main
When the feature is complete, reviewed, and current with main, merging it back follows the mirror image of the earlier steps. You switch to main, make sure main is up to date, merge the feature branch, and push the result so the shared repository reflects the new state.
git switch main git pull git merge feature/search-filters git push
Read the order carefully, because merging in the wrong direction is a classic slip. You always stand on the receiving branch and name the branch whose work should come in. Standing on main and merging the feature pulls the feature into main, which is the goal. The final push publishes the merge; until it runs, the merge exists only locally and your team still sees the old main. In many teams the merge itself happens through the pull request interface on the hosting platform rather than in the terminal, which achieves the same result and records the review alongside it. Either way, the local terminal steps after that are identical: pull the updated main so your copy matches. Teams managing projects on their own servers often pair this flow with the remote access basics in our SSH cheat sheet.
Step 7: Delete the Branch and Tidy Up
A merged feature branch has done its job, and keeping it only clutters branch lists and invites someone to base new work on stale code by mistake. Delete it both locally and on the remote as the final step of the workflow.
git branch -d feature/search-filters git push origin --delete feature/search-filters git branch
The d flag deletes the local branch, and Git protects you here: it refuses to delete a branch whose commits are not merged somewhere, so an accidental deletion of unmerged work is blocked. A capital D variant forces deletion, and it should be used only when you are deliberately discarding a branch. The second line removes the branch from the remote so teammates stop seeing it, which hosting platforms often do automatically when a pull request is merged through their interface. The final list command confirms the tidy state: main, plus whatever features are genuinely in progress. Make this cleanup a non-negotiable habit. A repository with five branches tells you exactly what the team is doing; one with fifty tells you nothing, and our Linux commands roundup makes the same tidiness argument for files and processes on the machine itself.
Handling Common Branch Situations
A few situations recur in every team. The first is the urgent hotfix: production is broken while your feature is half done. The workflow handles it gracefully. Commit or stash your feature work, switch to main, create a fix branch from main, repair, merge, push, then return to your feature branch and merge the fresh main into it so the fix is included in your work too. Because the feature was isolated, the fix never had to touch it directly.
The second is discovering you committed to main by accident, before pushing. The repair is gentle: create a feature branch right where you stand, which captures those commits under the branch name, then reset main back to match the remote. The commits are never lost because the new branch points at them. The reset mechanics are detailed in the undo guide, and attempting this is a good reminder of why checking your current branch with status before committing is worth the second it takes.
The third is the long review. When a pull request waits days for feedback, keep merging the latest main into the branch so it never drifts far, and respond to review comments with new small commits on the same branch; pushing them updates the request automatically. Resist starting the next feature from an unmerged branch unless the new work truly depends on it, because stacked branches multiply merge work when the first one changes during review.
Team Rules That Make the Workflow Work
The mechanics are only half the system. The other half is a few agreements every team member follows. Main is always deployable: nobody merges broken or half-finished code into it, which is the entire reason features live elsewhere until they are ready. Branches are short-lived, measured in days, not weeks, so merges stay small. Every branch gets reviewed by at least one other person when the team size allows it, and the author, not the reviewer, performs the merge once approved. Commit messages and branch names follow the shared conventions from steps two and three, so history reads like one project rather than five personal styles. None of these rules require special tooling. They require consistency, and consistency is what turns branching from a feature of Git into an actual workflow.
Conclusion
The feature branch workflow is a short loop repeated for every piece of work: refresh main, branch with a clear name, commit in small steps, push early, keep the branch current, merge back when reviewed, and delete the branch. Each step is a basic command, but the order and the discipline are what protect the main branch and keep a team moving in parallel without collisions. Adopt the loop for your next feature, including the cleanup, and within a few cycles it becomes automatic: main stays stable, work stays visible, and merging turns from an event into a routine.
Tagged: feature branches git branching git merge git tutorial git workflow version control



2 thoughts on “Git Branching Workflow: Feature Branches Step by Step”