Essential Git and GitHub for Beginners

Authors: Ali Shahed, ChatGPT

Ali Shahed
ML Hobbyist
Published in
4 min readFeb 26, 2023

--

Why do we need yet another Git/GitHub tutorial? Well …for one because it is easy to create a proper one with the help of ChatGPT. Secondly, I wanted to write a Git/GitHub tutorial for whoever wants to follow my later tutorials on various topics and bring them up to speed really fast on this topic. Let me know … sorry!… Let us know how we did.

Now that we addressed this giant elephant in the room, let’s start the journey.

Introduction

GitHub is a popular platform for version control and collaboration on software projects. Whether you’re a beginner or an experienced developer, knowing how to use GitHub effectively can help you manage your code and collaborate with others more efficiently. In this article, we’ll go over the basics of how to work with GitHub, including how to clone, commit, push, pull, branch, and merge using the command line interface (CLI). If you are not comfortable using CLI, there are tools like GitHub desktop, Source Tree or GitKraken that can provide you with smoother entry into Git and GitHub world with their graphical user interface.

What is GitHub?

GitHub is a web-based platform that provides version control for software development. It allows developers to host and review code, manage projects, and collaborate with other developers. GitHub uses Git, a popular version control system that helps developers keep track of changes to their code over time.

Getting Started with GitHub

To get started with GitHub, you’ll need to create an account on the GitHub website. Once you’ve created an account, you can create new repositories, clone existing repositories, and start collaborating with other developers.

Setting up GitHub account is fairly straight forward but in case you need help, you can check out one of the numerous tutorial on that subject, e.g. this one.

Clone a Repository

To clone a repository from GitHub to your local machine, you can use the “git clone” command. This command will create a copy of the repository on your local machine, which you can then modify and push changes back to GitHub.

For example, let’s say you want to clone the “hello-world” repository from GitHub. Here’s how you can do it:

$ git clone https://github.com/username/hello-world.git

Commit Changes

Once you’ve cloned a repository, you can start making changes to the code. When you’re ready to save your changes, you’ll need to commit them to the repository. To commit changes, you’ll need to stage your changes using the “git add” command, and then commit the changes using the “git commit” command.

For example, let’s say you’ve made changes to a file called “index.html” in your repository. Here’s how you can stage and commit your changes:

$ git add index.html
$ git commit -m "Updated index.html file"

Push Changes

After you’ve committed your changes, you’ll need to push them back to the GitHub repository. This will update the code on GitHub with your changes.

For example, let’s say you’ve made changes to the “index.html” file and you want to push those changes back to the GitHub repository. Here’s how you can do it:

$ git pull origin master

This command will pull the changes from the “master” branch of the GitHub repository.

Branching

Sometimes, you’ll want to work on a new feature or fix a bug without affecting the main codebase. In this case, you can create a new branch in the repository. A branch is a separate version of the codebase that you can modify independently of the main codebase.

To create a new branch in the repository, you can use the “git branch” command. For example, let’s say you want to create a new branch called “feature-branch” in your repository. Here’s how you can do it:

$ git branch feature-branch

To switch to the new branch, you can use the “git checkout” command. For example:

$ git checkout

Finally, you can see all the branches that you have in the repository using “git branch” for local branches,

git branch

For remote branches,

git branch -r

And finally, for all branches, local and remote,

git branch -a

Merge Branches

Once you’ve made changes to a separate branch, you may want to merge those changes back into the main codebase. To merge branches, you can use the “git merge” command.

For example, let’s say you’ve made changes to the “feature-branch” branch and you want to merge those changes back into the main “master” branch. Here’s how you can do it:

$ git checkout master
$ git merge feature-branch

This command will switch your local machine back to the “master” branch and merge the changes from the “feature-branch” branch into the “master” branch.

Conclusion

In this article, we’ve gone over the basics of how to work with GitHub using the command line interface. We’ve covered how to clone repositories, commit changes, push changes back to GitHub, pull changes from GitHub, create and switch between branches, and merge branches. These are the essential Git commands that every developer should know when working with GitHub. By mastering these commands, you can effectively manage your code and collaborate with other developers on GitHub.

If you buy me a coffee, I can work longer hours and create more content like this. Thank you!

--

--

Ali Shahed
ML Hobbyist

PhD EE | Data Scientist | Machine Learning Engineer