Hack School
Git/GitHub

Git/GitHub

What is Git?

Git is a distributed version control system that enables developers to manage code changes, collaborate on projects, and maintain a history of revisions. It allows multiple developers to work on a project simultaneously and provides tools for tracking changes over time.

Why do we need Git? (What is Version Control?)

Version control is essential in software development to track changes, manage collaborative work, and maintain a history of revisions. Without version control, managing code changes becomes complex, error-prone, and challenging to collaborate on. Git offers a structured approach to version control, making it easier to work in teams, track changes, and revert to previous versions if needed.

Git Installation

You can follow this guide (opens in a new tab) or follow the instructions below

Windows (opens in a new tab)

  1. Go to this link (opens in a new tab) and click on the blue Click here to download at the top of the page. This should download an exe file.
  2. Run the exe file
  3. It will ask for permission to make changes to your device, select YES
  4. Click Next then choose installation location (leave as default unless you have a reason not to) then press Next again
  5. You will be able to choose many options, leave all the defaults unless you have a reason not to. Click Next untill you get the Install button, click it.
  6. Then wait for installation and hit Finish
  7. Verify that it was correctly installed by opening any command prompt and typing:
git --version

If everything worked correctly it should print something like this: git version 2.41.0.windows.3

You now have git installed on your Windows machine!!

Mac (opens in a new tab)

Most versions of MacOS will already have Git installed, and you can activate it through the terminal with git version. However, if you don't have Git installed, you can install the latest version of Git through this link (opens in a new tab).

  • Verify that it was correctly installed by opening the command prompt "terminal" and typing:
git version

Key Concepts in Git

Repositories

A Git repository is a centralized location where your codebase and its complete history of changes are stored. It contains all versions of your files, branches, and commits.

Commits

Commits in Git are snapshots of your code at a specific point in time. Each commit has a unique identifier, a timestamp, and a message describing the changes made. Commits provide a detailed history of the project's development.

Branching

Branching allows developers to create separate lines of development within a repository. The main branch (often 'master' or 'main') serves as the foundation. Feature branches are created for specific changes, enabling developers to work on features without affecting the main branch.

Merging

Merging is the process of integrating changes from one branch into another. After changes are complete in a feature branch, they are merged back into the main branch. Conflicts may arise during merging, requiring resolution.

Common Git Commands

  1. git init: Initializes a new Git repository.
  2. git clone <repository>: Creates a local copy of a remote repository.
  3. git add <file>: Stages changes for commit.
  4. git add --all: Stages all modified files
  5. git commit -m "message": Commits staged changes with a message.
  6. git pull: Fetches and merges changes from a remote repository.
  7. git push: Pushes local commits to a remote repository.
  8. git branch: Lists existing branches.
  9. git checkout <branch>: Switches to a different branch.
  10. git merge <branch>: Merges changes from one branch into the current branch.

For full documentation for all git commands check this link (opens in a new tab) out

What is GitHub?

GitHub is a web-based platform built on top of Git. It provides an interface for hosting and collaborating on Git repositories. GitHub offers additional features, such as issue tracking, project management, and social interaction among developers.

Forking and Cloning

GitHub allows users to fork repositories, creating their own copy. Forks are often used to experiment with changes without affecting the original project. Cloning a repository means copying it from GitHub to your local machine, enabling you to work on code locally.

Pull Requests

Pull requests are a core feature of GitHub's collaborative workflow. They allow developers to propose changes and improvements to a repository. After creating a pull request, other team members review the changes, provide feedback, and approve the merge if everything looks good.

Creating a GitHub Account

  1. Go to github.com (opens in a new tab)
  2. Click on the Sign Up button at the top right
  3. Add your email address
  4. Create a password
  5. Choose your username
  6. Choose if you want updates and announcements
  7. Play the game to verify that you are human
  8. Hit the Create Account button
  9. Verify email with the code sent to your inbox and input it
  10. You now have your own GitHub Account!!

GitHub Actions

GitHub Actions is an automation tool provided by GitHub that allows you to define and automate your software development workflows. With GitHub Actions, you can automate tasks like building, testing, and deploying your code directly from your GitHub repository.

Setting Up a Basic Workflow

To set up a basic GitHub Actions workflow:

  1. On your repository's GitHub page, navigate to the 'Actions' tab.
  2. Click the 'New workflow' button or select an existing workflow template.
  3. Choose a workflow template or start with a blank file.
  4. Define the workflow using YAML syntax. This includes specifying triggers, jobs, steps, and more.

Example Workflow YAML:

name: CI/CD Pipeline
 
on:
  push:
    branches:
      - main
 
jobs:
  build:
    runs-on: ubuntu-latest
 
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
 
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'
 
    - name: Install dependencies
      run: npm install
 
    - name: Run tests
      run: npm test
  1. Commit the workflow file to your repository.
  2. GitHub Actions will automatically execute the defined workflow when the specified triggers are met.

Git/GitHub Workflow

git/github flow

Creating a Repository

Using GitHub:

  1. On GitHub, log in to your account.
  2. Click the '+' sign in the top right corner and select 'New Repository'.
  3. Provide a repository name, description, and other settings as needed.
  4. Click 'Create repository'.

Using Git:

  1. Open your command line or terminal.

  2. Navigate to the directory where you want to create the repository.

  3. Use the following commands:

    • Initialize a new Git repository in the current directory by typing
    git init
    • Add a remote repository named 'origin' and associate it with the provided <repository_url>. The <repository_url> should be the URL of the remote repository on GitHub where you want to push your code.
    git remote add origin <repository_url>
    • Pull any existing content from the 'main' branch of the remote repository into your local repository. This ensures that your local repository is synced.
    git pull origin main

Making Changes and Updating Git

Whether you're working directly on GitHub or using Git on your local machine, making changes, committing them, and pushing those changes is a fundamental aspect of version control.

Using GitHub:

  1. On your repository's GitHub page, navigate to the file you want to change.
  2. Click the pencil icon to edit the file.
  3. Make your changes directly in the editor.
  4. Scroll down and provide a commit message.
  5. Click 'Commit changes'.
  6. After committing changes on GitHub, they will be automatically pushed to the repository.

Using Git:

  1. Make changes to your code.
  2. Use the following command to stage changes for commit:
git add <filename>

Or use git add --all to stage all changed files

git add --all
  1. Use the following command to commit changes with a message:
git commit -m "Commit message"
  1. After committing your changes, use the following command to push them to a remote repository on GitHub:
git push origin main

Creating a Branch

Using GitHub:

  1. On your repository's GitHub page, click the Branch: main button.
  2. Enter a name for your new branch and click Create branch.

Using Git:

To create a new branch and switch to it, use the following commands:

git checkout -b new-feature

This command creates a new branch called new-feature and switches to it.

Merging New Branch with Main

Using GitHub:

  1. On your repository's GitHub page, navigate to the 'Pull requests' tab.
  2. Click 'New pull request'.
  3. Set the base and compare branches for the pull request.
  4. Click 'Create pull request'.
  5. Review the changes and click 'Merge pull request'.

Using Git:

  1. Switch to the main branch:
git checkout main
  1. Merge the new branch into the main branch:
git merge new-feature
  1. Resolve any merge conflicts if they occur.
  2. Push the changes to the remote repository:
git push origin main