• Basic ‘chain of command’ using git.
This is what you do to update your files on your computer (local) to GitHub (host). These commands assume you have already set up a repository (repo). Section 3 covers, creating a repo.
This is the basic chain of command you will use every time you update your files to GitHub.
$ git status
This asks terminal what is the current state of play with all your files in your repo. It will highlight everything that has changed in red. Usually, this is divided into two sections — changes made to existing files and new files created. If you have deleted files this will also show.
$ git add .
This will add ALL updates made to ALL files and will also add ALL new files created. That is what the full-stop does — basically ALL. You can additionally just add the file name instead of the full-stop to add individual files. For example…
$ git add index.html
Once you complete this command and performed another git status, everything will turn green to indicate files/changes have been added. Important to note, although they have been added they still need a final confirm action.
For those interested, but not important for this blog piece, there are two other related commands ```git add -A``` and ``` git add -u```. I’ll let you explore the differences here.
$ git commit -m “write your own message”
This is the confirmation. Everything has been added and you have told terminal to send the updates to your repo on Github. The commit is basically you saying ‘yep I commit to this action — confirm’.
Notice it is good practice and mandatory to write a message, this is the -m part, (m) for message. Always include the message inside of quotation marks. The message is basically communication with others who may also be working on the same file.
$ git push
This is your final command. Everything is ready and you have confirmed. Now to just press the send button and off it goes — your repo is now fully up to date.
Git status is just a visual helper and is not expressly needed to update your repo to GitHub. Steps 2–4 are needed every time, however.
• Navigating within terminal.
Here are the commands to search and locate your repository files.
$ pwd $ ls $ ls -a $ cd $ ..
pwd = print working directory. In other words, tell me where I am. It will list the hierarchal file tree including the directory you are in.
ls = list. This command will list all the visible files in that directory. Does NOT show hidden files.
$ ls -a
ls = list and list all hidden files. Will show all invisible files as well. By default, invisible files are not shown in terminal.
$ cd your/file/location
cd = call directory. This is basically the command telling terminal I would like to go to this directory. It does not work just writing your repo name. It is not like spotlight or search. You have to write the entire hierarchal tree or file pathway. By default, terminal will always start you in the very root file of your computer system. This is defined by this symbol ‘~’.
So an example… I called my repo ‘pigs-can-fly’. For some reason, I put it in loads of other files. Let’s say this is the tree pathway to get to my repo ~/documents/my-files/pigs/pig-pics/another-pointless-file/pigs-can-fly. This is the full path and so, therefore, this is the full command
$ cd documents/my-files/pigs/pig-pics/another-pointless-file/pigs-can-fly
I don’t need to write ‘~’ because I will automatically start in that repository. Unfortunately, you cannot just write
$ cd pigs-can-fly
These two commands
.. will navigate you between repositories. Say I am in ‘pig-pics’ and I type cd I will go straight back to the root file ‘~’. If I type
.. I will go back one directory — so I’ll end up in ‘pigs’.
That is navigating with terminal.
• Setting up a repository git Github.
This is performed via GitHub.
On GitHub go to
Profile Page > Repositories Tab > NEW (green button) > Name the Repository > Add a README file (good practice) > choose whether to add .gitignore file.
README file is basically a log or diary of what you are doing or making. .gitignore is a file that tells your repo to ignore other certain files. Read more here.
Once you have set up your repo, open it, and then press the green clone button. Copy URL inside.
Now we need to link GitHub to your computer. We don’t have to create any files on your computer. Simply open terminal and type this command.
$ git clone “url just copied”
URL goes inside quotations and job done. Terminal will create a file with the same name as your repo. Important to note this file will be placed in your master or root file on your computer (usually this file is named after you).
If you want the repo to be saved in another location make sure you are in that directory before you perform the clone command. So if I want my ‘pigs-can-fly’ repo to be saved in the ‘another-pointless-file’. I have to make sure I have navigated to ‘another-pointless-file’ first.
NOW, WHAT IF… you already have a project on your computer that you want to upload to GitHub? How do you do this on terminal?
Uploading from computer to Github.
First, make a repo on GitHub and copy that URL. These are the commands to familiarise yourself with.
$ mkdir $ git init $ git remote add origin $ git push origin master $ mkdir name-you-will-give-repo
Mkdir = make directory. Give a name to the repo you want to create. In my case…
$ mkdir pigs-can-fly
YOU DO NOT need to do this step if you already have a file or project started that you want to add.
Once created, you will then need to transform the repository into a git repository. Going a bit gangster we put ‘git in it’. Type the command.
$ git init
Then you run through the chain of command mentioned at the beginning. Status, add and commit (exactly as before). The ‘push’ part is slightly different here.
git remote add origin “url of repo created in GitHub” This command is basically saying, ‘go to GitHub and add the origin file at this URL. After this your files on your computer will now be connected with the repo on GitHub.
$ git push -u origin master
This is the last command, push. Slightly different than before because you have to specify where you are pushing to. In this case, you are pushing to the master branch of your repo. Repos can have many different branches, but when you have just created a repo it will always default to add it to the master branch.
Note these last two steps mentioned only need to be performed once in the initial setup of a repo. Once you have synced your files with GitHub, you then revert back to the chain of command as highlighted at the beginning.
• Adding files with terminal.
This is really simple.
touch command we can create any file within our repo without having to leave terminal. If I want to make an index.html file I type this command.
$ touch index.html
You can then do an ‘ls’ command to see the file you just created. You can create any file, for example, stylesheet.css, README file and even a .gitignore.
• Final conclusions of git.
Now it is fully possible within some text-editors to have GitHub integration. I use Atom and within that, I can use a few shortcut keys to instantly add, commit and push my changes to GitHub. I never need to open terminal. The package is called Git-plus.
I would however strongly recommend that you get used to using terminal, especially if you are serious about back-end dev.
To keep this piece really simple, which was the aim, I have omitted merging, forking, and conflicts within repos. I have also not explained the .gitignore file.
To be honest at an early stage when just beginning with GitHub you are unlikely to need anything more advanced than this.