Do you guys remember that Geico commercial with Salt-N-Pepa singing ‘Push It Real Good’? I think about that commercial every time I push to GitHub.
Ok, but really. In all seriousness, I used to have a ton of trouble pushing to GitHub. In my last post, I talked about creating a GitHub repository. In this post, I’m going to explain how to push to it after you’ve created it.
When I was first getting started on GitHub, I seemed to follow this inevitable pattern. First, I would make a repository and push the files I had locally to that repository. Then I would change some stuff around locally, like move the files to a new folder, or something like that and I wouldn’t be able to push to GitHub anymore. I’d delete the repository, (lose all the commits I’d been tracking in the process) and it was an all-around bad deal.
But practice makes perfect, and with much practice, I was finally able to figure out what the hell was up with GitHub. I’m really happy I did, because GitHub is actually pretty cool.
So, I’ll assume at this point that you already have a GitHub repository you’re pushing your work to. If not, check out my first post. Many times when I’ve been discussing GitHub with friends, I’ve been asked whether or not a person should wait until their project is completed and then post it to GitHub. The answer to this is a resounding No.
GitHub has a great function of tracking your changes, and holding on to your history for you. If you aren’t uploading your code as you create it, it cannot track it for you. In addition, it is a great way to back up your code (though you should be doing this with USB drives, as well) and it allows you to share your code with others if and when you get stuck on something.
So, moral of the story is, push your code to GitHub as soon as you have some code to push.
Let’s get started on how you can do this. All directions are for pushing code from the terminal, as is my preference. First open your terminal and go ahead and CD into the directory containing your work.
This command will check the status of your GitHub repository against the status of your local work. If there are differences, such as new files, deleted files, or changes within a file, it will list this out for you in your terminal.
This command will show you the differences between the file in your GitHub repo, and the file you have locally.
git add . OR git add --all OR git add “filename”
The first two variations will add all of the changes (including any deletions) you have made. The third command will allow you to only add changes from a specific file. This can be useful if your settings change between your local machine and GitHub. You’ll be able to add only the changes to the code, and keep your settings the same.
git commit -m “message”
This actually commits the files you have added.
git push origin <branchname>
This will push all of your local changes to GitHub, and then ta-da! You’re all done.
Questions? Tweet me!