Before reading this, I hope that you understand what you are trying to achieve with branching. It is explained in my guide: ‘What are ‘branches’?‘. If you’re new to branching, please make sure you’ve read that first.
Now, assuming you’re familiar with branching, I’m going to go ahead and show you how to do the following things with branches in Tortoise Git:
- Create a branch
- Checkout / Switch to a branch
- Push a new branch
- Merge a branch
- Delete a branch
To keep the text shorter, I will not be instructing you to right-click for bringing up your tortoise menu. I expect you to know how to do this already. I will simply type the shorthand version, such as “Go to ToirtoiseGit > Switch/Checkout“. This would mean I want you to right-click at the top-most folder of your git project in windows, and go to your Tortoise Git menu, and select Switch/Checkout.
Create a branch
- Go to TortoiseGit > Create Branch.
- When the dialogue appears, simply type a name for your new branch, and hit the OK button. Note that professional practice would instruct that your first new branch should be named development, with the expectation that you then make a new branch from development to work on.
Checkout / Switch to a branch
Your branch exists, but we aren’t using it yet. Go to TortoiseGit > Switch/Checkout and in the dialogue, select your new branch. This is how you switch branches.
Push a new branch
No, don’t push your computer over. That’s not what push means when we are talking about git. Push is simply what you do to take changes on your local repository (on your coputer) and push those changes to your remote repository (like gitlab).
Remember, we have created a new branch, and even switched to it. But this branch still only exists on your local repository. Your team members can’t access it, and if you try to work from a different computer, neither can you!
You need to push this new branch to the remote repository. To do that, go to TortoiseGit > Push. When the dialogue appears, simply push the OK button.
Note that this is identical to trying to push when you have committed your work. If you’ve followed my setup guide correctly, Getting Started with Tortoise Git for windows, then this should have worked successfully, with blue text, NOT RED:
If you have used GitLab as your server, like I suggested in Getting Started, then for your own peace of mind, you can also confirm the existence of your new branch by logging in to your GitLab account, clicking on your project, and viewing your branches. I won’t tell you how to do this, because I bet you can figure it out. But here’s what mine looks like on GitLab:
Not a necessary step, but this way you know for sure that the branch you created is accessible from places outside of your own computer.
So, now that you have a new branch, you can do THIS magic:
Did that not seem magical? Let me explain.
Some files have been created on the development branch, committed and then pushed. In the above GIF, you can see what my project folder looks like when I switch branches. Only the development branch has the 3 extra text files, but when we switch to the master branch, we can see that those files do not exist on master.
Hopefully, the above GIF demonstrates how branches are copies of your project, but with slight differences. So, what if you LIKE the changes on development, and feel like you need these same changes on the master branch? Then, you need to merge your branches!
Merge a branch
This process will take differences from another branch, and apply them to whichever branch you are currently working on. To make this magic happen, follow these steps:
- Go to TortoiseGit > Switch/Checkout to make sure you are on the branch that wishes to RECEIVE changes. Such as the master branch, as it wishes to receive changes from development. When the dialogue appears, simply push the OK button.
- After you’ve switched to your new branch, go to TortoiseGit > Merge. When the dialogue shows up, simply select the branch you wish to receive changes from. In this case, I choose development.
Note that the remote versions of these branches are also selectable, this is because you can merge directly from your server, meaning you don’t have to Pull changes to your local branch, or even have the branch on your machine in order to merge. You can simply grab the changes directly from the remote repository (AKA the server, AKA GitLab).Once you have successfully merged, you may need to commit and push these changes. As the merge will only have happened on your local repository, not on the remote repository.
That’s it. You’ve mastered merging. Grats.
Deleting a branch
Lastly, you’ll want to occasionally delete a branch. This is especially true if you have merged changes from a branch that you don’t need to keep anymore. Unfortunately with Tortoise, this is one of the LEAST straightforward features I have found (it is easier in OTHER Git clients, but not in Tortoise). I’m going to be lazy here and just copy someone else’s blog post for this (I would love to give credit for this, but as I found this on stackoverflow.com, ALSO un-credited, I have no idea who to credit for this… thanks though).
…remove the local branch by first opening up the
Checkout/Switch dialog to get at the
Browse refs dialog we can right click on the local branch and choose to delete it.
To delete a remote branch we can do the same thing, but instead of right clicking on our local branch we expand the remotes tree in the left part of the dialog and then locate the remote branch.
I believe the above steps can also be used to delete branches on your remote, which, by the way, you can also do via some buttons on Gitlab.com.
And that’s all there is to know about branching in Tortoise Git! This is also the final topic I intend to cover for now. At this stage, if you have followed and understood my Tortoise Git guides up to this point, great! Also, I’d happily develop some software or games or complex art with you.