18.9. Project: Git a Remote Repository

In this activity, you are going to create a remote repository and practice pushing changes to it from your local machine.

18.9.1. Step 1: Create a New Local Repository

  1. Launch Visual Studio Code and open the terminal panel.
  2. Navigate into the local_practice directory and create a new folder. You can name it whatever you want, but you will be reusing code from your Turtle Races project.
  3. Use the File menu to open the new folder in VS Code. In the terminal, use pwd to confirm that you are in the correct directory.
  4. Use git init to initialize your project as a Git repository.
  5. Move your turtle_race.py file into the directory. You an do this with terminal commands, or you can drag-and-drop the existing file into the folder.
  6. Run turtle_race.py to confirm that it still works. If not, debug your program to get it running again.
  7. Save and commit the changes.
  8. Check the name of your default branch with the git branch command. If necessary, use git branch -m to change the name of the branch to main.

Note

If you did not complete the Turtle Races project, no worries. Just add a new Python file to the repository. Code a simple print statement inside a loop and run with that.

18.9.2. Step 2: Create a Remote Repository

  1. Login to your GitHub account in a web browser. Click on the “+” button to add a new repository.

    The New Repository link in the dropdown menu at top right on GitHub.

    The New Repository link is in the dropdown menu at top right on GitHub.

  2. To create a new repository:

    1. Fill in the name and description.
    2. Uncheck Initialize this repository with a README and click Create Repository.
    Creating a new repository in GitHub by filling out the form.

    Create a new repository in GitHub

  3. On the next screen, click on the HTTPS button at the top of the page. Next, click the Copy button in the push an existing repository section.

    The page you see after creating an empty repository, with several options.

    Connecting to a repository in GitHub

  4. Go back to your terminal and paste the commands copied from GitHub. These should be very similar to:

    $ git remote add origin https://github.com/username/turtle-races.git
    $ git branch -M main
    $ git push -u origin main
    

    The final command produces quite a bit of output. The final line, Branch 'main' set up to track remote branch 'main' from 'origin' lets you know that the process worked.

  5. GitHub should now have the same files and code as your local project. Click on the project name link at the top of the page. This takes you to a dashboard that shows you what’s stored in the repo. You can see your code by clicking on different file names.

    A repository with one commit in GitHub.

    A GitHub repository with one commit.

  6. Your local and remote repositories are now linked. In the terminal, use the git remote -v command to check for the URL of the remote repo.

    $ git remote -v
    origin  https://github.com/username/turtle-races.git (fetch)
    origin  https://github.com/username/turtle-races.git (push)
    

18.9.3. Step 3: Push to the Remote Repository

Right now, your local and remote repositories match. As you continue working on your device, the two repos become different. From time to time, you need to push your local commits up to GitHub to keep the local and remote versions the same.

  1. In VS Code, add a new text file called README.txt. Inside the file, add a short description of your Turtle Races project.

  2. Save and commit your changes to the local repo.

  3. To update the version stored on GitHub, enter the command git push origin main in the terminal. The output includes information about what’s happening on GitHub. The final lines indicate if the push was successful, and they will look something like:

    To https://github.com/username/turtle-races.git
       51dbfe6..2e6f4fa  main -> main
    
  4. After making the push, you should see README.txt added to the GitHub repository. The contents of that file also appear below the list of file names.

    The turtle-races GitHub repo with README.txt added.

    git push added our local commits to the remote repository.

18.9.3.1. Push a New Branch

Whenever you push changes up to GitHub, the action only affects the current branch. In the steps above, you updated main.

  1. Back in VS Code, use git checkout -b in the terminal to create a new branch called readme.

  2. In the new branch, make some changes to the README.txt file. Save and commit those changes.

  3. Your local repository contains two branches now, but GitHub only knows about one. To add the new branch to the remote, just use its name instead of main in the git push command:

    $ git push origin readme
    
    To https://github.com/username/turtle-races.git
       * [new branch]      readme -> readme
    
  4. After this push, your GitHub project shows a list of branches in a dropdown menu.

    The dropdown menu lists the branches in the repository.

    Select different branches from the dropdown menu.

  5. Click on README.txt to see its contents. Use the dropdown menu to switch between the main and readme branches. Notice how the text changes inside the file.

Note

In Assignment #5, you will learn how to perform a merge in GitHub. For now, we will leave the branches separate.

18.9.4. Step 4: Pull Down Changes From the Remote

If you are working with a team on a project, it is very likely that one of your partners will push up changes to GitHub. When this happens, you need to pull those changes down to your own device.

To practice this, make a change in GitHub and then move those changes to your local repo.

  1. In your GitHub project page, select the readme branch from the dropdown menu.

  2. Click on the README.txt file name, then select pencil icon to edit the file.

    The pencil icon opens the GitHub file editor.
  3. Add some more text to the file, or change the words already there. When done, click the Commit changes button at the bottom of the page.

    The GitHub 'Commit changes' button.
  4. Return to VS Code, and make sure you are in the readme branch.

    $ git branch
    * main
      readme
    $ git checkout readme
    
  5. Enter the command git pull. Just like git push, the output tells you what’s happening to your local files. The final lines indicate if the pull was successful, and they will look something like:

    $ git pull
    Lots of output...
    
    Updating b407366..03f9f4c
    README.txt | 5 ++++-
    1 file changed, 4 insertions(+), 1 deletion(-)
    

    The + and - symbols indicate additions or deletions from the listed files.

After the pull, you will see the updated text appear in VS Code.

18.9.5. Success

Nice work! You now have experience with:

  1. Creating a new local repository.
  2. Creating a remote repository on GitHub.
  3. Linking your local and remote repos.
  4. Pushing and pulling changes between your local and remote versions.