Top 6 need to know GitHub Terminal Commands

As an upcoming Software Engineer or as someone who wants to create small time projects on the side or as a hobby, learning the skills of Git and GitHub are an essential for any project. Many developers use these technologies in their every day to day lives and there’s a good reason. GitHub allows you to save multiple projects that you’re working on, gives you the ability to have collaborators so others can work on your code with you, and keeps track of all of the previous versions of your code so if you messed something up you can always revert back. I’m going to show you the top 6 need to know commands for GitHub to get you started as a pro.

The two pieces of terminology I’ll be using are repo and branch. a repo is a repository; aka Project. A branch allows multiple developers to work on a project together without their code overlapping with each other. It’s a segment that brakes off from the original.

If you would like to test them out yourself, the link to my repository(project) will be at the end of my blog.

1. git clone

To get started you go to GitHub and you can do two things.

You can either make a new repo:

Once you created the new repo, then you can clone it down.

Or you can clone down an existing one by copying the url from the green box labeled code:

Cloning down the repo

One you have the url, you can use the following command:

This is how the code looks like (run it without the `less than` and `more than` operator signs

Then it should look like this:

This is a real world example while running the command

And voilà! You’ve successfully cloned down your repo and you’re ready to start coding!

2. git status

The command

If you run the command and haven’t made any changes to the file you’re working in, you’ll get the following message:

Notice how we are in /code/Sandbox-blog when running the command

It states that there’s “nothing to commit, working with a clean tree”. It’s stating that your current repo on your computer matches the save that is stored on GitHub.

But if you created a file, updated a file, or deleted something within the repository file and didn’t use the git add . command(more on this next), you’ll get something along the lines of this:

Oh noo! Red! I created a ruby file called `example-file.rb` but I did not add the changes

All this is stating is that the file on you computer does not match the one stored on GitHub. It’s giving you the status. What ever changes you’ve made AND SAVED is what’s being compared. To make sure your repo on GitHub matches your local save, use the next command.

3. git add

Adding specific files. Using the previous example above, when we ran
git status, we see that our file example-file.rb is being Untracked and we need to use the “git add <file>” command to get it in there. So we run the following code:

git add <file/folder-name>
The bottom two lines are most important here

We ran the code but nothing happened. There was no confirmation or anything like we’ve seen with the others. Lets run the git status command again.

We added that file!

Hey, it changed to green! So what happened?

When you use git add, you store the work you’ve done up to that point as a kind of save point.

KEEP IN MIND: This will not commit any changes to your repo on GitHub. It will only stage that file and prepare it to be committed to GH.

So this command is great and all but, it only works for 1 file at a time. You can add more to the list to commit later but as programmers, we would like to not write so much code, we do it all the time as it is.

Remember to be in the top level of your repo. It works better that way

So now I have two unstaged files. 1 has been modified as the other has been deleted. And now I also have a new directory added! To stage everything at once, let’s use a little cheat.

The command

Now this command here takes advantage of your normal terminal commands. suing the . there at the end of add means “This directory”. Combine that with git add and you get “Add and stage everything in this directory that’s been changed”. Running the command gives us this:

We've added all the changes

Great, so we’ve added all changes we made to the file. Just a small recap about the git add command. You can use the git add <file/folder-name command if you only want certain files to be later added to GH. Or you can use git add . if you want all your changed files to be later added to GH. From here we are now ready to commit our changes and then we’ll be ready to push them up to GH!

BUT THERE’S A BETTER ONE!

This is the ultimate add command. This still is directed towards your repo but it adds everything regarding the repo, even if you aren’t in that specific directory.

4. git commit

Version control is also known as revision control and it is a management system that is responsible for the management of changes to documents, programs, websites, and other things. In this case, it’ll be your program/documents.

When you’re committing your changes, you would use the following command:

Example of a git commit

So when doing git commit, the -m states that you’re writing a message. And within the quotation marks " ", you would write your own message. Best practice is to write something short but relevant in regards to what changes we made. Pressing enter will give us this:

A successful commit

Important: Once you write your message and finalized your commit, you can not change what you wrote in your message so make sure you double check that. So now you have committed your changes and your ready to update GitHub!

5. git push

The command

And the end result:

Now you see that you have that confirmation and you have updated the repo, you can continue coding without fear that your data hasn’t been updated.

6. git pull

The command
Make sure you’re in the repo directory on your pc

When you run the command, all the updated files will get pulled into your computer and then you can open up that file in a text editor and get to coding.

BONUS COMMAND(s)

The main branch is called Master and that’s is where normally all the code gets pushed and pulled from.

There are a few commands that will allow you to create, view, switch to, and delete branches.

git branch

The command

after running we can see all the branches that are in the repo:

There is only the master branch

We can see normally there is only 1 branch called “Master”. If we wanted to create a new branch, we can run the following:

git checkout

Enter the name you want the new branch to be called
making a new branch and checking all existing branches

This command will allow you to create and switch to that new branch simultaneously. Once you swap to a new branch and you wanted to continue coding, you could. But there are a few things that’ll change when you want to commit and push. It’ll be quick, promise.

When you want to commit but you want to commit to a branch, you’ll need to swap to that branch using: git checkout <branch-name> and that will take you to that branch(if it’s already created).

Then you want to use git add . like you would any other time. Then to commit those changes, use git commit -m “<message-here" and then lastly to push the changes: git push -u origin <branch_name>

That’ll push your committed changes into that branch of the repo.

That concludes the Top 6 need to know GitHub Terminal Commands.Thank you for reading my blog. I hope it was informative and helps you have a better understanding of Git and GitHub. The link to this repo is: https://github.com/jztimm/Sandbox-blog

Full Stack Software Engineering Student at Flatiron School New York City