Installing Git and Setting Up GitHub
Before diving into using Git and GitHub for version control and collaboration, you need to install Git on your machine and set up a GitHub account. This lesson will walk you through the necessary steps to get Git and GitHub ready for use.
Once you've completed the setup, you’ll be able to start tracking changes in your code and collaborating on projects using GitHub. Let's get started!
Installing Git
Git is available for Windows, macOS, and Linux. Below, we’ll guide you through the installation process for each operating system.
1. Installing Git on Windows
To install Git on Windows:
1.Download Git for Windows: Go to the Git for Windows website and download the latest version of Git for Windows.
2. Run the Installer: After the download completes, run the installer. You can select the default options, which should be suitable for most users, but make sure to check the following:
- Git Bash Here: This option allows you to open Git Bash (a terminal emulator for Git) by right-clicking in any folder.
- Adjusting the PATH environment: Choose the option to use Git from the command line and third-party software. This ensures that you can run Git commands in the Windows command prompt or Git Bash.
git --version
2. Installing Git on macOS
To install Git on macOS:
1. Install via Homebrew (recommended): If you have Homebrew installed (a package manager for macOS), you can easily install Git by running the following command in the Terminal:
brew install git
xcode-select --install
This will install Git along with other command line tools.
3. Verify Installation: Once Git is installed, verify the installation by running:
git --version
3. Installing Git on Linux
To install Git on Linux:
1. Install via Package Manager: Git is available in most Linux distribution repositories. Use the following commands based on your Linux distribution:
- For Ubuntu or Debian-based systems:
sudo apt-get update
sudo apt-get install git
- For Fedora:
sudo dnf install git
- For CentOS:
sudo yum install git
git --version
Setting Up Git
Once Git is installed, you need to configure it with your username and email address. This step is important because Git uses this information to associate your commits with your identity.
To set up Git, run the following commands in your terminal (replace the placeholder text with your actual name and email):
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
These commands will configure Git with your global identity. If you want to use different identities for different repositories, you can set them on a per-repository basis by omitting the --global
flag and running the commands inside the specific repository directory.
You can verify your configuration by running:
git config --list
This will display your settings, including the username and email address.
- Username: Choose a unique username.
- Email Address: Enter your email address.
- Password: Choose a secure password.
3. Click Sign Up for GitHub and follow the steps to verify your account.
2. Set Up SSH Keys for GitHub (Optional but Recommended):
To securely authenticate with GitHub without entering your username and password each time you push changes, it’s best to set up SSH keys. Here's how to do it:
1. Generate an SSH Key Pair: In your terminal, run:
ssh-keygen -t rsa -b 4096 -C "youremail@example.com"
2. Save the SSH Key: When prompted, press Enter to save the SSH key in the default location (~/.ssh/id_rsa
).
3. Add the SSH Key to the SSH Agent: Run the following commands to add the SSH key to your SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
4. Add Your SSH Key to GitHub: Copy your SSH public key to the clipboard:
cat ~/.ssh/id_rsa.pub
Go to your GitHub account, click on your profile picture, and select Settings. Then, navigate to SSH and GPG keys and click New SSH key. Paste your public key there and click Add SSH key.
5.Test SSH Connection: Test your SSH connection to GitHub by running:
ssh -T git@github.com
If successful, you’ll see a message like: Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Conclusion
Now that Git and GitHub are installed and set up, you're ready to start version controlling your projects and collaborating with others. GitHub provides a cloud-based platform that pairs perfectly with Git's version control capabilities, giving you the power to manage code, track changes, and collaborate with other developers.
In the next lesson, we’ll dive into how to create and manage Git repositories on GitHub and explore how to interact with them using Git commands.