The Beginner’s Guide To Git & GitHub

Git and GitHub essentials! Navigate version control, SSH keys and fundamental commands effortlessly. Your coding journey starts here. Let's code together!

The Beginner’s Guide To Git & GitHub

In the fast-paced world of software development, the absence of a reliable version control system can lead teams down a chaotic path. Without tools like Git, teams often resort to ad-hoc methods such as sharing files via email or using shared network drives. Unfortunately, this approach breeds confusion, conflicts, and the ever-present risk of losing track of critical changes. Sounds familiar, huh?

Objectives

By the end of this guide, you will:

  1. Gain a comprehensive understanding of basic Git terminology.
  2. Learn how to generate and use SSH keys for secure authentication with GitHub using openSSH.
  3. Acquire the necessary skills to navigate GitHub efficiently and effectively.

Prerequisites

Before proceeding, make sure you have the following prerequisite:

  • Git
sudo apt install git
sudo apt install openssh-client

What is Git? 

Git is an open source version control system for tracking changes to files over time. With Git, developers can create snapshots of their code at different points making it easy to revert to previous versions if needed. It also helps programmers to collaborate on projects and synchronize their work.

What is GitHub? 

GitHub is an online code hosting platform for software development projects that use git. It enables programmers to share their code files and collaborate with other developers from anywhere around the globe. GitHub offers a rich set of collaboration tools including pull requests, issue tracking and project management features. Additionally, GitHub integrates seamlessly with CI/CD pipelines automating build, test and deployment processes for projects.

💡
Note: Git isn't limited to GitHub! There are plenty of other platforms where you can leverage Git's power. Other options like GitLab, Bitbucket, SourceForge, GitKraken and Azure DevOps exist.

Git Terminology

Repository

It is a directory for storing all the files and folders related to a project as well as the history of changes made to the files of the project. A repository residing a local machine is called local repository whereas a repository that is hosted on a server is known as a remote repository.   

Commits

Commits are simply changes you have made in your branch since your last commit. A commit is basically a snapshot of your repository.

The staging environment

Also known as the index, it is an intermediate area where the files that are going to be part of the next commit are stored. For a file to be part of a commit, you must first add it to the staging environment.

Branch

A branch is a parallel version of your repository that allows you to work on new features or fixes without affecting the primary or master branch. Branches can be merged back into the main branch when changes are ready to be published.

Master

It is the repository’s primary/default branch. In recent years, GitHub has switched to using 'main' as the default branch name in repositories, reflecting a more inclusive and neutral terminology.

Push

Push means uploading your committed changes from your local repository to a remote one (in this case GitHub).  

Pull

Pull means downloading changes, that you or other people have made, from a remote repository to the repository on your local machine.

Pull request

Pull request is a way to notify the repository’s maintainers to review the commits you made to their code and, if acceptable, merge the changes into their master branch. 

Upstream branch

A local branch can be made to track a remote branch so that pushing and pulling become easier and less susceptible to mistakes. In such a case, the local branch is known as the tracking branch and the remote branch is known as the upstream branch.

Generating and testing an SSH key

Using SSH keys provides a secure and convenient way to access GitHub repositories without the need to enter a username and password for each interaction. It's recommended for anyone working with GitHub repositories to ensure secure access and protect sensitive information.

1.  To generate key using the Ed25519 algorithm, run the following command in your terminal:

ssh-keygen -t ed25519 -C "Enter your GitHub email address here"

When prompted to enter a file to which to save key, press Enter to accept default file location. You may enter a passphrase of your choice if you wish. 

💡
Note: Ed25519 is used as it is faster and more secure than DSA, RSA and ECDSA. It is resilient to hash function collisions and is also strongly resistant to side-channel attacks. Moreover they provide the same level of security as the forementioned algorithms at a much shorter key length. 

 2.  Start the ssh-agent with the following command:

eval "$(ssh-agent -s)" 

ssh-agent is used to manage SSH keys and to keep track of passphrases. Read more about ssh-agent here.

 3.  Add SSH key to ssh-agent:

 ssh-add  ~/.ssh/id_ed25519

 4.  Copy your SSH public key to your clipboard using one of the following methods: 

i. Displaying the SSH Public Key: To view your SSH public key, use the command:

cat ~/.ssh/id_ed25519.pub

When copying the key, ensure that you do not introduce any additional characters, spaces or new lines.

ii. Copying the SSH Public Key to Clipboard: If you have the xclip utility installed, you can copy the public key directly to your clipboard with the following command:

xclip -sel clip ~/.ssh/id_ed25519.pub 

5.  Navigate to SSH and GPG keys on GitHub account settings.

i. Click on your profile picture on the top right corner of GitHub. Then click on Settings.

ii. Click on SSH and GPG keys.

6.  Click new SSH key.

7.  Enter a descriptive title for your key.

8.  Paste your public key in the key field.

9.  Click Add SSH.

10.  If prompted, enter password. 

