Python 3

home

Getting Started with Github

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 an Anaconda 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.

  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
    





    Perform Your First Commit and Push

    A "commit" adds a new file or changed file to the repo; "push" syncs the change with the server.


    A. Modify the README.md text file to add to the repo. If the file was not created, please create one of this name here. Note the change or the text that you added to this file.
    B. Add the new or updated file. Strangely, the add command refers to either newly added or changed files. When we 'add', we're really adding a change to the repo (whether a new or changed file).

    (base) david@192 demo_20230808 % git add README.md
    

    C. Commit and add the file
    (base) david@192 demo_20230808 % git commit -m "first commit"
    
    You'll see a message ackowledging the changes and the commit.

    D. Push the changes to the github server.
    (base) david@192 demo_20230808 % git push -u origin main
    
    You'll see a series of messages indicating a warning (re: adding a RSA host)

    Having successfully pushed a first file to the remote repository, your local folder is now connected and you can push changes remotely with git push at any time.

    If you make a mistake initially and find you are having problems pushing, you can simply delete the local folder inside repos then delete the github repo by logging into github.com, clicking on the repository, choosing Settings, and scrolling down to "Delete this repository" at the bottom. (The entire repo including history of changes, comments, etc., will be deleted, so it should only be done to start from scratch.) Please follow the steps carefully and let me know if you have any problems -- thanks!





    The git commit cycle: add or change file, commit, push

    This cycle is repeated as we make changes and add these changes to git.


    add or change a file (in this case I modified test-1.py)


    git status to see that the file has changed

    This command shows us the status of all files on our system:  modified but not staged, staged but committed and committed but not pushed.
    (base) david@192 demo_20230808 %  git status
    On branch main
    Your branch is up-to-date with 'origin/main'.
    
    Changes not staged for commit:
      (use "git add ..." to update what will be committed)
      (use "git checkout -- ..." to discard changes in working directory)
    
    	modified:   test-1.py
    
    no changes added to commit (use "git add" and/or "git commit -a")
    (base) david@192 demo_20230808 %

    git add the file (whether added or changed) to the staging area

    (base) david@192 demo_20230808 %  git add test-1.py
    (base) david@192 demo_20230808 %

    git status to see that the file has been added

    (base) david@192 demo_20230808 %  git status
    On branch main
    Your branch is up-to-date with 'origin/main'.
    
    Changes to be committed:
      (use "git reset HEAD ..." to unstage)
    
    	modified:   test-1.py
    
    (base) david@192 demo_20230808 %

    git commit the file to the local repository

    (base) david@192 demo_20230808 %  git commit -m 'made a trivial change'
    [main e6309c9] made a trivial change
     1 file changed, 4 insertions(+)
    (base) david@192 demo_20230808 %

    git status to see that the file has been committed, and that our local repository is now "one commit ahead" of the remote repository (known as origin

    (base) david@192 demo_20230808 %  git status
    # On branch main
    # Your branch is ahead of 'origin/main' by 1 commit.
    #

    git pull to pull down any changes that have been made by other contributors

    (base) david@192 demo_20230808 %  git pull
    Already up-to-date.
    (base) david@192 demo_20230808 %

    git push to push local commit(s) to the remote repo

    The remote repo in our case is github.com, although many companies choose to host their own private remote repository.
    (base) david@192 demo_20230808 %  git push
    Counting objects: 11, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 318 bytes | 0 bytes/s, done.
    Total 3 (delta 2), reused 0 (delta 0)
    To https://github.com/NYU-Python/david-blaikie-solutions
       2ce8e49..e6309c9  main -> main
    (base) david@192 demo_20230808 %

    Once we've pushed changes, we should be able to see the changes on github.com (or our company's private remote repo).





    [pr]