0% found this document useful (0 votes)
14 views

Git & GitHub

The workshop on Git & GitHub covers the fundamentals of version control, installation, and basic commands for using Git, including branching and merging. It also introduces GitHub as a platform for collaboration, detailing how to create repositories, push changes, and manage pull requests. Best practices for commit messages and using .gitignore files are discussed, along with hands-on exercises to reinforce learning.

Uploaded by

govindjee725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Git & GitHub

The workshop on Git & GitHub covers the fundamentals of version control, installation, and basic commands for using Git, including branching and merging. It also introduces GitHub as a platform for collaboration, detailing how to create repositories, push changes, and manage pull requests. Best practices for commit messages and using .gitignore files are discussed, along with hands-on exercises to reinforce learning.

Uploaded by

govindjee725
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Workshop - Git & GitHub

Git & GitHub (4:00 to 6:00)


I. Introduction to Version Control and Git (10 minutes)
What is version control? Version control is a system that helps track
changes to files over time. It allows multiple people to work on the same
project simultaneously, keeping a record of who made what changes and
when.

Why use Git? Git is a distributed version control system that offers several
advantages:
• It allows for non-linear development (parallel branches)
• It provides strong support for collaborative development
• It's fast and efficient for both small and large projects
• It ensures data integrity and security

Basic Git terminology:


• Repository: A storage location for a project, containing all files and their
revision history
• Commit: A snapshot of changes made to the project at a specific point in
time
• Branch: An independent line of development, allowing work on different
features simultaneously without affecting the main codebase

Understanding these concepts is crucial for effectively using Git and


collaborating on software projects.

II. Setting Up Git (5 minutes)


Installing Git: This involves downloading and installing the Git software on
your computer. The process varies depending on your operating system:
• For Windows: Download the installer from the official Git website and run
it.
• For macOS: Use Homebrew package manager or download from the Git
website.
• For Linux: Use your distribution's package manager (e.g., apt-get for
Ubuntu).

Workshop - Git & GitHub 1


Configuring Git with your name and email: After installation, you need to
set up your identity for Git to use when you make commits. This is done
using the following commands in the terminal or command prompt:
• git config --global
user.name "Your Name"
• git config --global user.email "
[email protected]"
These settings are important as they are included in every commit you
make, helping to identify who made specific changes in a project.

These steps are crucial for setting up Git on your local machine and preparing it
for use with version control and collaboration on projects.

III. Basic Git Commands (20 minutes)

# Initialize a new Git repository


$ git init my_project
$ cd my_project

# Create a Python file


$ echo "print('Hello, Git!')" > hello.py

# Check the status of your files


$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committe
hello.py

# Stage the changes


$ git add hello.py

# Commit the changes


$ git commit -m "Initial commit: Add hello.py"
[master (root-commit) f7d1e1d] Initial commit: Add hello.py
1 file changed, 1 insertion(+)
create mode 100644 hello.py

Workshop - Git & GitHub 2


# View commit history
$ git log
commit f7d1e1d... (HEAD -> master)
Author: Your Name <[email protected]>
Date: Fri Aug 30 16:30:00 2024 +0000

Initial commit: Add hello.py

This example demonstrates:

git init: Creates a new Git repository in the 'my_project' directory.

git status: Shows that 'hello.py' is an untracked file.

git add: Stages 'hello.py' for commit.

git commit: Creates a new commit with the staged changes.

git log: Displays the commit history, showing our initial commit.

These commands form the basic workflow for tracking changes in a Git
repository.

IV. Working with Branches (15 minutes)

# Create a new branch named 'feature'


$ git branch feature

# List all branches (* indicates the current branch)


$ git branch
* master
feature

# Switch to the 'feature' branch


$ git checkout feature
Switched to branch 'feature'

# Make changes and commit them


$ echo "print('New feature')" >> hello.py
$ git add hello.py
$ git commit -m "Add new feature to hello.py"

Workshop - Git & GitHub 3


# Switch back to the master branch
$ git checkout master
Switched to branch 'master'

# Merge the 'feature' branch into 'master'


$ git merge feature
Updating f7d1e1d..3e4c5f6
Fast-forward
hello.py | 1 +
1 file changed, 1 insertion(+)

In this example:

We create a new branch called 'feature' using git branch .

We list all branches to confirm its creation.

We switch to the 'feature' branch using git checkout .

We make changes, stage them with git add , and commit them.

We switch back to the master branch.

Finally, we merge the 'feature' branch into 'master' using git merge .

This workflow allows for parallel development and safe experimentation without
affecting the main codebase until the changes are ready to be merged.

V. Introduction to GitHub (10 minutes)


