When I first learned about Git and GitHub I assumed it was something like Google Docs - a place to store your code online so that you could access it anywhere, share it and collaborate with anyone also using github. While github is all these things, I was missing the biggest advantage to git and github. Git is a version control platform - meaning that it can save your changes along the way and you can go back to each change that you have saved and revisit it.
I remember when writing my college papers, I would continually save my work in case something crashed and I lost everything. With word processing programs the changes you save are permanent. If I deleted a paragraph or changed any aspect of the paper and I wanted to go back to what I had before, I would have had to make a separate document which would have gotten very messy. However, If you were using a version control program like git, you could get your changes back much more easily. While working on that paper you could have “committed” or (saved) the changes along the way and like magic you could get back any of these changes!
Version control is an amazingly useful however, because it is executed through the command line (or terminal) it can be kind of confusing at first. So what are some basic git commands to know if you want to use git as your version control?
$ git init
- This creates a new repository, (or “repo” for short), which basically tells your computer you are going to use git.
$ git status
- Running this command will tell you exactly what is happening in git. Are there files that you have modified and haven’t been saved within git yet? git will let you know if you run this command.
$ git add file_name
or $ git add .
-This command tells your computer that you are about to save a file. It will put the file in a staging area that tells the computer, “Hey - I am about to tell you to save this file”. If you want to save all the files that have been modified you can use $ git add .
$ git commit -m “your message here”
- Here you are saving the files with a little message. This message is for you to look back on and remember why you made changes and why you are saving them at this stage in your project.
$ git log
-This command allows you to see all the commits or times you have saved your files throughout your project. You can look through them and if you decide you want to go back to one of these versions of your project it is easy to do so.
Using Git with GitHub
Github is the online or remote location you can store your files so that you can share them, save a copy remotely, and more easily see the changes you have made each time you commit a change.
You have to first create a remote repository in your github account that has the same name as directory where you have stored your files on your computer. Next you can start “pushing” or sending your files into your github repository. Here are the commands to know in order to do this.
$ git remote add origin https://github.com/hannahlw/my_project.git
- (the url should be the url of your remote repo). This command first tells your computer that there is a remote place for it to store your files you are saving with git. Next it names that remote repo “origin”. Finally, it tells the computer where the newly named repo is located by giving the url.
$ git push -u origin master
- This command will actually send or push the files to your github account.
Now you should now see your code in your GitHub repository!
Happy committing!
Hannah