-
Notifications
You must be signed in to change notification settings - Fork 2
Using Git
To use git, I will demonstrate with Git Bash on Windows. Git can be downloaded and installed here: https://git-scm.com
Go to https://git-scm.com and download the Git packages for your OS.
The first time you load Git Bash, you will start in your /home directory. For Windows users, this is your C:\Users\ directory. NOTE: For Windows users, you can denote C:\Users\ as /c/Users/.
Your Git Bash window should look like this.
Create your Projects folder using the mkdir command (make directory). To verify this directory was created, use the ls -d */ command. The ls command is a general list command with the -d (directory) flag to only show directories. We manually specify the current folder we are in with */.
While a small step, this is to illustrate how to navigate your file system. Use the cd command as shown to change into your Projects folder. To go back to your /home or /Users/ folder, you can either use the cd ../ command to change to the previous directory or cd ~ to change to your home directory.
Create the BioWebsites folder, cd into this directory, create a folder using mkdir with your GitHub username, then cd into this directory. Once you have done this, we will be initializing a local repository using the git init command. This local repository is a version control tracking system stored and maintained on your local machine. We will look at adding connectivity with the BioWebsites repository in a later step. Take note of the hidden .git folder and the new (master) flag on your command line. (master) denotes your current checked out branch. More info on this in later steps.
NOTE: While this step is broken down into individual commands, some of these commands can be done with one command.
Now that our local repository has been created, we need to get authenticated with GitHub so that we can sync our code wih the GitHub BioWebsites repository. The first step is using the ssh-keygen command as shown in the example. I recommend using your GitHub email and a strong password. This SSH key is used to authenticate you with GitHub so that you can push your code to the BioWebsites repository.
Once you have entered in your password and specified the directory to store your RSA key (I used a slightly modified version of the default path, you can use the defaults if this is your first time.), a SHA256 fingerprint and randomart image are generated. These are blurred for security purposes. Don't share them!
NOTE: You may add your SSH key to the SSH agent so that you do not have to enter your SSH key password with every commit/push command, but I will not be demonstrating that in this tutorial.
Navigate to the directory where you stored your SSH key. You may need to enable viewing hidden files and folders to see the .ssh directory. Once in this folder, open the file with .pub extension (shown as recognized by Microsoft Publisher above) with a text editor (VS Code shown). Copy the key in your text editor to the clipboard.
Login to your GitHub account at https://github.com, go to Settings (shown), then SSH and GPG keys.
On this page click the green New SSH Key. Give your key a recognizeable name, mine was named GitHub - BioWebsites, then paste your key in the large Key text area and click Add SSH key.
Now that we can authenticate with GitHub and we've created our local repository, lets create our first file! Since this was created with the BioWebsites repository in mind, create an HTML file named index.html as shown above. Save this file in the directory you initialized as your local repository. Use the ls -l list command as shown to verify the file is in your local repository directory.
NOTE: Your changes may exist in the folder but the local repository is not aware of the file yet!
We've created our first file and are ready to commit it to the repository. Try using the git commit command as shown. Take note of the message it returns. This means that when you tried to commit your files, you had not added them (otherwise known as staged) to the commit package. To add (stage) your files for commital, use the git add index.html command. Then try and commit again, this time use the -m flag to add a message to the commit. Take note of the return message. It shows what has been added to your local repository and the changes that were made.
Before creating your branch, first understand why we do this. Branching in repositories is a way to separate unstable code from stable code. Generally, once you have some code written and your application/site stable, you push your commit to the master branch, then create additional branches to keep the master branch stable. Once your development branches are tested, reviewed, and confirmed stable, you then submit a pull request to merge your development branch to the master branch. This is basic version control. :)
Now with the basic purpose explained, create your first branch using your GitHub username with the git branch [name] command. You'll notice that we are still on the (master) branch afterwards. This is due to the fact that Git makes no assumptions when you give it commands. You must be explicit with all your commands as a precautionary measure. To switch to your new branch, you must check it out as shown with the git checkout [branch] command. After checking out your new branch you will see (master) change in the context.
Let's go back to the index.html file and add one line to it. This will be what we push to the BioWebsites repository.
Now that we're using our new branch, stage our new change with git add --all (the --all flag stages every file that was changed), then create a new commit with this command: git commit -m "Our first commit to push to the remote repo"
In order to push our change to the BioWebsites repository, we must first add a remote to our local respository. This remote is used to direct our git push command to the "remote" BioWebsites repository. To add a remote to our local respository, use the git remote add origin https://github.com/CoderBoxes/BioWebsites.git command. To verify that the remote was successfully added, use the git remote -v command and it should return a fetch and push origin. We're almost there!
Let's try using the remote we added in the previous step. Try the git push command and look at the message returned. This message is telling us that the remote repository (BioWebsites) does not have our new branch (tptmike branch shown). So we need to push our branch with our commit together using the git push --set-upstream origin tptmike command as instructed in the return message. This will create our branch in the BioWebsites repository and commit our change to it as well. Any further commits we push to the BioWebsites repository won't need us to set the upstream branch as it now exists.
Our commit should now be visible in the BioWebsites repository for others to see. Navigate to https://github.com/CoderBoxes/BioWebsites and change branches with the select box as shown.
Once changed to your branch, you can see the files you added to the repository and the commits you made that show changes to the files in your branch. To view the files, simply click the link to index.html
You can see the raw contents of the file in the next page. Do explore the repository and get familiar with the links and tools available. More information regarding pull requests and code review will come in another Wiki article.