1. What is GitHub? GitHub is a web-based platform that uses Git for version
control. It provides a centralized location for storing repositories and facilitates
collaboration among developers.

2. Creating a GitHub account: Visit github.com and sign up for a new account.
You'll need to provide a username, email address, and password.

3. Creating a new repository on GitHub: After logging in, click the '+' icon in
the top-right corner and select "New repository". Give your repository a name
(e.g., "my-first-repo"), choose whether it should be public or private, and click
"Create repository".

Once you've created your repository, GitHub will provide instructions on how to
push an existing repository from the command line:

Workshop - Git & GitHub 4


# If you haven't initialized your local repository yet:
git init
git add .
git commit -m "Initial commit"

# Add the GitHub repository as a remote


git remote add origin https://github.com/your-username/my-fir

# Push your changes to GitHub


git branch -M main
git push -u origin main

This example demonstrates how to create a GitHub account, set up a new


repository, and connect it to your local Git repository. It's a fundamental
workflow for getting started with GitHub and sharing your code with others or
across multiple devices.

VI. Connecting Local Git to GitHub (15 minutes)


Scenario: You're contributing to an open-source project called "awesome-
calculator" on GitHub.

1. git remote: Adding a remote repository

# Clone the repository


git clone https://github.com/original-owner/awesome-calculato
cd awesome-calculator

# Add your forked repository as a remote


git remote add myrepo https://github.com/your-username/awesome

# Verify the new remote


git remote -v

This allows you to interact with both the original repository and your fork.
2. git push: Pushing changes to GitHub

# Create a new branch for your feature


git checkout -b add-exponent-function

Workshop - Git & GitHub 5


# Make changes to the code
echo "def exponent(x, y):\n return x ** y" >> calculator.py

# Commit the changes


git add calculator.py
git commit -m "Add exponent function"

# Push the new branch to your fork


git push myrepo add-exponent-function

This creates a new branch with your changes and pushes it to your fork on
GitHub.
3. git pull: Pulling changes from GitHub

# Switch back to the main branch


git checkout main

# Pull the latest changes from the original repository


git pull origin main

This ensures your local main branch is up-to-date with the original project.

These commands demonstrate how to connect your local repository to GitHub,


push your changes, and keep your local copy updated with the latest changes
from the original project.

VII. Collaboration on GitHub (20 minutes)


Scenario: You want to contribute to an open-source project called "awesome-
weather-app".

1. Forking a repository

Visit the "awesome-weather-app" repository on GitHub and click the "Fork"


button. This creates a copy of the repository under your GitHub account.

2. Creating pull requests


After forking, clone your fork to your local machine:

Workshop - Git & GitHub 6


git clone https://github.com/your-username/awesome-weather-ap
cd awesome-weather-app

Create a new branch for your feature:

git checkout -b add-fahrenheit-conversion

Make changes to the code, commit them, and push to your fork:

# Make changes to the code


git add .
git commit -m "Add Fahrenheit to Celsius conversion function"
git push origin add-fahrenheit-conversion

Go to your fork on GitHub and click "Compare & pull request". Fill in the
description and create the pull request.

3. Reviewing and merging pull requests


The project maintainers will review your pull request. They might request
changes or approve it.

If changes are requested, make the necessary updates in your local branch,
commit, and push. The pull request will automatically update.
Once approved, the maintainer can merge your pull request, incorporating your
changes into the main project.
This example demonstrates how forking, creating pull requests, and the review
process work together to facilitate collaboration on GitHub.

VIII. Best Practices and Tips (5 minutes)


1. Writing Good Commit Messages

Be Descriptive: Provide a clear summary of the changes to make the


commit easy to understand at a glance.

Use Imperative Mood: Write your commit message as if giving a


command, e.g., "Fix typo" instead of "Fixed typo."

Keep It Short: Keep the message brief, ideally under 50 characters, so


it’s easily readable in logs.

Workshop - Git & GitHub 7


Separate Subject and Body: Use a blank line between the short
description and detailed explanation for clarity.

Reference Issues: Link to related issues or tickets to track why the


change was made.

Avoid Vague Messages: Specific details help others (and your future
self) understand the change.

2. Using .gitignore Files

Purpose: Prevent unnecessary files from being tracked by Git, keeping


the repository clean.

Customization: Adjust .gitignore to exclude files specific to your


project's tools and environment.

Global .gitignore: Apply a global .gitignore for files that should never be
committed across any repository.

Placement: The .gitignore file should reside in the root directory of your
project to apply to the entire repository.

3. Regular Commits and Pushes

Commit Often: Make small, frequent commits to keep changes


