Getting Started with Git: Installing and Cloning Repositories from GitHub
This guide will help you get Git installed on your computer, clone your first repo from GitHub, and switch branches.
Whether you're brand new to Git or need a quick refresher, this guide will get you up and running in no time. It does assume you have at least some familiarity within a terminal or command-line environment. Linux & Linux-based OSs (Mac) make it easy; You just open up the Terminal app and away you go. With Windows it's slightly more challenging, although I've heard it's improved in recent years (but setting up a terminal environment on Windows is out of scope of this article).
Either way, crack your terminal open and let's get git-ing!
Git Newbie Glossary
Here are a few terms to keep on top of your mind as you read through this.
Term | Definition |
---|---|
Terminal | A command-line interface used to interact with the computer's operating system. |
Git | A piece of software used to track changes in source code during development. |
GitHub | A web-based platform that hosts Git repositories and provides tools for collaboration. |
Repository | A folder of files with some special files to allow Git to track changes inside the folder. |
Branch | A parallel version of a repository, allowing for separate development work to be done. |
Remote | A version of a repository hosted on a server, used for collaboration. |
Clone | A copy of a repository that is downloaded to your local computer from a remote server (mainly GitHub). |
Step 1: Installing Git
If you already have Git installed on your computer, go ahead and skip to step 3 for cloning and branch information.
Windows
Download Git:
- Go to the Git for Windows download page.
- Click the "Download" button.
Install Git:
- Run the downloaded
.exe
file. - Follow the installation wizard. Use the default options unless you have specific requirements.
macOS
Using Homebrew (recommended):
- Open the Terminal.
- Install Homebrew if you don't have it.
- Install Git with Homebrew:
brew install git
Using the installer:
- Go to the Git for macOS download page.
- Download and install the latest version.
Linux
Debian/Ubuntu-based distributions:
- Open the Terminal.
- Update your package list and install Git:
sudo apt update sudo apt install git
Fedora-based distributions:
- Open the Terminal.
- Install Git:
sudo dnf install git
Step 2: Configuring Git
After installing Git, you'll need to configure your Github username and email. This is necessary for tracking changes and contributions.
-
Open your Terminal (or Git Bash on Windows).
-
Add your GitHub account username to your git config:
git config --global user.name "username"
-
Add your GitHub account email to your git config:
git config --global user.email "your.email@example.com"
Step 3: Cloning a Repository
To clone a repository, you'll need the repository URL. This can be found on the repository's GitHub page.
-
Copy the repository URL:
- Navigate to the repository on GitHub.
- Click the "Code" button and copy the URL (use HTTPS for simplicity).
-
Clone the repository:
- Open your Terminal.
- Navigate to the directory where you want to clone the repository:
cd /path/to/your/directory
- Clone the repository:
git clone https://github.com/username/repository.git
- Replace
https://github.com/username/repository.git
with the actual URL you copied from GitHub. git clone
creates a new directory, so with our example commands we would end up with/path/to/your/directory/repository/
with all the code inside therepository/
directory.- To stay organized, I'd recommend setting up some sort of folder structure like
~/code/<client_name>/<repos>/
. So you wouldcd ~/code/<client_name>
and then rungit clone <repo_url>
to end up with~/code/<client_name>/<repo_name>
.
- To stay organized, I'd recommend setting up some sort of folder structure like
Step 4: Switching Branches
Once you have cloned the repository, you'll need to know how to switch to different branches.
-
List available branches:
- Navigate to the cloned repository:
cd ~/code/<client_name>/<repo_name>
- Fetch everything from the repository on Github:
git fetch --all
- List all the available branches:
git branch -a
- If you're switching to a branch for the first time, it will show up in this list prefaced with something like
remotes/origin/
. The<branch_name>
in the next step is whatever is after the last/
. i.e withremotes/origin/branch-xyz
your<branch_name>
is justbranch-xyz
- If you're switching to a branch for the first time, it will show up in this list prefaced with something like
- Navigate to the cloned repository:
-
Switch to a branch:
- Use the branch name from the list to switch branches:
git switch <branch_name>
- Use the branch name from the list to switch branches:
-
Pull the latest changes:
- After switching to the desired branch, make sure to pull the latest changes to ensure you have the most up-to-date code:
git pull
- While
git fetch
andgit pull
seem like similar commands, they are quite different. Think ofgit fetch
as getting the notification that there are changes, andgit pull
is getting the actual changes and applying them to your local copy of the repo.fetch
is getting the email that your package has been delivered,pull
is going to the mailbox to actually get the package.
- While
- After switching to the desired branch, make sure to pull the latest changes to ensure you have the most up-to-date code:
By following these steps, you can switch to any branch and ensure you are always previewing the latest code from that branch.
In Conclusion
And that's it! You now have Git installed, can clone repositories from GitHub, and switch between branches. This will enable you to access and review code locally on your computer.
Happy Git-ing!
Git Commands Cheat Sheet
Task | Command | Description |
---|---|---|
Clone a repository | git clone <repository_url> |
Clones the repository from the specified URL (You should only need to do this once per repo) |
Fetch Everything | git fetch --all |
Fetches information about all the branches from the remote repo. Do this every time. |
List branches | git branch -a |
Lists all local and remote branches |
Change branch | git switch <branch_name> |
Switches to the specified branch |
Pull latest changes | git pull |
Fetches and merges the latest changes from the remote branch. Do this every time. |