Mini Lecture: Using Git and GitHub in the Terminal for Windows, Linux, and macOS
1. Initial Setup
- Install Git: Ensure Git is installed.
- Windows: Download from git-scm.com and follow the installer.
- Linux: Run
sudo apt install git
(Debian/Ubuntu) orsudo dnf install git
(Fedora). - macOS: Use
brew install git
if Homebrew is installed or download directly from git-scm.com.
- Configure Git: Set your global username and email:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
- Set Up Authentication with SSH:
- Generate an SSH Key:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
Follow the prompts and press Enter to accept the default file location.
- Add the SSH Key to the SSH Agent:
eval $(ssh-agent -s) ssh-add ~/.ssh/id_rsa
- Add Your SSH Key to GitHub:
- Copy the public key to your clipboard:
cat ~/.ssh/id_rsa.pub | pbcopy # Use 'xclip' or manual copy on Linux
- Go to GitHub SSH settings and add a new SSH key.
- Copy the public key to your clipboard:
- Test the Connection:
ssh -T git@github.com
- Generate an SSH Key:
2. Cloning a Repository
To work on an existing project, clone the repository:
# Replace with your repository URL
git clone https://github.com/username/repository.git
Or, if using SSH:
git clone git@github.com:username/repository.git
Navigate into the project directory:
cd repository
3. Basic Git Workflow
- Check the status of your repository:
git status
- Stage changes (add files to the staging area):
git add . # Stages all modified files git add filename.ext # Stages a specific file
- Commit changes with a message:
git commit -m "Your commit message"
- Push changes to GitHub:
git push origin main # Replace 'main' with the appropriate branch name
4. Creating a New Repository
- Initialize a new repository:
git init
- Connect to a remote GitHub repository:
git remote add origin https://github.com/username/new-repository.git
Or, if using SSH:
git remote add origin git@github.com:username/new-repository.git
- Add and commit your initial project files:
git add . git commit -m "Initial commit"
- Push to GitHub:
git branch -M main # Ensure the branch is named 'main' git push -u origin main
5. Branching and Merging
- Create a new branch:
git checkout -b new-branch
- Switch between branches:
git checkout main
- Merge a branch into your current branch:
git merge new-branch
6. Handling Merge Conflicts
If you encounter merge conflicts:
- Git will mark conflicts in your files. Open the files in a text editor and manually resolve the conflicts.
- Once resolved, stage the changes:
git add resolved-file.ext
- Commit the merge:
git commit -m "Resolved merge conflict"
7. Pulling Updates
To keep your local repository up-to-date with the remote:
git pull origin main # Replace 'main' with your branch name
8. Additional Useful Commands
- View commit history:
git log
- Undo the last commit (without losing changes):
git reset --soft HEAD~1
- Remove files from the staging area:
git reset filename.ext
Summary
This mini lecture has outlined essential commands for using Git and GitHub across Windows, Linux, and macOS. Remember to commit often, write meaningful commit messages, and stay consistent with Git best practices.
concluding slide image – mostly decorative that restates text