Git and GitHub
Lab Sessions
Installation and Configuration
Installation
- macOS: Install Xcode Command Line Tools. Open Terminal and run:
xcode-select --install- Linux: Git may already be installed. If not, install it using:
sudo apt install git- Windows: Download Git for Windows from
https://git-scm.com/download/win and install it with default settings.
To verify that Git is installed, run:
git --versionSign Up for GitHub
Follow these steps to create a GitHub account:
- Open the GitHub website:
https://github.com/
Click Sign up in the top-right corner.
Enter your:
- Email address
- Password
- Username
- Email address
Choose your email verification preferences.
Complete the verification challenge.
Click Create account.
GitHub will send a verification code to your email. Enter the code to activate your account.
Choose the Free plan (sufficient for this course).
After this, your GitHub account is fully ready.
Git Configuration
Check Current Git Config
Run the following command to see your current configuration (should be mostly empty on new installations):
git config --global --listSet Username
git config --global user.name "your_name"Set Email
git config --global user.email "your_email"Verify Updated Config
Check again:
git config --global --listGenerate Personal Access Token (PAT)
GitHub no longer accepts passwords for Git operations.
You must generate a Personal Access Token (PAT).
Steps
Sign in to your GitHub account.
Navigate to:
Profile → Settings → Developer settings → Personal access tokens → Tokens (classic)
Click Generate new token.
Select scopes (minimum recommended):
reporead:orgworkflow- or simply select all
- Set No expiration.
Click Generate token.
Copy the token and store it securely.
You will paste this token when executing:
git pushGenerate SSH Key (Optional but Recommended)
SSH keys allow secure password-less authentication with GitHub.
Steps
Open Terminal or PowerShell.
Generate a new SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"- When prompted for a location, press ENTER to use the default:
~/.ssh/id_rsa
- When asked for a passphrase:
- leave empty for no passphrase
- or enter a secure passphrase
- leave empty for no passphrase
- Display your public key:
cat ~/.ssh/id_rsa.pubCopy SSH Key
macOS:
pbcopy < ~/.ssh/id_rsa.pubWindows PowerShell:
Get-Content ~/.ssh/id_rsa.pub | Set-ClipboardAdd SSH Key to GitHub
- Go to GitHub → Settings → SSH and GPG keys
- Click New SSH key
- Paste your key
- Save
Create a Remote Repository on GitHub
- Log in to your GitHub account.
- Click the New repository button.
- Fill in:
- Repository name
- Description (optional)
- Set it to Public
- Repository name
- Do NOT initialize with README (we will create files locally).
- Click Create repository.
GitHub will show you the remote URL, e.g.:
https://github.com/username/myrepo.git- Choose a folder on your computer.
- Initialize Git in that folder:
git init- Connect your local repo to the remote repo:
git remote add origin https://github.com/username/myrepo.git- Verify remote:
git remote -vAdding a Dummy File
Inside your local project folder, create a simple file:
echo "This is a test file" > test.txtThen stage it:
git add test.txtCommit it:
git commit -m "Add test file"Pushing to GitHub
Push your commit to GitHub:
git push -u origin mainIf your default branch is master, use:
git push -u origin masterYou will be asked for:
- username
- Personal Access Token (instead of password)
After authentication, your file will appear on GitHub.
Pulling Changes from GitHub
If changes have been made remotely, you can update your local repo:
git pull origin mainor:
git pullUse pull whenever you want your local files to match GitHub.
Cloning a Repository (Alternative Workflow)
Instead of creating a folder manually, you can clone an existing remote repo:
git clone https://github.com/username/myrepo.gitThis will:
- Create the folder
- Download all existing files
- Set the remote origin automatically
Summary Workflow
- Create repo on GitHub (empty, no README)
git initlocally
- Create files
git add
git commit
git remote add origin <url>
git push
or use:
git clone→ edit →git add→git commit→git push
Homework
This task ensures that you can create repositories, work locally, work remotely, and synchronize changes between GitHub and your machine.
Follow the steps below exactly in order.
Create a Remote Repository on GitHub
- Log in to your GitHub account.
- Click New Repository.
- Set:
- Repository name: `choose any name
- Visibility: Public
- Repository name: `choose any name
- Do NOT initialize with README.
- Click Create Repository.
Copy your remote URL, e.g.:
https://github.com/yourusername/myrepo.git
Clone the Repository to Your Computer
In a folder of your choice, open Terminal or PowerShell and run:
git clone https://github.com/yourusername/myrepo.gitThis will create a new local folder with the repository inside.
Create a Dummy File Locally
Navigate into the cloned folder:
cd myrepoCreate a simple text file:
echo "This is my first Git test file." > test.txtStage and commit the file:
git add test.txt
git commit -m "Add test file"Push it to GitHub:
git pushCheck GitHub — you should now see test.txt in the repository.
Add a New File on GitHub (Remote)
- Open your repo on GitHub.
- Click Add file → Create new file.
- Name it:
remote_file.txt - Add any text inside it.
- Commit the new file.
Pull the New File to Your Computer
Return to your local repo folder and run:
git pullYou should now see remote_file.txt in your local directory.
This confirms that your local and remote repositories are synchronized.
Submission
Submit your GitHub repository and GitHub profile links here.
Example format:
https://github.com/yourusername/myrepo