Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Git: Checking Out a Branch Off Of Another Branch

9/23/2016

0 Comments

 
This is something I've had to do quite a few times, but it wasn't until I worked on a bunch of Github projects at HBO that I really started to get understanding branching in git. In my own Github projects I would often work right out of the master branch. This is fine for developers who are just getting started with git, but when working on large, enterprise projects you really want to be smart about using branches. This post will help you understand making git branches off of existing branches.
Picture
A Google drawing I made that visualizes branching off of another branch.

Let's begin with this repository here, Park-Boys-Bootcamp:
https://github.com/ParkBoys/Park-Boys-Bootcamp

Of course, the first thing to do is to clone the repository:
git clone https://github.com/ParkBoys/Park-Boys-Bootcamp.git
Then you can checkout an existing branch with the checkout​ command: 
git checkout my-branch
Picture
If the branch already exists then you can checkout that branch directly right when you run the clone command with the --branch or shorthand -b flag:
git clone -b HJ/chapter-1 https://github.com/ParkBoys/Park-Boys-Bootcamp.git
Note that you can't create a new branch right from the clone command. If you try to do this it will complain, "fatal: Remote branch ____ not found in upstream origin": 
Picture
If you want to create a new branch, you can first run git clone without the branch flag and then run the checkout command. With the checkout command we can add the --branch or shorthand -b flag to create a new branch:
git checkout -b my-branch-new-leaf-branch my-root-branch
The first argument here is the name of the new branch. The second argument is the name of the branch that defines the files and folders in the new branch. 
This above command works, but there are some subtle less-than-optimal characteristics about checking out a branch this way. When you do git push or git pull it doesn't know exactly which branch is the "upstream branch" (ie. the one that you are pulling from / pushing to).
Also, when we print the branches with the branch command and the "double verbose flag" ( -vv ) it gives us a less than optimal description. Take a look at the screenshot below. The branch my-new-leaf-branch was checked out without tracking, and so it does not show a tracking branch in blue. However, consider the branch flipflop. We can see clearly (from the corresponding blue text) that flipflop is tracking boobledoop and thus when we are on the flipflop branch and simply type git pull or git push then git will automatically know to try to do the operation with the boodledoop branch. Also, just having the connections between branches listed out like this makes adding the tracking well worth it.
Picture
I use the -vv command often if I want to see what's tracking what.
git branch -vv

Tracking

Here's a nice article / tutorial on tracking branches. Basically, tracking tells git that this branch is linked to the branch that it's branching off of. But what do I mean by linked? Well, When you run push and pull in the new branch, it will by default try to push to and pull from the branch that you are tracking. This makes everything a lot nicer. You can checkout with tracking by adding the flag --track or it's shorthand form -t. 
​So, after all the background knowledge of my why is how it is, here's how I recommend creating a new branch called super-cool-new-branch off of the branch named base-branch: 
git checkout -tb super-cool-new-branch base-branch
But make sure you do "-tb" and not "-bt" since the second way will give an error:
Picture
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Picture
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    March 2023
    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2023