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
git clone lets you copy your own(or someone else’s) repo and clone them down to you computer. You will then have a copy of that entire file and all of its sub-files locally on your pc. What ever changes you make to that file will not effect the original(yet) unless you are a direct collaborator on that project or you make a separate branch.
To get started you go to GitHub and you can do two things.
You can either make a new repo:
Or you can clone down an existing one by copying the url from the green box labeled code:
One you have the url, you can use the following command:
Then it should look like this:
And voilà! You’ve successfully cloned down your repo and you’re ready to start coding!
2. git status
The git status is used to see all the important information regarding the current branch you are in (we’ll get more into branches later on). It compares what’s saved on your local machine to the save that is stored on GitHub. This command only works if you are in your repo file.
If you run the command and haven’t made any changes to the file you’re working in, you’ll get the following message:
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:
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
git add is very straight forward. You are adding the changes you want to make and making it clear that this/these are the files you want saved. There are two different ways of running this command.
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:
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.
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.
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.
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:
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
git commit is one of the most important commands and it’s the second to last step we need in order for our changes to actually be saved on GH. This command acts like a checkpoint in Mario bros. You haven’t completed the end goal (which is updating the repo) but you’ve saved your progress until then. It takes your staged files and puts them in a position where it’s ready to be sent off into the mail box. Another great thing is that this feature also utilizes version control.
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:
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:
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
git push is exactly as it sounds. You’re pushing your committed changes and updating the save on GH. This command only works if you have things committed.
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
So let’s say you pushed all your commits and you are working on a project with someone else. And let’s say they worked on the code and they pushed up their changes, how would you get that updated code without needing to clone down that repo again? git pull is the way. Make sure you’re in the repo file directory in your terminal and type the following command:
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.
So you’ve made it this far? I’m glad to know! As a reward for getting this far I’d like to introduce to you a command that’s a little more confusing. Before I mentioned branches and branching. GitHub brings together the world’s largest community of developers to discover, share, and build better software. To exchange ideas and to deviate and work off of it. One special feature of this marvel is branching. If you’re a collaborator and you want to test some things out without touching the main application, branching is the way to go. When you branch, you break off from that point in the code and create a sub directory that allows you to tamper with the original code and build off of it.
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.
This command allows you to view all the branches the repo has.
after running we can see all the branches that are in the repo:
We can see normally there is only 1 branch called “Master”. If we wanted to create a new branch, we can run the following:
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