This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Remote repos

Sharing your version history through a server

Remote repositories in Git are repositories stored elsewhere than on your computer, usually on a site like GitHub or a private enterprise server for your company. Remote repositories have a few key purposes:

  1. Remote repositories are the mechanism by versions can be shared between computers, e.g., between a lab and home computer or between the computers of multiple teammates collaborating on code.
  2. Remote repos maintain a copy of your version control history so that if disaster strikes your computer, you have a backup of your project.

Remote repositories are a hub to which multiple local repositories are linked. They function the same as a local repo, but the user takes extra steps to share changes with the remote and to retrieve changes, perhaps made by teammates, from the remote.

1 - Scenario 1 - Sharing a new project

You make a new project on your computer that you want to save to GitHub

Scenario: You are on your computer. You make a new project and begin working. You decide you want to keep the project under version control with Git.

Create the local repo and save an initial version

  1. Create a new directory called remote-sample in your seng-201/ directory.
  2. Open the remote-sample/ directory in Visual Studio Code.
  3. Create a file named test.py. Put some code in there, like print("We are going to share our new repository")
  4. Run git init to create a local repository.
Initializing a new local repo
  1. Now stage and commit the changes.
    Commiting version 1

You now have one version in the local repository, and the main branch (as well as the HEAD) are pointing to that version. I have left the INDEX and the HEAD out of the illustrations since we will not need them for this lab.

Create a “blank” remote repo on GitHub

  1. Browse to https://github.com and log in if necessary.
  2. Find and click the green button to Create a New Repository: New Repo Button
  3. On the “Create a new repository” form, enter remote-sample for the Repository name: repp name
  4. Leave all the rest of the options as-is.
  5. Click the green Create repository button at the bottom.

You will see a page that looks like this:

New GitHub repository confirmation page

Make a note of the URL in your browser bar. Your repo can be accessed from this address.

Leave the browser window open. We will return to it in a minute.

Public vs. Private Repos: You have the choice to make your repo Public or Private when creating it, and you can change this setting later.

  • Public repos are visible on the Internet. Anyone can view the website and checkout your code. Only you can commit code however.
  • Private repos are only visible to you when signed in. Only you can checkout and commit to the repo. You can control more finely if you want specific users to have read or write access to your repo through the Settings tab on the GitHub repo website.

Connecting the local repo to the remote repo

We have created a local repo with git init and created a “bare” remote repo using the GitHub website, but the two are not yet connected!

A new local and remote repo, but not yet connected

On your GitHub page in the browser, you have a section that looks like the following:

Instructions for pushing an existing repo
Copy that code for your repo and paste it into the Terminal. Run those instructions in the Terminal.

You should see output similar to the following:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 260 bytes | 260.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/llayman/remote-sample.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

That means you are good and your local repo is connected to the remote repo on GitHub.

If you see an error like this:

error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/llayman/remote-sample.git'

You forgot to git add and git commit your first version.

Viewing the remote repo

Refresh the GitHub page in your web browser. You should see something like this now:

GitHub repo page showing the first version

This is GitHub’s rendering of your remote repository! In Git, the remote repo looks just like the local repo on your computer. This is just how GitHub chooses to display it.

  • You can click on test.py to see the code.
  • Note that we are in the main branch as indicated in the top left dropdown.
  • You can click on the commit version, e.g., fb080da, to see all the changes in the most recent commit.
  • You can click on the history-clock icon clock.png next to the version name to see the main branch’s version history. There’s only 1 version right now.

Understanding the commands

You pasted three separate commands in the Terminal.

git remote add is what actually create a link between your local repo and the remote repository. Creating the remote repo link does not automatically share any version history or changes.

linking local to remote using git remote add

git branch -M main made sure the name of your default branch was main as opposed to master.

git push is what shared the version history from your local repo to the remote repo:

Git push

A few things happened to the repo state during this process.

  1. Your local repo now has a notion of an “upstream” remote repo that it is linked to.
  2. The version history of your local repo was pushed to the remote repo, including the branch name main.
  3. The remote repo on GitHub now has the entire version history of the main branch, and knows which version main refers to.

Again, the remote repo is behaves exactly the same as your local repo internally. It’s just that it saved to a GitHub server, and you need to run an additional command, git push to share your changes with the remote repo.

