Initializing and Pushing

Now we go over creating a git/Github repo and how to push to them.

Creating a git repository and pushing to Github

git init
git add .
git commit -m "message"
git remote add origin <GITHUB LINK>
git push

Let's go over how you would initialize a git repository and push it to a remote. First, we'll create a sample project we may want to use git for. For now it'll just be a folder with a simple text file:

Simple!

Now, we want to initialize a git repository in our project so it can start tracking changes. To do this, simply type git init in a terminal in the same directory as your project.

The next command you want to run is git add . : git does not track all files in your folder automatically! We have to tell git which files we want it to track with the add command. The . symbolizes the current directory. Thus git add . tells git to track all files in our current directory. Your terminal should now look something like this:

Your terminal should now look something like this.

Before we continue, I should explain commits. A commit captures a "snapshot" of the project at this point in time. Once you create a commit, you can go back to that commit anytime and your project will revert back to that saved state. When we eventually push our changes to Github, git will push the changes specified in the last commit.

So, we need to create a commit: run git commit -m "First upload". The -m parameter is required and specifies the commit message. What follows must be a string (double quotes) which contains a short description of what you've changed in this commit. In my example, I write first upload, because quite frankly, it's the first upload of our project. Your commit messages should generally be more descriptive so your teammates can easily understand what you changed.

Now that we've created a commit, we're ready to push that commit to Github. Go to https://github.com/arrow-up-right and create a repository (do not include a README or gitignore). Next, let's tell git which repository we'll be pushing to. Run git remote add origin <GITHUB LINK>, where <GITHUB LINK> is the link to the Github repository you created (do not include the <>). This adds a new "remote" repository (ie, not your machine) which you can now push to and pull from.

Your terminal should now look something like this:

Your terminal should now look something like this.

Finally, we're ready to push to our repository! From here just run git push. Git will then "push" your changes, which are specified in the last commit, to our remote repository. The first time you push you may also need to run an additional command, the terminal will tell you this. Just do what it says and no one gets hurt there won't be any more issues.

Just do what it says and there won't be any more issues.

Your files should now show up when you view your Github repository. Congrats!

Pushing

If you've followed the tutorial up to here you now also know how to push to an existing Github repo. Simply do the following:

You'll notice this is the same procedure that we did earlier, but without git init and remote add origin. We still need to add . before every commit because if we created a new file in between commits, git will not track those automatically. If you did not create new files between commits you can skip git add . and simple add an "a" parameter to the commit command, like so: git commit -am "message".

Last updated