Git & terminal - bradsorour/notes GitHub Wiki
Basic 'chain of command'
git status
git add
git commit
git push
$ 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.
$ 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.
Setting up a repository
This is performed via GitHub. 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 (including the quotation marks " ").
$ 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.
What if you already have a project on your computer that you want to upload to GitHub? How do you do this on terminal?
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
Give a name to the repo you want to create. 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. 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.
Adding files with terminal
This is really simple.
$ touch name-of-file
Using the 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