Python 3

home

Git Part I: Getting Started with git and GitHub

Confirming That git is Installed

git may already be installed, or may need to be installed.


First, check to see if git is available. At the Terminal or Command Prompt window, issue this command:

git --version

If you see a version number, then git is installed. If not, you may need to install it. On later Mac computers, the above command may conclude with a prompt offering to install the Mac Command Line Developer Tools. These tools include git, so this is the easiest way to install from a mac. However, if you see a message that indicates an error or problem, this means taht git is not installed. See next slide.





Installing git

On Mac: If git --version was not found, but did not prompt you to install git, then you must install it.


On a Mac it is easiest to install the Xcode Command Line Tools. In your open Terminal window, type this command and hit [Enter]:

xcode-select --install

On PC: please visit https://git-scm.com/download/win and download the git installer .exe file. Doubleclick and follow the prompts. Please note that these instructions are new, so let me know if you run into any issues.





Creating a New Repository on github

A repository is used to house a codebase.


A codebase refers to the code and supporting files that make up the software projects for a company or for a team, or may refer to a single software project or module, or the code being developed by an individual. A codebase could be developed over the course of years by a number of individuals. A code repository is a program that stores copies of the files in the code base and keeps track of the changes (deltas) that are made to them over time. It is possible to look back to how a given file looked at any previous date, and restore that file to a previous state, as desired. The repository also can manage changes to a code base that may be made by multiple participants -- it then allows those changes to be merged together; in this way it is an excellent collaboration tool. To create a new repository on github.com:

  1. If you do not yet have one, create a free account on github.com
  2. Log into your github account.
  3. Look for the green 'Create repository' button. If you have already created one, you may need to return home: click on the 3 lines at the top left and select Home. On the home page you should be able to see 'Create a new repository'.
  4. Name the repository (up to you); leave the 'Public' option selected ('Private' requires a paid account).
  5. Check the 'Add a README' file
  6. Do no choose to add .gitignore or license.
  7. Click the green 'Create repository' button


Github creates your project and lands you on the project page, with the name near the top left.





Generate and Install a New ssh Key

The "secure shell" (ssh) protocol allows us to validate a user, similar to a password.


"ssh keys" refer to a private key + public key arrangement -- the private key is held by the user, and the public key is placed on the server. Only holders of the private key will have access to the account. Under this encryption scheme, the server can validate this user using the public key, but the public key cannot be used to generate a private key, so it remains secure with the user. A. Generate a new ssh key pair.

  1. Open a Terminal or Command Prompt window (Windows users can use either a regular Command Prompt, or a Command Prompt).

  2. Execute this command, substituting your github email address (note that in all examples below, I'm showing my command prompt - yours will be different):
    (base) david@192 ~ % ssh-keygen -t ed25519 -C "your_email@example.com"
    The ed25519 key type is recommended by github.

  3. ssh asks where you would like to save the file and a default folder and file is displayed - you can confirm the default, which is usually /Users/[yourname]/.ssh/id_ed25519 (On Windows it would be C:\Users\[yourname]\.ssh\id_ed25519.)

  4. (note that if you created SSH keys previously, ssh-keygen may ask you to rewrite another key, in which case it is recommended to create a custom-named SSH key. To do so, modify the default file location and replace id_ed25519 with your custom key name)

  5. You can enter a passphrase, but I think for this there is no need - so you may hit [Enter] twice.

  6. The keys are generated and the filenames used are displayed. Make note of the location and name of the public key file, which should end in .pub My output looks like this:
    Your identification has been saved in /Users/david/.ssh/id_ed25519
    Your public key has been saved in /Users/david/.ssh/id_ed25519.pub
  7. (Note that on Windows, the paths displayed may contain forward slashes, where the Windows command line only recognizes backslashes in filepaths. Replace any forward slashes with backslashes when you use the .pub path in the next command.)

  8. Display the contents of the public key by executing the following command (keep in mind the filepath should match the path to the public file shown in the ssh command output):
    • On Mac/Unix:
      (base) david@192 ~ % cat /Users/david/.ssh/id_ed25519.pub
      
    • On Windows:
      C:\Users\david> type C:\Users\david\.ssh\id_ed25519.pub
    • The output from either of these commands should be a single line starting with ssh. Copy this line to clipboard.


(Note that if on Windows you see "the format of the command is incorrect" it may be because your path contains forward slashes. Windows only recognizes backslashes in filepaths - simply replace all forward slashes with backslashes to proceed.)

B. Install the public key on the github server.

  1. Click on the green 'Code' button -- a dropdown appears.

  2. Select SSH. A warning appears saying "You don't have any public keys..."

  3. Click the link 'add a public key'. Github displays a page titled Add new SSH key

  4. Enter a title (just for reference)

  5. Paste in the SSH public key you copied earlier

  6. Click "Add SSH key". The 'SSH Keys' page displays, showing the key that you added.





"Clone" (Establish a Copy) of your Repository Locally

"Cloning" connects a folder on your local machine to the repo on the server.


A. At the terminal, create a new directory to hold your github repo(s); cd into that directory. 'repos' is a reasonable name; you can also call it something convenient to you.

(base) david@192 ~ % mkdir repos         # (where projdir is the name of your repository)
(base) david@192 ~ % cd repos
(base) david@192 repos %


B. "Clone" the repository you created.

  1. Return to the repo you created. You may need to click on your username, then 'Repositories', then the name of your repo.

  2. Again, click on the green 'Code' button. A dropdown appears.

  3. Make sure ssh is selected.

  4. Copy the git@github.com path by clicking the copy button next to it.

  5. Return to the terminal, and issue the following command, pasting in your path after 'git clone':
  6. (base) david@192 repos % git clone git@github.com:davidostest/demo_20230808.git
    
    If this is the first time connecting to git, git responds with the following prompt:
    Cloning into 'demo_20230808'...
    The authenticity of host 'github.com (140.82.113.4)' can't be established.
    ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
    Are you sure you want to continue connecting (yes/no/[fingerprint])?  yes
    
    Type yes and hit [Enter]. This creates a special .git directory here. You'll see a message to this effect.

    (Note that if you see a DNS SPOOFING/DOING SOMETHING NASTY warning, you may need to delete the github.com line from your known_hosts file. Let me know if you're not sure what to do in this case.)

  7. Do an ls or dir, listing the contents of the repos directory. You should see a new folder there; this is the folder that has been created. cd into this directory:
    (base) david@192 repos % ls
    demo_20230808
    
    (base) david@192 repos % cd demo_20230808
    (base) david@192 demo_20230808 % ls
    README.md
    





    [pr]