4. Branching

Using branches to manage complex projects

What does git do?

  • When we work with git, we bundle up changes in our project folder (/directory) and commit our changes.
  • Each commit (bundle of changes) gets a unique id - a hexadecimal hash that’s 40 digits long (we’re just going to abbreviate to the first 7)
  • The commits are made to a “branch” - pause any thinking for a moment

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"

What does git do?

  • When we work with git, we bundle up changes in our project folder (/directory) and commit our changes.
  • Each commit (bundle of changes) gets a unique id - a hexadecimal hash that’s 40 digits long (we’re just going to abbreviate to the first 7)
  • The commits are made to a “branch”

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"

  • Because each “bundle” of changes has been saved with a unique id, we can roll back our changes to a previous version if we want

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"

  • But let’s say we’re happy with how our code is working, but we want to try out a different way of doing something
  • Or say we’ve written the conclusion section of our paper in a certain way, but our supervisor has some ideas for structuring it differently

How can we try this out without risking our current work that we’re happy with?

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"

The answer is branching

  • We mentioned earlier that your bundled changes (commits) were on the main branch
  • We can create other branches to try out experimental changes while keeping our main branch safe

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"

The answer is branching

  • When we are happy with the changes we have made on the experimental branch, we can decide to mix them back in with our main branch
  • We can merge the changes with the main branch

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"

The answer is branching

  • When we are happy with the changes we have made on the experimental branch, we can decide to mix them back in with our main branch
  • We can merge the changes with the main branch
    • This merge get’s it’s own unique id

Remember that you can always reverse to a previous commit, even across different branches!

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc"
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"

  • We can continue committing bundles of changes, and making new branches that support us taking risks with our work

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc"
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"
   branch experimental-3
   checkout experimental-3
   commit id: "456abc1"

  • We can create branches from other branches, if we want to noodle around with changes

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc"
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"
   branch experimental-3
   checkout experimental-3
   commit id: "456abc1"
   checkout experimental-2
   commit id: "ad1cb45"

  • We can create branches from other branches, if we want to noodle around with changes
    • We can then abandon those branches if we realise we made terrible choices, and keep working on the original branch like nothing happened…

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc"
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"
   branch experimental-3
   checkout experimental-3
   commit id: "456abc1"
   checkout experimental-2
   commit id: "ad1cb45"
   checkout main
   merge experimental-2 id: "be34af1"
   commit id: "def134a"
   commit id: "563bdef"

  • And we usually bring everything back to the main branch once we are happy with it

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc" tag:"v0.1.0 - preprint" type: HIGHLIGHT
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"
   branch experimental-3
   checkout experimental-3
   commit id: "456abc1"
   checkout experimental-2
   commit id: "ad1cb45"
   checkout main
   merge experimental-2 id: "be34af1"
   commit id: "def134a"
   commit id: "563bdef" tag:"v1.0.0 - published paper" type: HIGHLIGHT

  • We can also tag specific commits if the code at that point in time is important!
    • For example, the version of the code you used to generate results for a preprint or the final paper!

What does git do?

%%{
  init: {
    'theme': 'base',
    'themeVariables': {
      'primaryColor': '#9fe1ff',
      'primaryTextColor': '#470044',
      'primaryBorderColor': '#000000',
      'lineColor': '#9158A2',
      'secondaryColor': '#e79aff',
      'tertiaryColor': '#fffc58'
    }
  }
}%%

gitGraph
   commit id: "a1b2c3d"
   commit id: "4e5f678"
   branch experimental-1
   checkout experimental-1
   commit id: "90abcde"
   commit id: "7835cd3"
   checkout main
   merge experimental-1 id: "df37ba1"
   commit id: "34efc1a"
   commit id: "32753bc" tag:"v0.1.0 - preprint" type: HIGHLIGHT
   branch experimental-2
   checkout experimental-2
   commit id: "cb45ad1"
   branch experimental-3
   checkout experimental-3
   commit id: "456abc1"
   checkout experimental-2
   commit id: "ad1cb45"
   checkout main
   merge experimental-2 id: "be34af1"
   commit id: "def134a"
   commit id: "563bdef" tag:"v1.0.0 - published paper" type: HIGHLIGHT

  • These tags can also be called releases: (hopefully!) fairly complete, working, nice versions of your code
    • Commits in between releases (merged to the main branch) are like patches to video games
    • The commit notes are like patch notes, telling you what’s changed