1 of 13

How to Git at Collab Lab

2 of 13

What we’ll cover

There is so much to know, but this should get you started with what you need to know at The Collab Lab!

  • Creating and switching branches (git checkout)
  • Staging changes (git add)
  • Saving changes in history (git commit)
  • Sending changes to GitHub (git push)
  • Getting the latest changes (git pull)
  • Getting your changes into main (PRs)

3 of 13

Repository (repo) - A collection of files, under source control like git.

Remote - The origin repo somewhere online (like GitHub).

Branch - A working copy of the repo. The main branch is the source of truth.

Commit - A set of changes to the repo. HEAD refers to the last commit.

Pull Request (PR) - Documents how/why changes should merge with main. Specific to GitHub—you might also see Merge Request (e.g., GitLab).

terms

4 of 13

Initial commit

Add button

Update text

Add styles

Add tests

Merge branch

Branch

Merge

Initial commit

Add button

Update text

Add styles

Add tests

Merge branch

5 of 13

The Commands

6 of 13

Branching

Switch to an existing branch, or create a new one with the -b option

  • Make any changes you want on a working branch.

  • The remote project won’t be affected by anything you do on your branch.

# Checkout the cool-feature branch

git checkout cool-feature

# Create the new-feature branch

git checkout -b new-feature

git checkout

7 of 13

Staging changes

Staging changes prepares files to be committed.

  • Only staged files will be committed.
  • The -A option stages all files to be committed

# Stage a single file

git add ./Button.js

# Stage all files

git add -A

git add

8 of 13

Commiting changes

Takes staged changes and commits them to git. This creates a saved record of changes to the code base.

  • git commit comes with a few options which can be combined.
  • No options opens an editor to write a message
  • -m accepts an inline message
  • -a stages any tracked files to the commit
  • -am stages tracked files and accepts an inline message

# Opens an editor to write a message. # Commits staged files

git commit

# Takes an inline message

# Commits staged files

git commit -m "Update heading text"

# Takes an inline message

# Commits staged and tracked files

git commit -am "Add details layout"

git commit

9 of 13

Uploading changes

Takes all commits and uploads them to the remote.

  • Arguments and options tell git where to send commits.
  • origin refers to the source where the repo was originally cloned from.
  • -u adds an upstream reference to the remote. This only needs to be done once per branch. Afterwards, git knows where to send commits without args.

# Sets the upstream remote and sends

# all commits to the remote

git push -u origin cool-feature

# Sends all commits to the upstream

# remote that was set

git push

git push

10 of 13

Getting the latest changes

Gets changes from the remote based on your current branch

  • It's a good habit to pull the latest main before creating a new branch.

# Switch to the main branch

git checkout main

# Get the latest changes for main

git pull

git pull

11 of 13

Pull requests

12 of 13

Merge conflicts

It’s going to be okay!

  • Caused when git can’t figure out how to resolve changes to the same file from two different branches.
  • git will need to know what changes should get saved in the file. Communication with your team is key!
  • Here is a helpful guide, but your mentors are ALWAYS here to help, so be noisy in Slack!

13 of 13

Resources