Getting Started with Git and GitHub: A Beginner's Guide

What is Git and why do we need it? Git is a software that tracks the changes you make to your files over time. It allows you to create different versions of your files, called commits, and switch between them whenever you want. It also lets you create branches, which are separate copies of your files that you can modify independently and then merge with the main branch. This way, you can experiment with new ideas without affecting the original code.

Git is very useful for developers who work on large or complex projects, as it helps them organize their code, avoid conflicts, and undo mistakes. Git also makes it easy to collaborate with other developers, as you can share your code through remote repositories, which are online copies of your files hosted on a server. You can push your changes to a remote repository, or pull changes from it, to keep your code in sync with others.

What is GitHub and how does it work with Git? GitHub is a website that provides hosting services for Git repositories. It also offers many tools and features that enhance the functionality of Git, such as issue tracking, code review, pull requests, wikis, and more. GitHub is one of the most popular platforms for open-source projects, where developers can contribute to existing projects or create their own.

Setting Up Git on Windows and Unix-based Systems

Before we jump into the exciting world of Git and GitHub, let's start with the foundational steps of setting up Git on your system. Follow these steps to get started:

  1. Installing Git: If you haven't already, download and install Git for your operating system from the official Git website. The installation process is straightforward and user-friendly.

  2. Configuring Your Identity: After installing Git, open your terminal (Windows users can use Git Bash, PowerShell, or Command Prompt) and run the following commands to set your name and email address. This information will be associated with your commits.

     git config --global user.name "Your Name"
     git config --global user.email "yourname@example.com"
    

    Why are we running these commands? This helps in attributing commits to the correct author and is essential for collaboration.

  3. Setting the Default Branch Name: The third setting you need to configure is the default branch name for your repositories. A branch is a parallel version of your files that you can create and switch between. The main branch is the default branch that contains the most stable version of your code. In older versions of Git, the main branch was called “master”, but this name has been changed to “main” in newer versions. To set the default branch name to “main”, type and execute this command:

     git config --global init.defaultBranch main
    
  4. Disabling Pull Rebase by Default: To prevent unexpected rebase behavior, especially for newcomers, it's a good idea to disable pull rebases by default:

     git config --global pull.rebase false
    

    Why do we run this command? Pulling with rebase can sometimes rewrite commit history, which might be confusing for beginners. Disabling it ensures a linear and easier-to-understand history.

  5. Verifying Your Configuration: To verify that your identity is correctly configured, run the following commands:

     git config --get user.name
     git config --get user.email
    

    Make sure that the displayed information matches your name and email.

Setting Up SSH Key for GitHub

Securing your communication with GitHub is crucial when working with remote repositories. SSH keys provide a secure way to authenticate yourself. Follow these steps to set up your SSH key:

  1. Checking for Existing SSH Key: Let's start by checking if you already have an Ed25519 algorithm SSH key installed. Run this command:

     ls ~/.ssh/id_ed25519.pub
    

    If you see a message like "No such file or directory," you'll need to create an SSH key.

  2. Generating an SSH Key: To generate your SSH key, use the following command:

     ssh-keygen -t ed25519 -C "yourname@example.com"
    

    Follow the prompts and provide a secure passphrase for your key when prompted.

  3. Linking Your SSH Key with GitHub: After generating your SSH key, you need to link it with your GitHub account. Run this command to display your public key:

     cat ~/.ssh/id_ed25519.pub
    

    Copy the entire output.

  4. Adding the SSH Key to GitHub:

    • Log in to your GitHub account.

    • Click on your profile picture in the top-right corner, and go to "Settings."

    • In the left sidebar, select "SSH and GPG keys."

    • Click on the "New SSH key" button.

    • Paste the copied SSH key into the "Key" field and provide a recognizable title.

    • Click "Add SSH key."

Testing Your SSH Key

It's important to make sure that your SSH key is set up correctly. Here's how you can test it:

  1. Testing Connection: In your terminal, run the following command to test your SSH connection to GitHub:

     ssh -T git@github.com
    

    If you see a message like "Hi username! You've successfully authenticated, but GitHub does not provide shell access," you're all set!

Congratulations! You've successfully set up Git, configured your identity, created and linked an SSH key, and tested your connection to GitHub. You're now equipped to start collaborating on projects, contributing to open source, and managing your code efficiently. Happy coding!

Remember, this guide provides you with a solid foundation, but there's much more to explore in the world of Git and GitHub. I hope you enjoyed this blog and learned something new. If you have any questions or feedback, please leave a comment below. Thank you for reading!

Did you find this article valuable?

Support Vishesh Gubrani by becoming a sponsor. Any amount is appreciated!