Codepath

Git and GitHub

Git & GitHub Walkthrough

Git and Github are tools we will be using throughout the course to track and store changes to our code and allow you to collaborate with others and keep everything in sync.

This guide introduces using Git with the terminal and GitHub web UI.

Note: You do not need GitHub to use Git, but you cannot use GitHub without using Git.

Overview

What is Git?

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development 1(https://en.wikipedia.org/wiki/Git).

What is GitHub?

GitHub is a code hosting platform for version control and collaboration. It let's you and others work together on projects from anywhere.

Why use Git?

Version control is very important. Without it, you risk losing your work. With Git, you can save your work as often as you like. You can also go back to previous work. This takes the pressure off of you while you're working.

Tip Commit often and commit early, this way you'll never have that gut sinking feeling of overwriting or losing changes.

Initial Setup

To get started, you will need to download, install and configure Git on your computer and create a free GitHub account here.

Install Git

  1. Download the latest version based on your operating system:
  2. Follow the instructions as provided in the Download until the installation is complete.
  3. Open the command line terminal
  4. Type git version to verify Git was installed.

Setting your user in Git

Git uses a username and email to associate commits with an identity. Let's define your user with the following commands

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

Authenticating with Github from Git

When you connect to a Github repository from Git, you will need to authenticate using either HTTPs or SSH. We recommend connecting over HTTPS and caching your GitHub credentials in Git following this instructions

Working with Git & GitHub through the command line

The general development flow is:

  1. Make local code changes on your computer.
  2. Commit your local changes. This saves your progress locally.
  3. Push your new changes to GitHub. This stores your progress on the cloud.
  4. Refresh the repository by pulling in the latest code from GitHub. This ensures the code you are working on is the most up-to-date code and prevent conflicts when looking to push new changes into GitHub

Below are some walkthroughs steps for

  • Setting up a local Git repository
  • Pushing a new repository onto GitHub
  • Pushing new changes when your local code is updated
  • Pulling Changes from GitHub

Creating a local Git repository

Local Git repositories are created and managed locally on your computer.

To begin, open up a terminal and create a new directory, open it and perform git init to create a new repository

mkdir newproject
cd newproject/
git init

Adding and committing changes to your local repository

Now that your local Git repository is up and running, you can update it with any changes you make.

  • Check for any changes that have been made or new files since your last commit

    git status
  • Stage any files with changes (or new ones) you would like to commit

    • git add <path>: This stages an individual file or directory
    • git add .: This stages all files with changes
  • Check to see what was added git status

  • Commit the changes to your local repository with a brief message outlining the changes

    git commit -m "your message about the commit"

Note: Git will notice any new files inside the repository but it won't track the files unless you explicitly tell it to.

Create a new repository on GitHub

To put your project up on GitHub, you will need to create a repository for it. To create a new repository:

  1. Go to GitHub and Log in with your account.
  2. In the upper-right corner of any page, use the drop-down menu, and select New repository.
  3. Type a short, memorable name for your repository.
  4. Optionally, add a description of your repository.
  5. Choose a repository visibility.
  6. Click Create repository.

For more details, checkout GitHub documentation on Creating a new repository.

Linking the local repository with GitHub repository

Since we've already created our repository locally, we want to push that onto our newly created GitHub repository.

  • Connect your local repository to GitHub repository

    git remote add origin https://github.com/yourUserName/yourRepoName.git
  • Push your local changes to your remote repository

    git push -u origin main
  • Browse to your GitHub repository web page to refresh and see your code

Syncing the local repository with your remote repository

To update your local repository to the most recent changes that you or others have push to GitHub, execute git pull

Note: git pull does two things behind the scene: fetch & merge remote changes. Git tries to auto-merge, but unfortunately this is not alway possible and may result in conflicts. You are responsible to merge those conflicts manually by editing the files shown by Git.

Cloning a remote repository

If you are collaborating on a project, or just want access to your project from a different computer, you can clone the project from the GitHub repository.

Cloning a repository is typically only done once, at the beginning of your interaction with the project. Once you have cloned a repository, you won't need to clone it again to do regular development.

To clone a repository:

  • Find the GitHub HTTPS repository url on the main page of the repository.
  • Open the terminal
  • Change the current working directory to the location where you want to clone the repository
  • Type git clone, an the paste the repository URL & press Enter

    git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

Setting up a repository from starter code

Throughout the program, you may be provided initial code to get you started. If so, follow this steps to setup your own repository:

  • Go to the starter code GitHub repository.
  • Find and click the green Code button, and follow the instructions to download the project in a .zip file
  • Unzip the project on your computer's desired location.

Once you have the starter code in your computer, let’s open up a terminal to initialize the git repository.

cd /path/to/unzipped/project
git init
git add .
git commit -m “initial starter code setup”

Further reading

Fork me on GitHub