Why you should be using Git Branches in LS 101

A step by step guide to the magical world of Git Branching

A picture of code with git branches

Why you should be using git branching

While working through Launch School, you will end up working on games such as Rock Paper Scissors, Tic-Tac-Toe, and other similar projects. That wonderful moment finally arrives when your code is working as it should and you can happily move on to the next lesson. Then you see that you have to add some bonus features to the game. OK, that sounds like fun.

The magic of git branching

What if I told you there was an almost magical way to try out new things, add new features, and not have to worry about breaking your code in the process? Well, today is your lucky day. Thanks to the magic of git branching, you can try out new features and undo them at any time. You can restore your code to a working state with just a few short commands. So how does it work?

How to use git branches

[eamon@fedora lstimeestimate]$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean

Creating a new branch

To create a new branch, I type git checkout -b development into the console, and in response git prints the message: Switched to a new branch ‘development’. You can name the branch whatever you like, for the purposes of this article I’m using development.

Merging changes back to main

OK, that’s simple enough, but what if I add a new feature in the development branch and everything works as expected? In that case, I can merge the development branch into the main branch, which incorporates any changes I’ve made back into the main branch.

[eamon@fedora lstimeestimate]$ git merge development
Auto-merging app.js
Merge made by the 'recursive' strategy.
app.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

Conclusion

You can open as many branches as you like and you can even open different branches for different features, but that is beyond the scope of this article. For now, practice using git branches every time you add a new feature to your project, soon it will become second nature.

List of all the git commands contained in this article:

  • git add . — Stage your files, preparing them for the next commit.
  • git commit -m 'your message here' — Commit your changes, creating a snapshot of your code.
  • git checkout -b development — Creates a new development branch and switches to it. This is shorthand for the following two commands.
  • git branch development — Creates a new branch called development.
  • git checkout development — Switches you over to the development branch.
  • git checkout main — Switch back to the main branch.
  • git merge development — Merge the changes from development to the current branch.
  • git branch -d development — Delete the development branch.
  • git branch -D development — Force delete the development branch.
  • git status — Shows you the status of your current branch.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eamon O'Callaghan

I’m a Software Engineer working at S1SEVEN. My main programming language is TypeScript. I enjoy sharing what I’m learning by writing about it here!