Getting Your First Project On GitHub
Git is a staple of the development process. It has effectively replaced almost all of the older choices of source control. GitHub is a platform for storing files and projects in the cloud. Git is often confused with GitHub, but they are different and not related at all. Let’s discuss what the differences are, and how to use them.
Git is a technology that can manage files locally,
GitHub is a platform for storing files in the cloud
Git can be found here: https://git-scm.com/downloads.
1. Download the correct version for your operating system
2. Install it, accepting the defaults through the installation wizard
3. Once installed, open up a terminal
Create a fake project
mkdir fake-project
cd fake-project
Create a file in the project directory called foo.txt
touch foo.txt
Open this file in any text editor, add some text to it, save and close it.
Turn your project into a git project
git init
If you browse to your project’s directory you’ll now find a new folder called .git/
. If you were curious, you could open and explore this directory, but for 99.99% of use cases, you’ll never need to know anything about it.
Now that we are in a valid git project, we can take advantage of other git commands inside the terminal.
git status
After getting the status of the project, git tells us that it found one file, but that it’s untracked, meaning, it hasn’t been added to the commit history of this project yet. Untracked files will not be seen by anyone else that clones your repo from some remote host. It will remain on your local disk only. This is important because we can take advantage of that to keep local credential files, or artifacts from compiling a project, out of the final git project.
Git conveniently recommends we git add
that untracked file.
git add
will stage our file. Files that are staged still aren’t in the final git project history. Consider this a holding place for preparing a commit. Now we can commit this file to our git project history.
git commit -m "My first commit!"
Now we are issuing a commit
command to git. The command line argument we’re using is called -m
, or --message
. This is a convenient way to add a message to your commit. This message will show up in the history of commits on your project, viewable by anyone and important for knowing who changed what and when they did it.
Now let’s run our handy git status
to get the state of our local working directory.
Congratulations! You’ve successfully added a file to a git project and committed it to its history! From here we can view the prior commits with git log
if you were curious as to what a history looks like.
That sums up our steps for getting a local git project going. Now let’s move on to getting this project up on GitHub.
Pushing your local git project to GitHub
First we need to create a repository in GitHub. This will serve as the “virtual directory” that matches the one you had locally.
1. Head over to https://github.com/ and create an account or login
2. In the upper right-hand corner, drop down the plus (+) sign and select New repository
3. Fill out the details on the create repository page
I have left the options to add a
.gitignore
and alicense
off. I prefer to manually create the repository. These are useful if you plan on starting your project from GitHub and working in reverse from what we did.
Click Create repository
and you’ll be brought to a quick setup page. GitHub provides you with a few ways to get your local files up into this new repository in the cloud. I prefer the one labeled …or push an existing repository from the command line
Open up your terminal and navigate back to our project’s root directory. Run a git status
to make sure nothing has changed since we were last here. If all is well, then we’re ready to push our repo up to GitHub!
git remote add origin https://github.com/mariocatch/fake-project.git
git push -u origin master
These commands that were provided by GitHub will do two things.
1. You will add a link from your local working directory to the remote one we created on GitHub a few moments ago. This is basically like connecting a long chord from your directory to a plug on GitHub, so now you have a link to that repository.
2. The second command is what pushes your local commits to the remote link we just created in the last step.
You may be prompted for credentials if this is your first time. Follow the flow provided to you and you’ll be all set.
Head back over to your browser and refresh (F5).
You’ve done it! You have taken a local directory from your computer, turned it into a git project, added your local files to that project, committed them to that project’s history, and pushed your changes to a remote repository on GitHub.
This is just the start of your journey. Play around with adding new files locally, committing them, and pushing them to the remote repository. The typical development workflow will look like this:
git add .
git commit -m "Some commit message"
git push origin master