manageable and easy to track.

Push Regularly: Regular pushes prevent merge conflicts and keep your
work backed up and in sync with the team.

Review Before Committing: Ensure that only the intended changes are
staged for commit, avoiding accidental commits.

Use Branches: Work on features or fixes in branches to keep the main


codebase stable and organized.

IX. Hands-on Exercise (10 minutes)


Create a local repository

Make changes and commit them

Create a GitHub repository

Push local changes to GitHub

Create a branch, make changes, and create a pull request

Workshop - Git & GitHub 8


X. Q&A and Wrap-up (10 minutes)
Addressing student questions

Recap of key concepts

Resources for further learning

Xl. NOTE:
1. Tell them about GitHub Student Developer Pack

2. Tell them about how Visual Studio is directly connected to GitHub

XII. All commands I used:

# Set configuration values for your username and email


git config --global user.name "YOUR NAME"
git config --global user.email "YOUR EMAIL"

# Set default branch to main


git config --global init.defaultbranch main

# Get help on a command


git help COMMAND
git COMMAND -h

# Initialize a new git repository


git init

# Clone a repository
git clone REPOSITORY_URL

# Add a file to the staging area


git add FILE

# Add all file changes to the staging area


git add --all
git add -A
git add .

# Check the unstaged changes

Workshop - Git & GitHub 9


git diff

# Commit the staged changes


git commit -m "MESSAGE"

# Reset staging area to the last commit


git reset

# Check the state of the working directory and the staging are
git status

# Remove a file from the index and working directory


git rm FILENAME

# Rename a file
git mv OLD_NAME NEW_NAME

# List the commit history


git log

# List all the local branches


git branch

# Create a new branch


git branch BRANCH_NAME

# Rename the current branch


git branch -m NEW_BRANCH_NAME

# Delete a branch
git branch -d BRANCH_NAME

# Switch to another branch


git switch BRANCH_NAME
git checkout BRANCH_NAME

# Merge specified branch into the current branch


git merge BRANCH_NAME

Workshop - Git & GitHub 10


# Create a connection to a remote repository
git remote add NAME REPOSITORY_URL

# Push the committed changes to a remote directory


git push REMOTE BRANCH
git push origin main

# Download the content from a remote repository


git pull REMOTE

# What comes when I make a new repo for importing from a local
git remote add origin https://github.com/Rush-Code10/name_of_
git branch -M main
git push -u origin main

# A few commands
git config --list (u can see all the configurations)
after git log to come out use the key q

XIII. git —help

1. Start a working area


git clone
This command is used to copy an existing repository into a new directory.

git clone <https://github.com/example/repo.git>

git init
This initializes a new Git repository in the current directory.

mkdir my_project
cd my_project
git init

Workshop - Git & GitHub 11


2. Work on the current change
git add
Add a specific file to the staging area (index):

git add file.txt

Or add all files:

git add .

git mv
Rename or move a file:

git mv oldname.txt newname.txt

git restore
Restore a file that you modified but don’t want to include anymore:

git restore file.txt

Restore a file from a specific commit:

git restore --source=HEAD~1 file.txt

git rm
Remove a file from the working directory and staging area:

git rm file.txt

3. Examine the history and state


git bisect
This command helps to find a specific commit that introduced a bug using
binary search.

Workshop - Git & GitHub 12


git bisect start
git bisect bad HEAD
git bisect good COMMIT_HASH

git diff
Compare changes between the working tree and the last commit:

git diff

git grep
Search for a specific string in the repository:

git grep "search_term"

git log
View the history of commits:

git log

git show
Show details of a specific commit or object:

git show COMMIT_HASH

git status
Check the current state of the working directory and staging area:

git status

4. Grow, mark, and tweak your common history


git branch
List all branches:

Workshop - Git & GitHub 13


git branch

Create a new branch:

git branch new_feature

Delete a branch:

git branch -d old_branch

git commit
Record changes to the repository:

git commit -m "Initial commit"

git merge
Merge changes from one branch into the current branch:

git merge new_feature

git rebase
Reapply commits from your current branch onto another branch:

git rebase main

git reset
Move the current HEAD to a different commit:

git reset --hard COMMIT_HASH

git switch
Switch between branches:

git switch main

Workshop - Git & GitHub 14


git tag
Create a new tag:

git tag v1.0

List all tags:

git tag

5. Collaborate
git fetch
Fetch updates from the remote repository without merging:

git fetch origin

git pull
Fetch and merge updates from a remote branch into your current branch:

git pull origin main

git push
Push local commits to the remote repository:

git push origin main

Workshop - Git & GitHub 15

You might also like