Bootcamp
Search…
0.ICE.1: Git Branches

Introduction

We will practise using Git Branches by creating a new Git repo and branches.

Create New Repo

  1. 1.
    Create a repo on GitHub.
    1. 1.
      When creating, choose the option to add a README.md file so the repo does not start empty.
  2. 2.
    Clone the repo.
    1
    git clone <REPO_URL>
    Copied!
  3. 3.
    cd into the repo and add the following index.js file.
    1
    let name = process.argv[2];
    2
    console.log(`hello ${name.toUpperCase()}! How are you?`);
    Copied!
  4. 4.
    Add, commit and push this file to GitHub.

Create Feature Branch

  1. 1.
    Checkout a new branch add-json. Note that branch names are typically in kebab-case.
    1
    git checkout -b add-json
    Copied!
  2. 2.
    Add a data.json file to the repo.
    1
    {
    2
    "names": []
    3
    }
    Copied!
  3. 3.
    Update index.js to contain the following.
    1
    import { writeFile } from 'fs';
    2
    let name = process.argv[2];
    3
    writeFile('data.json', name, (err) => console.error('err ->', err));
    Copied!

Push Feature Branch Changes to GitHub

git push needs additional parameters origin and target branch name to push feature branches to GitHub because when we run git push without params, it pushes the main branch by running git push origin main. origin is the default Git Remote that references a remote repo, in this case our GitHub repo. We also need to specify a target branch name so Git knows which branch to push our changes to. By default we always use the local branch name as the target branch name.
  1. 1.
    Run the following command to push changes from add-json branch to GitHub.
    1
    git push origin add-json
    Copied!
  2. 2.
    After running the above, Git may prompt us to "set an upstream branch", i.e. to set a default branch for this local branch to push to and pull from. Here is why. Follow instructions in terminal if necessary.
  3. 3.
    Visit GitHub to see what we pushed. Use the branch dropdown as follows.
We can view changes on each branch in GitHub via the branch dropdown in the repo.

Push Main Branch Changes to GitHub

  1. 1.
    Checkout main and make the following change to the console.log in index.js.
    1
    console.log(`good day ${name}, are you well?`);
    Copied!
  2. 2.
    Add, commit and push this change to GitHub.

Using Pull Requests to Compare Changes

  1. 1.
    When we push to a feature branch on GitHub we may see a GitHub message prompting us to create a PR.
  2. 2.
    Clicking the button will show a preview PR comparing the feature branch's code with main (or any other branch) and any potential merge conflicts. This will not create a PR until we click another button.
  3. 3.
    A convenient development strategy is to view code changes in GitHub with its convenient syntax highlighting, then return to VSCode to edit and test our code. We can technically edit code and resolve merge conflicts from within GitHub, but we discourage this because there is no way to test code in GitHub.
Button to create PR after pushing feature branch to GitHub.

Add Partner to Repo

Add your partner as a contributor to the repo.
  1. 1.
    Go to the Settings tab of the repo.
  2. 2.
    Click Manage Access in the left-side menu.
  3. 3.
    Click the green Invite teams or people button.
  1. 1.
    Type to autocomplete your partner's GitHub username.
  1. 1.
    Your partner should receive an email with an invitation to collaborate. This notification may not show on the GitHub website.

Make Changes in Partner's Copy of Repo

  1. 1.
    Once we have added our partner as a collaborator, have our partner clone the repo.
    1
    git clone <REPO_URL>
    Copied!
  2. 2.
    Create a new branch.
  3. 3.
    On the new branch, make the following change to the console.log in index.js.
    1
    console.log(`yo ${name}, what's up dude.`);
    Copied!
  4. 4.
    Add, commit and push this change.

Partner Submits Pull Request

PRs document changes to a code base and allow us to review changes by teammates. Once ready to merge each feature, engineers create a PR for the team to review. Once approved, engineers can "merge" PRs, i.e. merge their feature branch into main.
  1. 1.
    Create a PR with the changes we made on the partner's new branch.
  2. 2.
    Merge the PR into main.

Pull Partner's Code from GitHub

  1. 1.
    After the partner has merged their changes to main, we will now pull those changes to the original contributor's repo.
  2. 2.
    Checkout main and run git pull in the original contributor's repo to get the latest changes from GitHub. Note we need to be in main to pull changes from main.
    1
    git checkout main
    2
    git pull
    Copied!
  3. 3.
    Verify that the code in main locally is now the code in main on GitHub.