Knowledge Check

  1. (Question) What is the purpose of running git init?
  2. (Question) How do you connect a local Git repository to a remote repository?
  3. (Question) Explain the function of git remote add.
  4. (Challenge) Create a local repository and link it to a newly created GitHub remote repository.
  5. (Challenge) Stage, commit, and push an initial version of a project to a remote repository, verifying success through the GitHub interface.

2 - git push

Manually sending changes from the local to the remote

We showed in Scenario 1 that the git push command was necessary to share the version history from the local repo to the remote repo.

Sending changes to and pulling changes from the remote repo is always manual, just like staging, committing, and merging are. This is a good thing because it allows you to decide when to share changes or integrate changes from your teammates.

Let’s illustrate the sharing process.

Create a second version

  1. Edit your test.py file. Make a change to the code. What is up to you.
  2. Save the file, stage, and commit your change.
  3. Run git log

The repos now look like this:

Version 2 committed to the local repo

Your git log clearly shows the new version saved to the local repo.

However, open your remote repository’s GitHub page in your browser. You will see that it is still showing the previous version. Your local main branch is linked to the remote main branch, but the latter is not up-to-date.

Again, sharing with and retrieving from the remote requires a manual command.

git push

Run the command git push. This sends any changes to your local repo to the remote.

Updating the remote with a second version using git push

Refresh the GitHub page in your browser, and you will see that the version name and the content of test.py are updated to the latest version. You will also see two versions now in the commit history.

Now everything is up to date!

Running git push always runs on the active branch, which is main in our case. Suppose you have two local branches, main and rand. If you have parallel commits to in multiple branches, you will either need to need to checkout and git push each branch , or run git push --all.

Knowledge Check

  • (Question) What does the git push command do?
  • (Question) Why is sending and pulling changes from the remote repository a manual process?
  • (Question) How does the local main branch stay linked to the remote main branch?
  • (Question) What happens if there are changes in the remote branch that are not present in your local branch before you push?
  • (Question) How can you verify that your push was successful?
  • (Challenge) Make a change to a file in your local repository, commit it, and then push it to the remote repository.
  • (Challenge) View the commit history and confirm changes appear both locally and on the remote.

3 - Scenario 2 - Clone an existing project

The remote already exists and you want the project

Scenario: A remote repository already exists, and you need a copy of the version history on your computer. You could be a part of a team working on the same project, or maybe you created a new project in lab and you need to check it out from your home computer.

git clone

We already ran through this scenario when setting up Assignment 4 in class. I put a sample repository on GitHub, and you “cloned” it in class.

Let’s start a new project to illustrate the process.

  1. In your Terminal, navigate to your seng-201/ directory.
    • When you clone, it will create a new subdirectory for you. So you need to be in the parent of where you want the workspace to live. We want to be in seng-201/ for this example.
  2. Run git clone https://github.com/llayman/git-remote-clone

You will see output similar to:

➜  ~ git clone https://github.com/llayman/git-remote-clone          
Cloning into 'git-remote-clone'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (4/4), done.
➜  ~ 

You will also have a new subdirectory named git-remote-clone inside seng-201/.

git clone executing and creating a new repo
What happened?

  1. git clone went to the target URL looking for a repo. It found it, and made a copy of the version history on your local computer in the git-remote-clone/ subdirectory.
  2. Git created a local copy of the main branch, which is linked to the remote main branch
  3. Git checked out the main branch into the workspace folder git-remote-clone/.

You are now ready to open git-remote-clone/ in Visual Studio Code or other editor and start working. You edit, stage, commit, make branches, and push as usual.

Do not edit the files yet. Leave them in their initial version to illustrate the next lab.

Knowledge Check

  • (Question) What does the git clone command do?
  • (Question) How does git clone handle creating a subdirectory for the repository?
  • (Question) After cloning, what branch is typically checked out in your local copy?
  • (Question) Does git clone also copy files into your workspace?
  • (Question) How is the local main branch linked to the remote main branch after cloning?
  • (Challenge) Clone an existing repository to your local machine and verify the directory structure.
  • (Challenge) Open the cloned project in an editor and review its initial state without making changes.

4 - Scenario 3 - Retrieving changes

Manually retrieving sending changes from the remote to the local

