Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Git Fundamentals

University of Pisa

Why Git?


Online resources

Tutorial: https://gitimmersion.com/

Sandbox: https://learngitbranching.js.org/

Recap: https://jwiegley.github.io/git-from-the-bottom-up/

Book: https://git-scm.com/book/en/v2


Git Terminology — Foundations

CommandMeaning
git initCreate a .git/ metadata directory and begin tracking history in the current folder.
git clone <url>Copy an existing repository, including all commits, branches, and remotes.
git statusSummarize working tree, staging area, and untracked files at a glance.
git add <path>Stage file changes so the next commit snapshot includes them.

Git Terminology — Commits & History

CommandMeaning
git commitRecord staged changes as an immutable snapshot with author + message.
git logWalk through commit history; combine with --graph/--oneline for structure.
git show <rev>Inspect a single commit’s metadata and diff.
git diff A..BCompare two revisions (branches, tags, hashes) to see how they diverge.

Git Terminology — Branching & Recovery

CommandMeaning
git branch -vvCreate/list/delete branch refs and display upstream tracking info.
git switch <branch>Move HEAD to another branch or commit (optionally create it with -c).
git merge <branch>Integrate another branch into the current branch via fast-forward or merge commit.
git rebase <base>Replay commits onto a new base to linearize local history.
git tag <name>Label specific commits (typically releases) with immutable names.
git stashShelve working tree changes temporarily to restore a clean state.

Create a repository (or clone)

# start from scratch
mkdir lab03 && cd lab03
git init

# or clone existing work
git clone git@github.com:luca-heltai/sspa.git
cd sspa

Configure identity and defaults

git config --global user.name  "Luca Heltai"
git config --global user.email "luca.heltai@unipi.it"
git config --global core.editor "code --wait"
git config --global init.defaultBranch main

Staging area workflow

git status -sb
git add src/main.cpp docs/README.md
git restore --staged docs/README.md   # unstage
git diff --staged                     # review

Crafting commits

git commit -m "Grid: add MPI halo exchange for lab"
git commit --amend            # rewrite last commit (before push)

Inspect history

git log --oneline --graph --decorate -n 8
git show HEAD^
git diff main..feature/parser
git blame src/io.cpp

Branching model

git switch -c feature/cli-enhancements
git switch main
git branch -vv

Merging and fast-forwards

git switch main
git merge feature/cli-enhancements

Resolve merge conflicts

  1. Run git merge feature/api and let Git stop at conflicts.

  2. Open marked files (<<<<<<<, =======, >>>>>>>) and edit to the desired result.

  3. git add <file> after each fix; verify with git status.

  4. Finish with git commit (merges reuse combined log).

git merge --abort   # bail out if you need to restart

Undo strategies


Lab 03 outline