Introduction to Git and GitHub
What is Git?
Git is a free, open-source version control tool designed to manage changes in code
efficiently. It is fast, scalable, and widely used by developers to track and collaborate on
projects.
Why Use Git?
● Free and Open Source: Available at no cost with a supportive community.
●
Fast: Quick performance for managing code changes.
●
Scalable: Suitable for small personal projects and large team collaborations.
What is GitHub?
GitHub is a platform that allows users to store, manage, and collaborate on code using Git. It
provides a centralized location for repositories and additional features like pull requests and
issue tracking.
● Website: https://github.com
●
Key Terms:
○ Repository (Repo): A folder containing project files and their version history.
○
Commit: A snapshot of changes made to files in a repository.
●
Setting Up Git
To use Git and GitHub, follow these steps:
1. Install Git:
○ Windows: Download and install Git Bash from git-scm.com.
○
Mac: Git is pre-installed; open Terminal to use it.
2.
3.
Configure Git: Open Git Bash or Terminal and run:
○ git config --global user.name "Your GitHub Username"
○
git config --global user.email "Your GitHub Email"
○
git config --list (Displays your configuration details)
4.
Working with Git and GitHub
Basic Workflow
1. Create a Local Repository:
○ Create a folder and open it in a code editor like VS Code.
○
Open the terminal in the editor.
○
Initialize a Git repository: git init
2.
3.
Clone a Repository:
○ Copy an existing repo to your local machine: git clone <repository-url>
○
Navigate into the folder: cd <repo-name>
4.
5.
Link to GitHub:
○ Create a repo on GitHub and copy its URL.
○
In the terminal: git remote add origin <url>
○
Verify: git remote -v
6.
7.
Make Changes:
○ Add files or modify existing ones.
○
Check status: git status
■ Untracked: New files not yet tracked by Git.
■
Modified: Changed files.
■
Staged: Files ready to commit.
■
Unmodified: No changes.
○
8.
9.
Stage and Commit:
○ Stage changes: git add <file-name> or git add . (all files)
○
Commit changes: git commit -m "Describe your changes"
10.
11.
Push to GitHub:
○ Change default branch (if needed): git branch -M main
○
Push changes: git push origin main
12.
Git Commands
● List Files: ls (Unix) or dir (Windows)
●
Hidden Files: ls -Force (Unix) or Get-ChildItem -Force (PowerShell)
●
Initialize Repo: git init
●
Push Shortcut: After git push -u origin main, use git push for subsequent pushes.
Git Branches
Branches allow you to work on different versions of a project simultaneously.
● View Current Branch: git branch
●
Rename Branch: git branch -M <new-branch-name>
●
Switch Branch: git checkout <branch-name>
●
Create Branch: git checkout -b <new-branch-name>
●
Delete Branch: git checkout -d <branch-name>
Merging Code
Combine changes from different branches:
1. Command Line:
○ Compare branches: git diff <branch-name>
○
Merge: git merge <branch-name>
2.
3.
Pull Request (PR): Use GitHub’s web interface for safer merging.
Pulling Changes
Fetch and update your local repo from the remote:
● git pull origin main
Resolving Merge Conflicts
A merge conflict occurs when Git cannot automatically reconcile differences between two
commits. Resolve manually by editing the conflicting files, then:
● Stage: git add <file-name>
●
Commit: git commit
Undoing Changes
● Unstage Changes:
○ Specific file: git reset <file-name>
○
All staged files: git reset
●
●
Undo Last Commit: git reset HEAD~1
●
Revert to Specific Commit:
○ Soft reset: git reset <commit-hash>
○
Hard reset (discard changes): git reset --hard <commit-hash>
●
●
View Commit History: git log (Press q to exit)
Forking
A fork is a copy of a repository under your GitHub account, linked to the original (upstream)
repo. Fork via GitHub’s website to contribute to projects.
Git Flow Summary
● GitHub Repo → Clone → Edit → Add → Commit → Push