Git Branching

Updated on 26 Sep 2020

Part of git flow is that you would have separate branches for each of your main features that you are working on. When they’ve been coded and tested, they’ll be able to be merged back into your master branch. For semantics, the master branch could be the actual master branch, or it could be a different branch (i.e. dev, staging etc)

Anything to do with a branch is done with git branch and git checkout.

Create new branch

To create a new branch, we use the following command.

git checkout -b feature1

Not only will this create a new branch, but it will also switch to that branch in your workspace.

List Branches

To see a list of your branches, and which one you’re on,

git branch

Notice that from the previous command where we did the checkout -b, we are now on the branch that we created.

List branches that have been merged

We already know that git branch will list all of our branches, but what about seeing which branch(s) have already been merged? We can use the following flags:

  • --merged
  • --no-merged

make your first push

When you make your first push on a branch, you can also set the upstream to that branch. I.e. Your local repo will know what remote repo to look for changes on. It also means that subsequent pushes can be done with just git push instead of git push origin feature1. Setting the upstream is done with the -u flag.

git push -u origin feature1

  • -u is shorthand for –set-upstream. I.e. git push --set-upstream origin feature1

switching between branches

You can switch between branches using the git checkout command. In the screen shot below, I am switching from the feature1 branch back to the master branch.

git checkout master
git secret reveal -f

Important It is important to note that if you are using git-secret then you will also need to reveal the encrypted files. The reason for this is that there are no hooks for switching branches and your decrypted files will still be from the previous branch until you reveal them in the newly switched branch.

Feature 1

The first step that I made was to create the feature1 branch. Just incase I’m not certain which branch I’m on, I can also run git branch which will list all my branches, and place and astericks next to the branch I currently have checked out.

I have then made some changes on the code for this branch, and then pushed it onto the repo and set the upstream. Remember, -u is shorthand for --set-upstream, and is used to sync your local repo with the remote repo.

After I have tested the changes on the feature1 branch, it is time to merge it back into the master branch.

git checkout master
git merge feature1

So long as there are no conflicts, then the changes will be automatically merged in and I’ll get the screen shot as shown above. If not, I’ll get a warning and will have to deal with merging conflicts.

Feature 2

This one is a little bit more complex because I’m still working on feature2, while feature1 has already been merged with the master branch. What I want to do is refresh feature2 with the latest updates from the master branch before continuing to work with the feature.

Workflow:

  • checkout feature2
  • do some work
  • merge latest changes from master into feature2

So we already know how to checkout a new feature branch, and while we’re on that feature branch (and everything has been committed), we just need to do a merge.

On the feature2 branch

git merge master

Please note that this is only merging from your local repo. If the changes to the master branch have occurred from another developer and/or on the remote repo, then you’ll need to update your repos.

git checkout master
git pull

git checkout feature2
git merge master

Also note that this merge went smoothly because there were no conflicts. If there were conflicts, then we’d be informed and we’ll have to use git mergetool to deal with the conflicts.

Deleting a branch

Once your feature branches have been merged onto the master branch, it is time to delete the branch.

git branch -d feature1

However, it is important to note that this will only work IF the branch has already been merged into the master branch. If you need to delete a branch that hasn’t been merged, then use the -D action.

Deleting a branch 2

There can be cases where remote branches have been deleted, however they still show up in your local repo. In the example below, attempting to delete the groups branch fails because it doesn’t actually exist. Using the normal command from before doesn’t work. What will work is the --prune.

git fetch --prune