Branches

Git - Quick Reference

3 min read

Published Jun 19 2025


7
0
0
0

CLIGit

It is useful to create a branch to work on a new feature. Then you can develop and test the branch with the option of dropping the changes completely if it doesn't work out, or merging it back in to develop if everything is good.


List all branches:

git branch -a

Displays a list of all branches, with a * next to the current branch.

Example output:

* develop
  main
  remotes/origin/develop
  remotes/origin/main

Create a new branch:

git checkout -b my-branch-name

The -b flag creates the branch.

The new branch will also be auto selected after creation.

This will create the branch with a starting point of whatever branch you currently have checked out. eg. if you have develop checked out and run the above command, the branches starting point will be the same commit as develop currently is on.


Select a branch

git checkout my-branch-name

Make changes in the new branch as normal and commit as normal:

git add .
git commit -m "my commit message"

If you want to push the branch to the remote respository:

git push -u origin my-branch-name

This only needs doing the first push, after that you can just use git push.


If you want to just keep the branch locally, you can just use:

git push



Merging this branch back in to develop

First switch to the develop branch:

git checkout develop

Then merge the branch in to develop:

git merge my-branch-name

Then push these changes to develop:

git push



Deleting the branch

First delete the remote one if you have one:

git push -d origin my-branch-name

Then delete the local branch:

git branch -d my-branch-name

The -d means delete the branch




Bringing in develop branch changes

If other changes have been done to the develop branch, where the feature branch originated from, and you are still working on the feature branch, then there are 2 ways you can bring the develop branch updates in to the feature branch.


1. Merge develop into your feature branch:

Make sure you have the feature branch selected:

git checkout my-branch-name

Fetch the latest changes from the remote:

git fetch origin

Merge the updated develop into the feature branch:

git merge origin/develop
  • Preserves history - you will see the merge commit.
  • Good for teams that want to keep full context.
  • Might require resolving merge conflicts if the same code has been edited in both branches.
    • Edit each of the conflicted files and then add them to mark as resolved git add thefilename.txt.
    • Once all resolved complete the merge with git commit.


2. Rebase your feature branch onto latest develop:

Make sure you have the feature branch selected:

git checkout my-branch-name

Fetch the latest changes from the remote:

git fetch origin

Rebase your feature branch onto the latest develop

git rebase origin/develop
  • Cleaner history - no merge commits.
  • Rewrites your branch's commits as if they were made after the latest develop.
  • Need to be careful if the feature branch has been shared with others.
  • If there are merge conflicts:
    • Fix the file and mark as resolved git add thefilename.txt.
    • Continue the rebase git rebase --continue.
    • Or abort the rebase git rebase --abort.


After either of the above methods:

If you have already pushed your feature branch to the remote and you rebased, you'll need to force-push:

git push --force-with-lease

If you merged, just do a regular push:

git push


Products from our shop

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet - Print at Home Designs

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Mouse Mat

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Travel Mug

Docker Cheat Sheet Mug

Docker Cheat Sheet Mug

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet - Print at Home Designs

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Mouse Mat

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Travel Mug

Vim Cheat Sheet Mug

Vim Cheat Sheet Mug

SimpleSteps.guide branded Travel Mug

SimpleSteps.guide branded Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript - Travel Mug

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Dark

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Embroidered T-Shirt - Light

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - White

Developer Excuse Javascript Mug - Black

Developer Excuse Javascript Mug - Black

SimpleSteps.guide branded stainless steel water bottle

SimpleSteps.guide branded stainless steel water bottle

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Light

Developer Excuse Javascript Hoodie - Dark

Developer Excuse Javascript Hoodie - Dark

© 2025 SimpleSteps.guide
AboutFAQPoliciesContact