11.  To verify your SSH key is properly authenticated to GitHub, run:

ssh -T [email protected]

Basic commands

1.  Initializing a Git Repository

Whether you're starting a new project or want to add version control to an existing one, initializing a Git repository is the first step.

mkdir myProject
cd myProject
git init

To initialize a git repository, run the git init command in the project directory as shown above. 

When you run git init, Git creates a hidden directory called .git in your project’s root folder. This directory houses all the necessary metadata for version control.

2. Managing Files in Git

After initializing the repository, you can start adding and managing files.

touch newFile.txt 
git status

git status displays the current state of the working directory and the staging area. It shows which changes have been staged, which changes are not yet staged for commit and which files are not being tracked by Git. 

  • touch newFile.txt: Creates a new file within the project.
  • git status: Checks the current status of the repository and lists any changes.

3.   Staging Changes

Before committing changes, you need to stage them for inclusion in the next commit.

git add . 

git add . stages all (new, modified, deleted) files in the current directory.

git add -A stages all(new,modified, deleted) files in the entire working tree and not just the current directory.

  • git add .: Adds all new, modified and deleted files in the current directory and its subdirectories to the staging area.
  • git add -A: Adds all changes, including deletions and file creations, to the staging area.

4.  Configuring Git Identity

Configure your username and email address for Git commits.

git config --global user.name "Enter your name here"
git config --global user.email "Enter your email here" 

These commands set the username and email that are associated with your Git commits.

--global specifies that the configuration is applied to the entire system and and is not specific to just that repository. If you want to use another username and email for specific projects in the future, you can run the command without the --global option when you are in those projects. 

Run git config user.name and git config user.email to view your Git username and email respectively.

5.  Creating commits

To create a commit, use the following command:

git commit -m "Your message about the commit" 

Make sure that the message is meaningful and related to the commit!

 6.  Branching out

Branching allows you to work on different features or versions of your project concurrently. Here are some common commands for managing branches:

To create a new branch called newBranch:

git branch newBranch

To switch to the branch called newBranch:

git checkout newBranch

To create a new branch called newBranch and switch to it in a single command:

git checkout -b newBranch

git branch displays all the branches and shows which branch you are currently on.

Using branches helps keep your work organized and enables easier collaboration by isolating different lines of development.

7.  Create a new GitHub repository 

Select new repository from the + dropdown menu next to your profile picture in the top right corner.

Enter a name for your repository and click Create repository. 

8.  Linking Your Local Repository to GitHub

After ensuring that SSH is selected, go to your GitHub repository page and copy the SSH URL of your repository. It should look something like [email protected]:username/repository.git.

git remote add origin [email protected]:username/repository.git

This command creates an entry in your Git configuration that associates the name origin with your GitHub repository URL. In this case the name is origin. However, feel free to rename it as you wish.

git remote -v

This command will list the remote repositories linked to your local repository, showing the name origin and the corresponding URL.

9.  Creating a pull request

Submit changes to other projects by creating pull requests on GitHub. This allows project maintainers to review and merge your changes.

Fork the repository:

If you do not have write access to the repository (i.e., it belongs to another person or organization), you need to create a personal copy of the repository by forking it.

  • Navigate to the repository you want to contribute to on GitHub.
  • Click the "Fork" button at the top-right corner of the page to create a copy of the repository under your GitHub account.
  • Clone your forked repository to your local machine:
git clone https://github.com/your-username/repository-name.git
cd repository-name

To create a pull request, follow these steps:

i.  Switch to the branch where you have committed your changes.

git checkout newBranch

ii.   Push the branch to remote repository.  

git push -u origin newBranch

This command uploads your branch to the remote repository. The -u option sets the upstream branch, establishing a tracking relationship between your local branch and the remote branch.

iii.  Open your GitHub repository in a web browser and click Compare and pull request.

iv.  Create pull request.

10.  Merging a Pull Request

Merge branches to incorporate changes from one branch into another. 

  • Open the Pull Request: Go to your GitHub repository and navigate to the "Pull requests" tab.
  • Select the Pull Request: Click on the pull request you want to merge.
  • Merge the Pull Request:Review the changes one last time to ensure everything is correct.Click the green "Merge pull request" button.Confirm the merge by clicking the "Confirm merge" button.

11.  Pulling changes

To update your local repository with changes from the remote repository, follow these steps:

  1. Switch to the master Branch: Ensure you are on the master branch or the main branch you want to update.
git checkout master
  1. Pull the Changes: Fetch and merge changes from the remote master branch into your local master branch.
git pull origin master

If you have set the upstream branch, you can simply run git pull.

12.  Merging two branches on your local repository

To merge changes from one branch into another:

i.  Check out the target branch that you want to merge into.

git checkout targetBranch

ii. Merge the Other Branch: Merge the changes from the specified branch into your current branch.

git merge branchX

13.  Viewing all commits made to a repository 

View the commit history to understand the evolution of your project.

git log