DEV Community

Cover image for #3 - A Complete Guide to Setting Up Git and GitHub for Beginners
Matheus Fernandes
Matheus Fernandes

Posted on

1

#3 - A Complete Guide to Setting Up Git and GitHub for Beginners

Introduction

Git is an essential tool for any developer, providing powerful version control for your projects. Throughout your journey in software development, you'll work with Git extensively. Although the ins and outs of Git will become clearer as you progress, understanding the basics now will set you up for success.

GitHub and GitLab, on the other hand, are platforms that integrates with Git, enabling you to upload, host, and manage your code in the cloud. It provides a web interface to interact with Git repositories, making collaboration easier. While Git and GitHub/GitLab are often used together, it's important to note that they are separate entities, developed by different companies.

This guide will walk you through the steps to install and configure Git, create a GitHub account, set up your SSH keys, and more.

Install Git

Before we dive into configuration, we first need to install Git. Here are the installation steps for different operating systems:

  • Linux: You can typically install Git through your distribution's package manager. For Ubuntu-based systems:
sudo apt update
sudo apt install git
Enter fullscreen mode Exit fullscreen mode
  • MacOS: You can install Git using Homebrew:
brew install git
Enter fullscreen mode Exit fullscreen mode

Configure Git and GitHub

  • Create a GitHub Account Head over to GitHub and sign up for an account. During the signup process, you will be prompted to provide an email address. This email will be tied to your contributions and will be visible in your Git history.

If you’re concerned about privacy, you can choose to keep your email address private. To do this:

  1. Sign in to GitHub
  2. Go to Settings → Emails
  3. Enable the option "Keep my email addresses private."

GitHub will then provide you with a private email address that you can use for your commits. You’ll need this when configuring Git in the next step.

  • Setup Git Now that you have a GitHub account, it's time to link Git to your GitHub profile. This is important for associating commits with your identity.

Open your terminal and run the following commands:

git config --global user.name "Jhon Doe"
git config --global user.email "jhondoe@provider.com"
Enter fullscreen mode Exit fullscreen mode

Additionally, GitHub recently switched the default branch name from master to main. Set your default branch to main with this command:

git config --global init.defaultBranch main
Enter fullscreen mode Exit fullscreen mode

We also recommend configuring Git to use merge behavior for pull requests:

git config --global pull.rebase false
Enter fullscreen mode Exit fullscreen mode

To verify that everything is set up correctly, run these commands:

git config --get user.name
git config --get user.email
Enter fullscreen mode Exit fullscreen mode

This will display your name and email address, confirming the configuration was successful.

  • For macOS Users macOS users may also want to configure Git to ignore .DS_Store files, which are created by the Finder application. To do this, run:
echo .DS_Store >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
Enter fullscreen mode Exit fullscreen mode
  • Create and Link an SSH Key

Using SSH keys allows you to authenticate with GitHub securely without having to enter your password every time you push code. Let's set this up!

Check if You Have an Existing SSH Key:
First, check if you already have an SSH key by running:

ls ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

If you see a message like "No such file or directory," it means you don’t have an existing SSH key, and you'll need to create one.

Generate a New SSH Key: To generate a new SSH key, run:

ssh-keygen -t ed25519
Enter fullscreen mode Exit fullscreen mode

When prompted, hit Enter to save the key in the default location. You can optionally add a passphrase for extra security.

Link Your SSH Key to GitHub: Once your SSH key is generated, it’s time to link it to GitHub. First, you need to copy your SSH key to your clipboard:

cat ~/.ssh/id_ed25519.pub
Enter fullscreen mode Exit fullscreen mode

Copy the entire output, which starts with ssh-ed25519 and ends with your username and hostname.

Next, go to GitHub, navigate to Settings → SSH and GPG keys, and click on New SSH Key. Paste your SSH key into the provided field, name it, and click Add SSH key.

Test Your SSH Connection:
To verify that everything is set up correctly, run the following command:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

You should see a message that says, "Hi username! You’ve successfully authenticated, but GitHub does not provide shell access." This means you’ve successfully connected your SSH key to GitHub!

Conclusion

Congrats on setting up Git and GitHub! This is an important first step in your development journey. Even if you didn’t understand every detail, that’s perfectly normal—this is just the beginning.

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay