Cloning a Git repository and working locally

If you’ve been following the main path, you have just had interacted with repositories on GitHub. This might not be what you usually do, so now we move to working on your own computer.

Objectives

  • We are able to clone a repository from the web and modify it locally.

  • We can do the same things we did before (commit, branch, merge), but locally.

  • We get a feeling for remote repositories (more later).

Instructor note

  • 10 min introduction and setup

  • 25 min exercise

  • 15 min discussion

What is in a Git repository and what are we cloning?

Illustration of cloning a repository to a local computer

Illustration of cloning a repository to a local computer.

Git repository:

  • Contains all the files and directories of a project.

  • Contains the complete history of all changes (commits) to these files and directories.

  • Each commit is a snapshot of the entire project at a certain point in time and has a unique identifier (“hash”).

  • Sometimes it contains multiple branches and tags.

  • All the commits and history of a local repository are stored in a directory called .git which is located at the root of the repository.

Cloning:

  • Copying (downloading) the entire repository with all commits, branches, and tags to your computer.

  • It is a full backup of the repository, including all history.

  • You can then work on your local clone of the repository.

  • Changes on local clone will not automatically appear in the repository where we cloned from. We have to actively “push” them there (we will practice this in a later episode: How to turn your project to a Git repo and share it).

Exercise

Work on this by yourself or in your teams. Conceptually this episode should seem familiar, from the browser-based exercises we did yesterday.

We offer the Command Line and VS Code paths for this exercise. GitHub isn’t an option in this episode, since that is what we already demonstrated in Committing changes and Merging changes and contributing to the project and since the point of this episode is to work locally.

It is also possible to use the command line (terminal) from inside VS Code.

Exercise: Cloning a Git repository and working locally (25 min)

  1. Configure Git command line and editor if you haven’t done that already.

  2. Decide which repository you want to clone: your fork or the original repository? Both will work for this exercise. Then, clone the recipe book.

  3. Create a new branch.

  4. Make a commit on your new branch.

  5. Switch back to the main branch and create one or two commits there.

  6. Merge the new branch into main.

  7. Compare the graph locally and on GitHub and observe that the changes only exist locally on your computer.

  8. Where are the remote branches? Practice how you can see all remote branches also locally and how you can fetch them and make local changes to them.

The solution below goes over most of the answer and should be used as your guide (you can’t figure it out just from the exercise instructions).

Solution and walk-through

(1) Configure Git command line and editor

We have an own section for this: Configuring Git command line and editor.

(2) Cloning a repository

Now you need to decide which repository you want to clone. All of these options will work for this exercise since we don’t plan to push changes back (for step 8 it might be easier to use the original repository):

The examples below assume you are cloning the original repository. If you are cloning your fork, you should replace cr-workshop-exercises with your GitHub username.

If you are unsure whether you are using SSH or HTTPS, please read Authenticating to GitHub: SSH or HTTPS or VS Code?.

$ git clone git@github.com:cr-workshop-exercises/recipe-book.git

This creates a directory called “recipe-book” unless it already exists. You can also specify the target directory on your computer (in this case “my-recipe-book”):

$ git clone git@github.com:cr-workshop-exercises/recipe-book.git my-recipe-book

(3) Creating branches locally

Create a new branch called another-recipe from main and switch to it:

$ git switch --create another-recipe main

If you leave out the last argument, it will create a branch from the current branch:

$ git switch --create another-recipe

(4) Creating commits locally

Create a new file. After we have created it, we can stage and commit the change:

$ git add new-file.md
$ git commit -m "Short summary of the change"

Make sure to replace “new-file.md” with the actual name of the file you created and to replace “Short summary of the change” with a meaningful commit message.

(5) Switching branches and creating commits

First switch to the main branch:

$ git switch main

Then modify a file. Finally git add and then commit the change:

$ git commit -m "Short summary of the change"

(6) Merging branches locally

On the command line, when we merge, we always modify our current branch.

If you are not sure anymore what your current branch is, type:

$ git branch

Another way to find out where we are in Git:

$ git status

In this case we merge the another-recipe branch into our current branch:

$ git merge another-recipe

(7) How to compare the graph locally and on GitHub

$ git log --graph --oneline --decorate --all

We recommend to define an alias in Git, to be able to nicely visualize branch structure in the terminal without having to remember a long Git command:

$ git config --global alias.graph "log --all --graph --decorate --oneline"

Then you can just type git graph from there on. We have an own section about aliases: Aliases and configuration.

Compare this with the graph on GitHub: Insights tab → Network view (just like we have done before). The result is that we should not be able to see the new branch and the new commits on GitHub (since we haven’t pushed it to GitHub yet - it is only local work so far).

Compare this with the graph on GitHub: Insights tab → Network view (just like we have done before). The result is that we should not be able to see the new branch and the new commits on GitHub (since we haven’t pushed it to GitHub yet - it is only local work so far).

(8) Browsing remote branches and creating local branches from them

With git branch you can list all local branches:

$ git branch

  another-recipe
* main

But where are the remote branches? We expect to see a couple of them.

We can see them by asking for all branches (your output might vary depending on where you cloned from):

$ git branch --all

  another-recipe
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/alex/fruit-salad
  remotes/origin/main
  remotes/origin/radovan/lasagna
  remotes/origin/radovan/poke

You can create a local branch from a remote branch which will “track” the remote branch. For instance, to create a local branch alex/fruit-salad from the remote branch origin/alex/fruit-salad and switch to it, you can do:

$ git switch --create alex/fruit-salad origin/alex/fruit-salad

This shortcut will do the same thing:

$ git switch --track origin/alex/fruit-salad

Or even shorter:

$ git switch alex/fruit-salad

If you want to create a branch and not switch to it, you can use git branch.

$ git branch alex/fruit-salad

Summary

  • When we clone a repository, we get a full backup of the repository, including all history: all commits, branches, and tags.

  • Yesterday we learned about branches and commits, and now we created and used them locally.

  • Creating local branches and commits does not automatically modify the remote repository. To “push” our local changes to the remote repository, we have to actively “push” them there. We will practice this in a later episode: How to turn your project to a Git repo and share it

  • Remote branches and local branches are not the same thing. If we want to create local commits, we always need to create a local branch first. But the local branch can “track” the remote branch and we can push and pull changes to and from the remote branch.