PEB

Phil Elliott Blog

A Comprehensive Guide to GitHub Commands

Main Article Image

If you've just begun learning about Github, here's an introduction to some basic commands every coder should know.

Create or Clone a Repo

A repository is where all of your code is stored. If you haven't created a repository yet, it's important that you do so first before uploading any code. You can visit the site and click on the "New Repository" button.

If you created the repository on GitHub or if you want to work on an old repository, then you will need to clone the repo onto your computer. This can be done by copying the repo URL from GitHub and using the git clone command.

git clone <repository url>

You can also use the command line to create your repository. You will need to use the git init command inside the project folder.

git init <repository name>

You will then need to type in the command below so that you can have access to the repo you created on GitHub. To do this you will need your repo URL.

git remote add origin <repository url>

Add, Commit, and Push changes

The primary function of Git is to preserve the repository's changes. We'll delve into adding saved modifications first. Afterward, we'll talk about committing them. Finally, we'll discuss using the push command to transmit them to GitHub.

Add Code

To make changes and then commit them, we simply issue the add command in the terminal. The command always begins with "add". The second part may be a directory, a file name, or a period to include all changes.

add file.txt 

add folder name

add . 

Commit

A commit is basically a snapshot of your code at a given moment in time. This will allow you to look back and see the exact code that you had in your code base at that particular moment. This can be really helpful if you want to revert back to an older version of your code.

To commit your changes, type the below command. You may include a description of the changes in this commit.

git commit -m "message here"

Push

Now that all of the files have been added and committed, you may push the changes to the repo. You may do this with the git push command.

git push

Branches

Working on a team can be enhanced by using branches. Because different members of the team can work on different sections of the project at the same time, using branches is important. Branches can be used to create a new version of the project and then, once finished, be merged back into the main branch.

To see what branch you're on and what options are available type git branch. To create a new branch, use the command below.

git checkout -b <branch name>

You can also enter a branch using the following command.

git checkout <branch name>

Pull request

In order to apply all of the team's changes to our local repo, we must use the pull command. By executing this command, we will fetch the latest changes that were pushed to GitHub.

git pull

Merging

We can perform a merge operation to incorporate changes from a branch into the main repository. You may also accomplish this on the GitHub website. To merge a branch into the main branch, you must first be on the main branch. The following command may be used for this purpose.

git merge <branch name>

Summary

GitHub is an excellent resource for coders. It can help you collaborate with other developers, track your progress, and it's also a great way to showcase your coding skills. It can be a little bit intimidating when you first start learning how to code. However, once you get the basics down, it can actually be a lot of fun. One of the best ways to learn quickly is by using examples and tutorials that are available on sites like GitHub.