Lecture 3: Git and GitHub for collaboration
2026-03-06
git init → git add → git commitgit switch -c feature → make changes → git commitgit switch main → git merge featuregit clone / git remotegit pull / git pushorigin.remote adding it from an existing repositoryWhen you want to add an existing repository to GitHub, you will have to add the remote yourself
Example:
Note 1: It only works if the remote repo exists and if you are not in a repo which has already been linked to a remote.
Note 2: To learn about all possible commands for remotes use
-h, likegit remote -h.
git clone-ing a repositorygit clone just works, for private ones you will need to be authenticatedgit clone, the remote named origin is set up for you automatically.git clone <remote URL>You first pull the latest changes from the remote…
… and then push your own changes.
git pullgit pull in your terminal.git pullgit pull does two things under the hood: it fetches the latest changes and merges them into the current branch.git fetch to fetch the latest changes and then git merge <remote>/branch to merge them)git pushgit pushpull you need to pull again.test2, created locally but never pushed on remote.git push from the branch leads to the following message:fatal: The current branch test2 has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin test2git push -u origin test2 or git push --set-upstream origin test2 to push the branch and set the upstream at the same time.-u is short for --set-upstream)test3, which is not yet tracking any remote branch.git pull from test3:There is no tracking information for the current branch.
Please specify which branch you want to merge with.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/test3 test3Note: The easiest way to avoid both situations: always use
git push -u origin <branch>the first time you push a new branch.
… set it up once,
…done forever as long as you don’t change computer,
… takes about 10 minutes …
Note: Never share your private key. Not by email, not on GitHub, not anywhere. It belongs to .gitignore.
Once you’ve set up your SSH key, test your connection using the following code.
How to clone the course repository will be covered in the exercise session, but here is a quick overview of the steps.
Remember our course structure:
Introduction_to_programming/
├── github_course_materials/ # is empty for now, you will clone the git repo in week 3
├── exercises/ # Student's own work
│ ├── week_01/
│ ├── week_02/
│ ├── ...
│ ├── week_12/
├── group_project/
│ ├── ...
Go on https://github.com/ASallin/BECON_4222_Introduction_Programming and click on <>Code
You can find the remote URL by going to a repository on GitHub and clicking the Code button
The course is public, so cloning via HTTPS will work without a SSH key.
Now you’ve cloned the course repository in your directory. You’ll have a new folder called BECON_4222_Introduction_Programming. You can remove the github_course_materials folder.
Introduction_to_programming/
├── BECON_4222_Introduction_Programming/
├── exercises/ # Student's own work
│ ├── week_01/
│ ├── week_02/
│ ├── ...
│ ├── week_12/
├── group_project/
│ ├── ...
Every week, you can refresh the course content with git pull.
Note: this is experimental. The course materials uploaded on Canvas every week are the official version. If you want to use the GitHub repository, make sure to pull the latest changes before starting to work on the exercises.
On your GitHub account, click New repository
Choose the parameters:
git-practice-week03)You’ve been working locally and now want to publish your project on GitHub.
Create the repo on GitHub first, then clone it locally:
A pull request (PR) is a request to merge changes from one branch into another.
main: you work on a branch, then request for your changes to be reviewed, pulled and mergedOverview of the pull requests page in a GitHub repository. Repository: https://github.com/worldbank/GEEST
View of a single pull request on GitHub. Repository: https://github.com/worldbank/GEEST
git push -u origin my-branchmy-branch → main (you are usually invited to do so)PRs keep the main branch clean and give the team a chance to review code before it’s integrated.
I find PRs to be flexible (enough), and very helpful to avoid merge conflicts.
main, everyone works on a branch and contributes via a pull request.There are many more features of Git and GitHub that we won’t cover in this course. You’ll learn the rest when you need it (I still discover so many new things in Git).
Go on the GitHub page of the BFS package (API wrapper from the Swiss Federal Statistical Office) and look at the different issues.
forking themgitprofile.config.ts to customize your profile page.Thanks Jan Simson for the inspiration on this chapter
Several tools exist to visualize git repositories. These can be useful to get a quick overview of a git repository.
uvgit add, git commit, .gitignore