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.
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
git init
: Initializes a new Git repository.git clone <repository>
: Creates a local copy of a remote repository.git add <file>
: Stages changes for commit.git add --all
: Stages all modified filesgit commit -m "message"
: Commits staged changes with a message.git pull
: Fetches and merges changes from a remote repository.git push
: Pushes local commits to a remote repository.git branch
: Lists existing branches.git checkout <branch>
: Switches to a different branch.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
Git Instalation
You can follow this guide (opens in a new tab) or follow the instructions below
Windows (opens in a new tab)
- 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. - Run the exe file
- It will ask for permission to make changes to your device, select
YES
- Click
Next
then choose installation location (leave as default unless you have a reason not to) then pressNext
again - You will be able to choose many options, leave all the defaults unless you have a reason not to. Click
Next
untill you get theInstall
button, click it. - Then wait for installation and hit
Finish
- 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
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
- Go to https://github.com/ (opens in a new tab)
- Click on the
Sign Up
button at the top right - Add your email address
- Create a password
- Choose your username
- Choose if you want updates and announcements
- Play the game to verify that you are human
- Hit the
Create Account
button - Verify email with the code sent to your inbox and input it
- 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:
- On your repository's GitHub page, navigate to the 'Actions' tab.
- Click the 'New workflow' button or select an existing workflow template.
- Choose a workflow template or start with a blank file.
- 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
- Commit the workflow file to your repository.
- GitHub Actions will automatically execute the defined workflow when the specified triggers are met.
Git/GitHub Workflow
Creating a Repository
Using GitHub:
- On GitHub, log in to your account.
- Click the '+' sign in the top right corner and select 'New Repository'.
- Provide a repository name, description, and other settings as needed.
- Click 'Create repository'.
Using Git:
- Open your command line or terminal.
- Navigate to the directory where you want to create the repository.
- Use the following commands:
git init
git remote add origin <repository_url>
git pull origin main
Making Commits
Using GitHub:
- On your repository's GitHub page, navigate to the file you want to change.
- Click the pencil icon to edit the file.
- Make your changes directly in the editor.
- Scroll down and provide a commit message.
- Click 'Commit changes'.
Using Git:
- Make changes to your code.
- 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
- Use the following command to commit changes with a message:
git commit -m "Commit message"
Pushing Changes
Using GitHub:
- After committing changes on GitHub, they will be automatically pushed to the repository.
Using Git:
- 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:
- On your repository's GitHub page, click the
Branch: main
button. - 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:
- On your repository's GitHub page, navigate to the 'Pull requests' tab.
- Click 'New pull request'.
- Set the base and compare branches for the pull request.
- Click 'Create pull request'.
- Review the changes and click 'Merge pull request'.
Using Git:
- Switch to the main branch:
git checkout main
- Merge the new branch into the main branch:
git merge new-feature
- Resolve any merge conflicts if they occur.
- Push the changes to the remote repository:
git push origin main