Blog Post

ITOps Talk Blog
3 MIN READ

Working with Git Branches!

techielass_ms's avatar
techielass_ms
Icon for Microsoft rankMicrosoft
Nov 26, 2020

Now Git is something I never thought I’d have to learn, a few years ago if you’d suggest I learn Git I would have laughed and said I wasn’t a developer, however I probably use Git at least once a week now.

 

Last year I called out how you can get started with Git, so if it’s still something you aren’t sure about go check out that article.

 

There is one thing I still kinda of struggle with within Git, and that’s branches.  I am that person that’s guilty of committing directly to the main branch of whatever repository I am working on.  Which has got me in trouble a few times with some things.

 

What is a Git Branch?

The best way I can think of to describe Git Branches is it’s a way of tracking your changes while you make modifications or add features to the main project.  It’s kind of a kin to printing out a document, making some changes with old fashion pen and paper and then getting approval from a peer before changing the main document.

 

A branch gives you a chance to play with changes, new features without breaking or “messing” with the main source of code or documentation.

 

How to work with a branch?

So, let’s see branches in action, let’s look at GitHub and Git and working with branches.

I’m taking a generic, test GitHub repository that I have for testing features and trying things out on.  It’s here https://github.com/weeyin83/Test, at the moment it only has one branch the main one, where everything is stored.

 

The first thing I am going to do is clone a copy of this repository to my local computer

 

 

 

git clone https://github.com/weeyin83/Test.git

 

 

 

I now have a copy to work with on my laptop. 

 

I want to restructure the files and folders within this repository, so let’s create a new branch where I can do that with.  So within my Git Bash command interface I say

 

 

 

git branch restructure

 

 

 

With restructure being the name of the branch, I want to create, you can specify anything you want here.  To start working in this new branch I need to issue the command:

 

 

 

git checkout restructure

 

 

 

I’m now no longer in the main branch so can make my changes.  If I’m ever unsure of what branch I am working in I can run the command

 

git symbolic-ref –short HEAD or git branch and it will show me which branch I am working in.

 

Once all my changes have been made and I am happy I can then issue the following commands

 

 

 

git add .
git commit -m “folder restructure changes”
git push –set-upstream origin restructure

 

 

 

These three command add changes in the working directory (branch) to a staged area, commits the new changes to that branch and then pushes them up to my GitHub repository.

 

Now within GitHub I can see that I have two branches to this repository

Git Branches within GitHub

Now that I am happy with my changes in the branch and I want to commit them all to the main/default branch I need to do the following:

 

 

 

git checkout main

 

 

 

This switches me back into the main branch of the repository. Now I can do:

 

 

 

git merge restructure
git push

 

 

 

Which merges the changes I made in the restructure branch and then push those new changes back into GitHub.  So, my main branch now looks just like the restructure branch.

 

How do you use branches?

As I said at the start of the post I’ve made some mistakes in my main branch and had to scramble to get things working again, so branches are something I need to get Are branches a helpful feature when you are working with repositories?

 

If you are looking to learn more about Git and GitHub be sure to check out Microsoft Learn, where there is lots of hands on learning resources. 

 

Feel free to comment below with your stories of join us on the team Discord server to chat about Git, Git mistakes and branches. 😉

Updated Nov 26, 2020
Version 6.0
No CommentsBe the first to comment

Share