Lecture 2: Introduction to Git
2026-02-27
Note: We might not finish all the materials of week 2 today. If so, we will finish the rest next time.
We will learn the basic commands of Git in a Command-Line Interface (CLI), and we will use the GUI in VSCode
All the commands we will learn are available in the official Git Cheat Sheet. Please download the sheet.
Run the following commands in your terminal to correctly configure Git on your computer.
# Add your name
git config --global user.name "Your Name"
# Add your email address
git config --global user.email "your.email@unisg.ch"
# Use modern main branch name
git config --global init.defaultBranch main# For Linux/Mac:
git config --global core.autocrlf input
# For Windows:
git config --global core.autocrlf trueWhy? Windows saves linebreaks (enter) differently then Linux/Mac does. Remember Data Handling (Linux / macOS: LF \n, Windows: CRLF \r\n). Git may interpret this as code changes. This setting prevents unnecessary diffs and conflicts.
git add to select the files that you want to include in your project history, that you want to track, and that you want to share with others.git commit to record the staged changes and creates a snapshot in time.Three levels: changes can be either unstaged, staged or committed.
Files are added to the staging area with git add <path to file or directory>
All files in the staging area are committed with git commit
Remember our folder 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/
│ ├── ...
We will initialize a repo in the exercises folder. You will have to initialize another repo in your group_project folder.
This is done via the init command.
In your terminal from VSCode, navigate to your cd Introduction_to_programming/exercises. Then:
With git init we turn the directory into a repository.
At this point, 🗂️ tracking has started!
Let’s write a text file example.txt using our terminal.
echo first steps in git > example.txt # Windowns (cmd)
echo "first steps in git" > example.txt # Mac/Linux (bash)Both create the same file content. Verify:
Add example.txt to the staging area with git add <path to file or directory>.
* to represent any sort of filename e.g. add all .txt files via *.txtNote: You can use
git add .to add all unstaged changes in the current directory to the staging area. Be careful: this may include sensitive files (API keys, passwords, etc.)!
You can see the high-level changes and what is about to happen with git status
git status
# On branch main
# No commits yet
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: example.txtLet’s change the content of example.txt. Save it, then run:
git status
# No commits yet
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: example.txt
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git restore <file>..." to discard changes in working directory)
# modified: example.txtgit commitAll changes in the staging area are committed with git commit. Every commit needs a message!
Let’s add and then commit the new change
git add example.txt
git commit -m "Create example.txt"
# [main (root-commit) fc0372c] Create example.txt
# 1 file changed, 1 insertion(+)
# create mode 100644 example.txt
git status
# On branch main
# nothing to commit, working tree cleanOnce a change is committed it becomes significantly harder to remove it.
clean_gdp_data()” 😋.DS_Store files on MacOS.venv/: this directory is machine-specific (environment realization),pyproject.toml and uv.lock (environment definition)..gitignore and if it matches, Git will ignore the file.gitignore file itself is tracked just like any other file.gitignore to exclude data files..gitignore filegit checkout by referencing the hash: git checkout a3f7b2ccheckout sends you out of a branch, i.e. “detached HEAD”. You need to explicitly switch to main (or to any other branch).git switch -c my-branch: creates a branch and checks it out (you are leaving main and working on your new branch)git status to be aware on which branch you are on.git branch to check the existing branches.git branch -d <branch-name> to safely delete (-d) a branch after it’s merged.git switch my-branch to switch branches.Since Git 2.23, git switch is the recommended alternative to git checkout for switching branches.
git merge my-branchgit merge <branch> merges the named branch into your current branch (usually main)Note: If you merge your branches / edits before making more changes, you can avoid conflicts.
git statusIn this conflict, the lines between <<<<<< HEAD:index.html and ====== are the content from the branch you are currently on.
The lines between ======= and >>>>>>> issue-5:index.html are from the feature branch we are merging.
To resolve the conflict, edit the whole section until it reflects the state you want in the merged result. Remove the conflict markers <<<<<<, ====== and >>>>>>>.
Then run git add index.html and git commit to finalize the merge. CONFLICTS RESOLVED.
Example from Happy Git with R
If, during the merge, you get confused about the state of things or make a mistake:
git merge --abort to abort the merge and go back to the state prior to running git merge.Once you’ve recovered from the lecture, please read the following slides.
Once you’ve recovered from the lecture, please read the following slides.
git diff: changes between your working directory and staging area (unstaged changes)git diff --staged: changes between your staging area and the last commit (what will be committed next)git reset: reset your staging area (use git reset <filename/directory> for specific files)git restore --staged <file>: used to undo the effects of git add (unstage a certain file and undo a previous git add)git restore <file>: discard local changes in a file, restoring its last committed state. Undo any changes to files (go back to last commit stage). Cannot be undone 💪git log: see the history of your commits (who changed what, when and why). You can move up and down with the arrow keys and leave the log view by pressing q. Use git log --oneline to see the log in a compact way.Git commands, like many other CLI tools follow a certain structure:
git <command> [flags/options] [arguments]
git status
git commit -m "Adding example.txt" # -m for message
git config --global user.name "Your Name" # --global for setting the configuration globallyWith -h you can get help on any Git command 🚨
git commit without the -m option. Every commit needs a message. If you don’t provide one, Git will open a text editor in the current terminal so you can write the commit message manually.Esc (make sure you’re in normal mode):wq then Enter → save and quit:q! then Enter → quit without savingCtrl + O then Enter → saveCtrl + X → exitThis tells Git to use VS Code instead of vim/nano.
git checkout <commit-hash> to go back to a previous commit, HEAD detaches from the branch and points directly to that commit. This is called a “detached HEAD” state.a3f7b2c directly, while main still points to the latest commit e4d1f9a.git switch main. HEAD will re-attach to the main branch.