Scenario: Your started work on an assignment in the computer lab and pushed your changes to the remote. You went home and cloned the repo, worked some more, then pushed your changes to the remote. Now you are back in lab, and you need to get the latest changes from the remote. Or, perhaps a teammate pushed changes to the remote and you need to retrieve them.

Remote changes

I will make some changes to and push them, so the repos now look like this:

A new version is available on the remote

The remote repo has a new version, but your local repo is not up-to-date. You need to manually retrieve the changes. This is a good thing! You don’t want changes to automatically be applied whenever someone else on your team sends them to the remote repo. They could conflict!

Super important point

Before you retrieve changes from the remote, you almost always want to either:

  1. Stage and commit any unsaved changes you have.
  2. Undo, reset, or discard any uncommitted changes you have. Ideally, you should have a “clean” workspace before you retrieve changes. It will make life easier on you.

git pull

Run the command git pull. A few things happen:

  • The changes from the remote repository on the active branch, main, are fetched and integrated into your local repo.
  • Any changes are automatically merged into your workspace. This is why we wanted our workspace to be “clean.”
result of git pull when a new version exists

You now have the most recent version of main in your workspace. you are ready to edit it, commit, and push as usual.

Concurrent changes to the local and the remote

All of this is relatively straightforward when you are the only one working on a project. The version history of branches remains somewhat linear: you are the only one committing, pushing, and pulling, so you are always (probably) working on the latest version.

Life gets considerably more challenging when you have a team of developers all pushing and pulling from the same repo. If you commit a change to main to your local repo, but then Bob pushes a new version of main to the remote repo, what happens when you try to push or pull? Git will protect us from losing work, but we will likely end up with merge conflicts.

Team coordinator through Git remote repos can be smooth if we follow a good process. We will discuss this in the next lab.

Knowledge Check

  • (Question) What does the git pull command do?
  • (Question) Why is it important to have a “clean” workspace before running git pull?
  • (Question) What happens if there are conflicting changes on the local and remote repositories when using git pull?
  • (Challenge) Create a scenario where you make changes locally and have conflicting changes on the remote repository. Use git pull and resolve any conflicts.
  • (Challenge) Demonstrate how to ensure your workspace is clean before pulling changes.

5 -

Scenario 1: Sharing a new project

  1. Do GitHub CLI setup
  2. [WORKSHEET] run through 1-3
  3. create remote-sample/ and open in Visual Studio Code
  4. Create test.py. print(“We are going to share our new repository”)
  5. git init
  6. git add . + git commit
  7. Create a “blank remote repo”. Go to github.com, new, remote-sample as name
  8. Show “success” page
  9. Comment on public vs. private
  10. copy the “…push an existing repository from the command line”
  11. View the remote repo in the browser
  12. [WORKSHEET] run through 4-6

Subsequent versions

  1. test test.py
  2. add and commit
  3. git log. Point out local repo vs. remote repo
  4. [WORKSHEET] add local main and remote main to pg 2 top picture
  5. git push
  6. [WORKSHEET] add 2nd version to remote, update main refs, label git push arrow
  7. Refresh the browser. Show the history.
  8. [WORKSHEET] fill bottom of page 2.

Scenario 2: Clone an existing project

  1. [WORKSHEET] Walk through 1-3
  2. Have students open https://github.com/llayman/git-remote-clone in browser.
  3. Terminal, cd into seng-201
  4. git clone https://github.com/llayman/git-remote-clone
  5. [WORKSHEET] Fill in drawing
    1. Create workspace folder, then local repo.
    2. Right to left. Cloned version into local. Link remote main to local main.
    3. Clone Version into workspace.
  6. [WORKSHEET] Fill in bottom.
  7. DO NOT EDIT FILES YET.

Scenario 3: Retrieving changes.

  1. [WORKSHEET] Explain the scenario at top.
  2. Or, scenario where a teammate makes a change.
  3. [YOU CODE] Edit git-remote-clone/hello.py and push a new version.
  4. Have students refresh the repo in their browser.
  5. [WORKSHEET] add main refs to the top.
  6. Have everyone run git pull. Point out how the code changes.
  7. Run git log
  8. [WORKSHEET] Fill out the middle bullet points and the bottom diagram.