Working with Git Branches!

Published Nov 26 2020 12:01 AM 4,677 Views
Microsoft

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 GitHubGit 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. ;)

%3CLINGO-SUB%20id%3D%22lingo-sub-1900867%22%20slang%3D%22en-US%22%3EWorking%20with%20Git%20Branches!%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1900867%22%20slang%3D%22en-US%22%3E%3CP%3ENow%20Git%20is%20something%20I%20never%20thought%20I%E2%80%99d%20have%20to%20learn%2C%20a%20few%20years%20ago%20if%20you%E2%80%99d%20suggest%20I%20learn%20Git%20I%20would%20have%20laughed%20and%20said%20I%20wasn%E2%80%99t%20a%20developer%2C%20however%20I%20probably%20use%20Git%20at%20least%20once%20a%20week%20now.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELast%20year%20I%20called%20out%20how%20you%20can%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fitops-talk-blog%2Fgetting-started-with-git%2Fba-p%2F905676%3FWT.mc_id%3Dmodinfra-10949-salean%22%20target%3D%22_blank%22%3Eget%20started%20with%20Git%3C%2FA%3E%2C%20so%20if%20it%E2%80%99s%20still%20something%20you%20aren%E2%80%99t%20sure%20about%20go%20check%20out%20that%20article.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20one%20thing%20I%20still%20kinda%20of%20struggle%20with%20within%20Git%2C%20and%20that%E2%80%99s%20branches.%26nbsp%3B%20I%20am%20that%20person%20that%E2%80%99s%20guilty%20of%20committing%20directly%20to%20the%20main%20branch%20of%20whatever%20repository%20I%20am%20working%20on.%26nbsp%3B%20Which%20has%20got%20me%20in%20trouble%20a%20few%20times%20with%20some%20things.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EWhat%20is%20a%20Git%20Branch%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EThe%20best%20way%20I%20can%20think%20of%20to%20describe%20Git%20Branches%20is%20it%E2%80%99s%20a%20way%20of%20tracking%20your%20changes%20while%20you%20make%20modifications%20or%20add%20features%20to%20the%20main%20project.%20%26nbsp%3BIt%E2%80%99s%20kind%20of%20a%20kin%20to%20printing%20out%20a%20document%2C%20making%20some%20changes%20with%20old%20fashion%20pen%20and%20paper%20and%20then%20getting%20approval%20from%20a%20peer%20before%20changing%20the%20main%20document.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20branch%20gives%20you%20a%20chance%20to%20play%20with%20changes%2C%20new%20features%20without%20breaking%20or%20%E2%80%9Cmessing%E2%80%9D%20with%20the%20main%20source%20of%20code%20or%20documentation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHow%20to%20work%20with%20a%20branch%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3ESo%2C%20let%E2%80%99s%20see%20branches%20in%20action%2C%20let%E2%80%99s%20look%20at%20GitHub%20and%20Git%20and%20working%20with%20branches.%3C%2FP%3E%0A%3CP%3EI%E2%80%99m%20taking%20a%20generic%2C%20test%20GitHub%20repository%20that%20I%20have%20for%20testing%20features%20and%20trying%20things%20out%20on.%26nbsp%3B%20It%E2%80%99s%20here%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fweeyin83%2FTest%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fweeyin83%2FTest%3C%2FA%3E%2C%20at%20the%20moment%20it%20only%20has%20one%20branch%20the%20main%20one%2C%20where%20everything%20is%20stored.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20first%20thing%20I%20am%20going%20to%20do%20is%20clone%20a%20copy%20of%20this%20repository%20to%20my%20local%20computer%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20clone%20https%3A%2F%2Fgithub.com%2Fweeyin83%2FTest.git%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20now%20have%20a%20copy%20to%20work%20with%20on%20my%20laptop.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20want%20to%20restructure%20the%20files%20and%20folders%20within%20this%20repository%2C%20so%20let%E2%80%99s%20create%20a%20new%20branch%20where%20I%20can%20do%20that%20with.%26nbsp%3B%20So%20within%20my%20Git%20Bash%20command%20interface%20I%20say%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20branch%20restructure%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20restructure%20being%20the%20name%20of%20the%20branch%2C%20I%20want%20to%20create%2C%20you%20can%20specify%20anything%20you%20want%20here.%26nbsp%3B%20To%20start%20working%20in%20this%20new%20branch%20I%20need%20to%20issue%20the%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20checkout%20restructure%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%E2%80%99m%20now%20no%20longer%20in%20the%20main%20branch%20so%20can%20make%20my%20changes.%20%26nbsp%3BIf%20I%E2%80%99m%20ever%20unsure%20of%20what%20branch%20I%20am%20working%20in%20I%20can%20run%20the%20command%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3Egit%20symbolic-ref%20%E2%80%93short%20HEAD%20or%20git%20branch%3C%2FSTRONG%3E%20and%20it%20will%20show%20me%20which%20branch%20I%20am%20working%20in.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20all%20my%20changes%20have%20been%20made%20and%20I%20am%20happy%20I%20can%20then%20issue%20the%20following%20commands%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20add%20.%0Agit%20commit%20-m%20%E2%80%9Cfolder%20restructure%20changes%E2%80%9D%0Agit%20push%20%E2%80%93set-upstream%20origin%20restructure%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThese%20three%20command%20add%20changes%20in%20the%20working%20directory%20(branch)%20to%20a%20staged%20area%2C%20commits%20the%20new%20changes%20to%20that%20branch%20and%20then%20pushes%20them%20up%20to%20my%20GitHub%20repository.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20within%20GitHub%20I%20can%20see%20that%20I%20have%20two%20branches%20to%20this%20repository%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22gitbranches.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F234414iE212411D2D32C897%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22gitbranches.PNG%22%20alt%3D%22Git%20Branches%20within%20GitHub%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EGit%20Branches%20within%20GitHub%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ENow%20that%20I%20am%20happy%20with%20my%20changes%20in%20the%20branch%20and%20I%20want%20to%20commit%20them%20all%20to%20the%20main%2Fdefault%20branch%20I%20need%20to%20do%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20checkout%20main%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20switches%20me%20back%20into%20the%20main%20branch%20of%20the%20repository.%20Now%20I%20can%20do%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-bash%22%3E%3CCODE%3Egit%20merge%20restructure%0Agit%20push%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhich%20merges%20the%20changes%20I%20made%20in%20the%20restructure%20branch%20and%20then%20push%20those%20new%20changes%20back%20into%20GitHub.%26nbsp%3B%20So%2C%20my%20main%20branch%20now%20looks%20just%20like%20the%20restructure%20branch.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EHow%20do%20you%20use%20branches%3F%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EAs%20I%20said%20at%20the%20start%20of%20the%20post%20I%E2%80%99ve%20made%20some%20mistakes%20in%20my%20main%20branch%20and%20had%20to%20scramble%20to%20get%20things%20working%20again%2C%20so%20branches%20are%20something%20I%20need%20to%20get%20Are%20branches%20a%20helpful%20feature%20when%20you%20are%20working%20with%20repositories%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20looking%20to%20learn%20more%20about%20Git%20and%20GitHub%20be%20sure%20to%20check%20out%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Flearn%2Fmodules%2Fintro-to-git%2F%3FWT.mc_id%3Dmodinfra-10949-salean%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3EMicrosoft%20Learn%3C%2FA%3E%2C%20where%20there%20is%20lots%20of%20hands%20on%20learning%20resources.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFeel%20free%20to%20comment%20below%20with%20your%20stories%20of%20join%20us%20on%20the%20%3CA%20href%3D%22https%3A%2F%2Faka.ms%2Fitopstalk-discord%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%22%3Eteam%20Discord%20server%3C%2FA%3E%20to%20chat%20about%20Git%2C%20Git%20mistakes%20and%20branches.%20%3B)%3C%2Fimg%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1900867%22%20slang%3D%22en-US%22%3E%3CP%3EWhat%20are%20Git%20branches%2C%20how%20do%20you%20create%20them%20and%20how%20do%20you%20push%20your%20changes%20back%20to%20the%20main%20branch%3F%20Sarah%20explains!%26nbsp%3B%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22workingwithbranches.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F234418iB3208EC899F350E8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22workingwithbranches.jpg%22%20alt%3D%22Working%20with%20Git%20Branches%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EWorking%20with%20Git%20Branches%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1900867%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESarah%20Lean%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Version history
Last update:
‎Nov 26 2020 03:20 PM
Updated by: