Josh A. Young
Software Engineer

Using Git Feature Branches

Below I have outlines the steps necessary to create a feature branch and use this branch for all of your work before merging to your main or master branch. In this case, my "feature" branch is the branch that I have initially created from my default branch (main/master) and used for all of my subsequent branches. I find it simpler to name this branch something like TICKETNUMBER-main or TICKETNUMBER-master. This way I know that it is my new default branch going forward.

Since my default branch is named main, I will be referring to it as main going forward. In your case, it could be master.

Start by creating a new branch from the default branch (main). Then push that branch up to your remote.

git checkout main
git checkout -b "TICKET-123-main"
git push -u origin TICKET-123-main

From this new default branch, checkout your first branch and push that up.

git checkout -b "TICKET-123-a"
git push -u origin TICKET-123-a

Then, make your necessary changes. Add your commits, and then push up your changes.

git add . 
git commit -m "Remove unneeded Student model tests"
git push

At this point, you will need to switch to GitHub and start the process to open your PR. By default GitHub will assume you want to compare your new branch against main.

So before you open your PR, you will need to change your base branch to be your new default branch TICKET-123-main.

Once your PR has been approved, you will be able to merge it. Because you changed the base branch, you will be merging your commits into your new base branch instead of main.

If you want to push additional changes to this feature branch, continue to follow these steps. See commands for another branch below.

# Pull the changes that you just merged into this branch:
git pull TICKET-123-main

git checkout TICKET-123-main

# Checkout a new branch off of TICKET-123-main:
git checkout -b "TICKET-123-b"

git push -u origin TICKET-123-b

# make your changes and add your commit(s)

git add . 
git commit -m "More changes"
git push

Change your base branch back to TICKET-123-main and open up your next PR. Once your have received approval from your team, merge this change into your feature branch (TICKET-123-main).

After all of your changes haven been completed, you will need to merge your TICKET-123-main branch back into main. This should work as normal in GitHub by switching to the repo page and opening up a PR for your TICKET-123-main branch. By default, GitHub will use the main branch as the base branch. This diff should contain all of the code for the individual commits (TICKET-123-a, TICKET-123-b) you merged into your feature branch (TICKET-123-main). At this point, you can open the final PR to merge TICKET-123-main into main.

After you are done with all of the merges to your feature branch (TICKET-123-main), you will be able to merge a single PR into main. Therefore, you will have a cleaner commit history for your main branch instead of a whole bunch of small incomplete commits.

Last Updated: December 27, 2021
Icons made by Freepik and Pixel perfect from www.flaticon.com. Also icon(s) from iconfinder.
"Nōn nōbīs, Domine, nōn nōbīs, sed nōminī tuō dā glōriam."