Git: and now what?

Pierre Marchand

Team-project POEMS, Inria, ENSTA and IPP

What you know?

Use cases

  • with : versioning,
  • with with : versioning and backup,
  • with with : versioning, backup and synchronization,
  • with with : versioning, backup, synchronization and collaborative work.

Git commands

  • git clone/git init for creating a repository
  • git add/git commit for versioning
  • git push/git pull for backup and synchronization
  • git status/git log for when lost
Any question? 🙂
What is next?

Collaborative work is the most difficult part!

  • Write an article with LaTeX\LaTeX
  • Collaborate on a research code
  • Contribute to an open-source library
  • ...

git merge spaghetti 🍝

How to use multiple git branches efficiently?

A few commands for git branches

Git commands for branches 🌳

  • Create a branch locally called "new_branch": git branch new_branch

A few commands for git branches

Git commands for branches 🌳

  • Create a branch locally called "new_branch": git branch new_branch

  • Go to another branch, for example "main": git switch main

A few commands for git branches

Git commands for branches 🌳

⚠️ you cannot go to another branch if you have some local modification

➡️ git stash saves the current changes, git stash pop to apply them

Recap so far

  • git branch new_branch
  • git switch main
  • git stash/git stash pop

And now?

So far so good, but how to deal with branches efficiently? Efficiency in the following sense:

  • The history needs to be as linear as possible.
  • Avoid merge conflicts as much as possible.
git workflows

What is a git workflow ?

It is a set of rules chosen for a project to maximize efficiency.

Gitflow

One of the first successful workflow is git-flow. Introduced in 2010, it can be summarized by the picture on the side, we will see how it works...

or not!

because it is completely inadequate obviously 🤪

The tragedy

What is so difficult?

What to do? It depends...

Only local modifications without commit

git stash your local modifications ➡️ git pull ➡️ git stash pop to reapply your modifications

Single branch workflow

Only local commits

  • git pull --rebase

  • git pull --no-rebase

What is hidden?

git pull is actually git fetch + ... merging somehow the origin/main and main

Fast-forward merge
  • On branch B, git merge A (default), git merge --ff A, git merge --ff-only A

⚠️ Default when using git pull is fast-forward, otherwise

hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
Rebase
  • From B, do git rebase A

  • Git will make you modify each commit where a conflict occurs.
    • git add to select your modifications solving a conflict in the current commit
    • git rebase --continue to proceed to the next commit.
  • It modifies the branch history! Never do it on a public branch

⚠️ git pull --rebase uses git rebase origin/main in our example.

Merge
  • From B, do git merge A

  • The "merge commit" will contain the potential modifications to solve conflicts.
    • git add to select your modification
    • git commit to create the merge commit.

⚠️ git pull --no-rebase uses git merge origin/main in our example.

Feature branch workflow

Concepts

  • All ongoing works are done in dedicated branchs, feature branches.
  • main branch is always clean

In practice

  1. git branch a_new_feature and git push -u origin a_new_feature
  2. git add/git commit/git push as much as you want
  3. git rebase main to update your branch with main (and potentially deal with conflicts)
  4. git checkout main and git merge a_new_feature
Pull Request/Merge Request

In practice, we use a Git plateform: GitHub, Gitlab, Bitbucket,...

Features

  • Git server (your remote repositories)
  • "social network" around coding projects, providing discussion spaces for
    • issues
    • wiki
    • Pull Request/Merge Request
    • ...
  • Automate testing/deploying, for Continuous Integration (CI) and/or Continuous Deployment (CD)
  • Project management

Feature branch workflow in real life

Concepts

  • All ongoing works are done in dedicated branchs, feature branches.
  • main branch is always clean
  • Merging features branches is done via `Merge Request`/`Pull request`, which lead to review, testing,...

In practice

  1. git branch a_new_feature and git push -u origin a_new_feature
  2. git add/git commit/git push as much as you want
  3. Create MR/PR via IDE or your favorite Git platform
  4. Once accepted and rebased, merge the feature branch via IDE or your favorite Git platform

Tips

  • Use git rebase -i to clean your branch before merging.
  • Forbid merge if tests/formatting/... are not validated.
  • Forbid merge without a PR/MR.
Why forks? 🍴
You do not always have the right to push your branch!

➡️ a fork is a copy of an existing repository in which, usually, someone else is the owner.

In practice

  1. Fork the repository in which you want to submit a change
  2. git branch a_new_feature and git push -u origin a_new_feature
  3. git add/git commit/git push as much as you want
  4. Create MR/PR via IDE or your favorite Git platform-> you can still do it from your fork!
  5. Once accepted and rebased, merge the feature branch via IDE or your favorite Git platform

Tips

  • A fork is still a git repository
  • You can update your fork from the "upstream" repository. You need to add another remote corresponding to the upstream repository
References

Documentation

Git workflows

Others

Exercice

git scales ! from your article/proto code to Linux kernel and Windows