Basic Git Commands

Git init

git init initializes a local git repository on your computer. You do this after you have created the directory (aka folder) that you want to push to remote (Github).

Also make sure that you are actually inside the directory you want to be in. pwd if you have to!

git status

When you have added some files and filled them with some content, type the git status command in Terminal, and hit return. Copy and paste this command in Terminal, if you have to!

The great thing about git status is, that it returns the files that either have been newly created and never staged or committed, or the files that have been modified, including the path to the file!

git add .

When you are ready to commit your changes locally, type git add . in Terminal and hit return, if you want to commit all your files and directories at once.

git add pathtofile

If you don't want to commit everything all at once, you can add (aka stage) a single file, or several files, individually.

If you want to add more than one file, you can type git add pathtofile pathtofile, etc.

git status: screenshot

screenshot of git status command in Terminal

git add pathtofile revisited


git add index.html
                

You could also do something like


git add index.html form.html
                

if there was another new, un-tracked file to add, and another, and so on.

New, un-tracked files always appear red, and newly modified files also appear red.

When a file, or files, have been added to the staging area with the git add command, if you then execute the git status command, the file or files that have been added will show up green. That means they are ready to be committed locally on your computer as opposed to remotely on Github.

Note: In the screenshot in the previous slide, the file that was ready for staging was NOT a new file, but a modified one. That meant it had already been committed at least once previously, and was therefore already being "tracked". "un-tracked" refers to files that have never been added/committed yet.

git commit

So you have staged your file(s), and they are ready to be committed locally. There are 2 ways to do this. You can do:


git commit -m "My first commit"
                

or you can do:


git commit
                

I prefer the second way, because it separates concerns. First I execute the git commit command, which takes me into vim, a cool text editor which comes with most UNIX systems and with macOS. I love using it for git commit messages. Why? Because it is easy to use, and allows you to create multi-line messages, if you want.

You probably want to keep your commit messages to ~50 characters or less long, but it's nice to have options!

git remote add origin remoteurl

So you have committed your new local repository on your computer. Now you are ready to push it to remote origin.

remote origin refers to the remote hosting service you are using to host your repositories remotely.

origin does not even have to be origin. For example, if you push your repository to a hosting service called Heroku for applications with backend servers/backend databases, you would use heroku instead of origin. However, historical convention on Github is to use the origin keyword.

git remote add origin remoteurl (contd)

In order to be able to push your local commits for the first time to remote, you have to associate your local repository with a remote one. However, before you even can take this step, you have to make sure you have a remote repository to push to!

creating a remote repository on Github

To create a remote repository on Github, go to your Github profile, click on the Repositories tab, and then the green New button that appears to the right of the "Find a repository" search bar. This will take you to https://github.com/new, where you will create your new repository.

Creating a remote repository on Github: screenshot

screenshot of creating a remote repository on Github

Creating a repository: repository name

The first step to creating a remote repository on Github is to fill out the repository name field with a repo name of your choosing.

When I created the remote repository for housing the local repository of this slide deck, I input the following in the repository name field:


basic-git-commands
                

It is best practice to create a repository name using hyphens (-) between words. Github might not even accept other character conventions such as _. I have tried it, and Github did not like it at all, so I had to rename the repo.

Create repository

To create your new repository, click on the green create repository button.

Create repository outcome: screenshot

create repo outcome screenshot

git remote add origin remoteurl revisited: SSH

In order to push my local test-repo to Github remote, I would do the following in Terminal:


git remote add origin git@github.com:interglobalmedia/test-repo.git
                

origin is the first argument passed to the git remote add command, and git@github.com:interglobalmedia/test-repo.git is the second argument. It is the remote url of test-repo on Github. In fact, it's the remote url using the SSH protocol as opposed to the HTTPS protocol as shown in the screenshot on the previous slide. That's because I configured my Github account to use SSH. SSH stands for Secure Shell.

For those of you who are interested, I have created a slide deck on how to set up your Github account with SSH. It goes beyond the scope of this course, but to read the slide deck, please visit Getting Set Up on Github with SSH.

git remote add origin remoteurl revisited: HTTPS

In order to push my local test-repo to Github remote using HTTPS, I would do the following in Terminal:


git remote add origin https://github.com/interglobalmedia/test-repo.git
                

origin is the first argument passed to the git remote add command, and https://github.com/interglobalmedia/test-repo.git is the second argument. It is the remote url (HTTPS) of test-repo on Github.

Before you take this step, make sure that you have previously created a personal access token (PAT) on Github.

We no longer can do HTTPS authentication to Github simply by typing in our username and password when prompted to do so in Command Line. We have to create a personal access token on Github, and when prompted for it in Command Line, and to provide it once so that we can push our changes to remote. I have created another slide deck entitled Setting up Github with PAT. it takes you through the steps to set up a personal access token on Github and how to use it.

git push -u origin master

Now that your local repository has been associated with the remote repository, you are ready to push the local repository to remote origin! To do that, type the following git command in Terminal and then press the return key:


git push -u origin master
                

git push -u origin master: breakdown

git push basically results in the upload of your local repository on Github. You are pushing it to your remote origin, which is Github, and to the remote master branch from your local master branch.

branch master is the default branch that is created locally when you initialize your local git repository.

branch master is also where your production ready code for your application resides.

Other branches are created for application features, and when the code has been reviewed and accepted by the development team, it is then merged into the master branch.

Sometimes, the feature branch does not get merged directly into the master branch, but first to a development branch for further testing, then a staging branch, for testing on a remote hosting environment , and THEN, if all goes well, merged into branch master.

The -u flag is used the first time you git push your local repo to remote. The -u flag is the abbreviation for -set-upstream.

git push -u origin master: success!

screenshot of the complete creation cycle of a git repository

git init to git push -u origin master: demo

A few caveats on the previous movie demo

I use ZSH (Z shell) on my computer and not bash. So when I change into another directory, I do not have to use cd because of some custom configurations.

Some of you may be using bash or at least are set up for it. If so, so please use the cd command to change into your project directory. You would type cd development in Terminal instead of simply development, as I do in the demo.

Replace development with whatever is the name of the folder you want to cd into. Same goes for cding into git-test-repo.

Instead of simply typing git-test-repo, you would type cd git-test-repo.

Lastly, when you are creating your remote repository on Github, the green create repository button was a bit cut off. There is a green create repository button at the bottom of the "https://github.com/new" page that you must click on in order to complete the creation of your new remote repository on Github.

Related Resources