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.

Collaborative Git Workflows

University of Pisa

Why collaboration is hard


Online resources

Workflow guide: https://docs.github.com/en/pull-requests

Rebase deep dive: https://git-scm.com/docs/git-rebase

History surgery: https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

Debugging: https://git-scm.com/docs/git-bisect


Remote basics

CommandMeaning
git remote -vList remotes and fetch/push URLs.
git remote add origin <url>Define a primary remote (often your fork).
git remote add upstream <url>Track the canonical repo when working from a fork.
git fetch --all --pruneUpdate remote-tracking branches and drop deleted refs.
git push -u origin feature/apiPush a branch and set upstream for future pulls.

Fork/PR workflow (canonical)

  1. Fork the repo on GitHub (or create a bare upstream on shared storage).

  2. Clone your fork, add upstream pointing to the canonical repo.

  3. Create a topic branch from upstream/main: git switch -c feature/foo upstream/main.

  4. Commit small, testable changes; rebase onto upstream/main before pushing.

  5. Push to your fork and open a pull request targeting upstream/main.

  6. Iterate on review feedback; keep the branch updated via git pull --rebase upstream main.


Keeping your fork in sync

git fetch upstream
git switch main
git rebase upstream/main   # or: git merge upstream/main
git push origin main

Rebase vs merge

CommandWhen to useNotes
git merge <branch>Preserve true history, create merge commits for divergent lines.Safe for shared branches; can produce complex graphs.
git rebase <base>Linearize your local/topic history before review.Avoid rebasing published branches; keep conflicts small by rebasing often.
git pull --rebaseUpdate current branch without merge commits.Set pull.rebase=true for consistency.

Interactive cleanup

git rebase -i upstream/main
# reorder, squash, or drop commits
git commit --amend
git push --force-with-lease

Code review essentials


Conflict handling (multi-remote)

git pull --rebase upstream main   # resolve conflicts once locally
git merge --abort                 # if you need to restart a merge
git rebase --continue             # after fixing files

Safety nets

CommandPurpose
git reflogRecover lost commits/branches by walking HEAD history.
git stash push -m "<msg>"Temporarily park work-in-progress changes.
git worktree add ../wt-feature feature/fooMultiple working trees for parallel tasks without stashing.
git cherry-pick <rev>Apply specific commits onto another branch.
git bisect startBinary search commits to find regressions.

Hooks and automation

HookUse case
pre-commitLint/format before commits; can block bad code.
commit-msgEnforce message conventions (prefixes, ticket IDs).
pre-pushRun smoke tests before pushing to CI.
post-checkoutAuto-install deps or switch configs per branch.

Release hygiene


Lab 04 outline