![gnugen logo](assets/gnugen_white_trans.svg) # just do *git* *Collaborative Workflow*
## Please help us improve! There will be a short survey at the end of the presentation
## Table of contents * [About gnugen](#about-gnugen) * [Collaborative workflow](#collaborative-workflow) * [Useful commands](#useful-commands) * [Best practices](#best-practices) * [Command Summary and Reference](#commands) * [Some practice](#some-practice)
# About gnugen

Who are we?

Promote free/libre software on the EPFL campus

free/libre software

Four fundamental freedoms

  • freedom to use: freely run parts of a program or not
  • freedom to study the source code
  • freedom to share the program
  • freedom to improve the program

free/libre software

But why?

  • Respect for the user
  • Accessible, inclusive
  • Privacy
  • Many more...
# collaborative workflow

This is why!

by https://github.com/pushpankq/Git-Commands-

What is 'origin/main' ?


							$ git status
							On branch main
							Your branch is up to date with 'origin/main'.
						

The remote

### publish your commits to the remote ``` bash $ git push ```
### get the commits from the remote ``` bash $ git pull ```

but...

```bash $ git pull error: cannot pull: You have unstaged changes. error: Please commit or stash them. ```

remember to commit your changes first

### Multiple remotes #### until now ```bash $ git remote -v origin https://gitlab.epfl.ch/... (fetch) origin https://gitlab.epfl.ch/... (push) ``` #### add a remote ```bash $ git remote add another_origin https://github.com/... $ git remote -v origin https://gitlab.epfl.ch/... (fetch) origin https://gitlab.epfl.ch/... (push) another_origin https://github.com/... (fetch) another_origin https://github.com/... (push) ```
### Multiple remotes You have to specify the remote #### pull ```bash $ git pull another_origin main ``` #### push ```bash $ git push another_origin main ```
### What if someone has changed the remote before you ? ```bash $ git push error: failed to push some refs to 'https://gitlab.epfl.ch/...' ```

NOT the thing to do: ~~git push --force~~

## get latest changes from the remote branch
### Rebase your commits on top of the remote If you don't have new commits yet ```bash $ git stash push $ git pull $ git stash pop // solve the conflicts $ git add name_of_file_changed.txt $ git commit -m "the commit message" $ git push ```
### Rebase your commits on top of the remote If you have done commits already ```bash $ git pull --rebase // solve the conflicts $ git rebase --continue //if you had conflicts $ git push ``` Tip: Some code editors help you solve conflicts
##### unsolved conflicts ```python <<< HEAD def f(self): a = 0 while a < 256: a += 1 print(a) ======= def f(self): x = 0 while x < 256: x += 1 print("Hello World") >>> the_new_branch ``` ##### solved conflicts ```python def f(self): x = 0 while x < 256: x += 1 print(x) ```
## Merge your feature branch into main

Rebase branch


							$ git checkout feature
							$ git rebase main
							// if there are conflicts, solve them and continue with: 
							// git rebase --continue
							$ git push --force // --force is necessary here
						

Before rebase

After rebase

Pull/merge requests


							$ git checkout feature
							$ git rebase main
							// if there are conflicts, solve them and continue with: 
							// git rebase --continue
							$ git push --force
							// finally create pull/merge request on github/gitlab
							// the remote server merges main and feature after approval
						

The goal is to allow other developers to review your code before it's added to the project's main branch

live demo

Pull/merge requests

Manually merge main


							$ git checkout feature
							$ git rebase main
							// if there are conflicts, solve them and continue with: 
							// git rebase --continue
							$ git push --force // --force is ok since on feature branch
							$ git checkout main
							$ git merge feature
							$ git push // do NOT --force on main branch
						

Avoid manual merge, use pull/merge requests

# Useful commands
Add some changes to the last commit ```bash $ git commit --amend ``` Restore a file from the last commit ```bash $ git restore file_name ``` Print the history of commits ```bash $ git log --graph --oneline ``` Print changes between your file and the last commit ```bash $ git diff file_name ```
### Jump to a specific commit use the commit hash ```bash $ git checkout 68aef35 ``` or the commit position from the last commit ```bash $ git checkout HEAD~3 ```
### Tag the last commit allow to easily find a specific commit ```bash $ git tag -a v1.2 -m "message for this version" $ git tag v1.2 $ git checkout v1.2 ``` you have to explicitly push the tags ```bash $ git push --tags ``` github and gitlab can do predefined actions when a tag is pushed
### reset ```bash $ git reset HEAD~2 ``` - Go back x commits behind without changing the files - Use "--soft" to keep the changes staged - Don't use "--hard" because it overwrites the files
# Best practices

Do not

  • use push --force (until required)
  • work on the same branch
  • work on main

Do

  • small commits
  • meaningful commit names
  • use as many branches as necessary
  • use merge/pull requests
  • add branch protection to main in github/gitlab
# Command Summary and Reference

Summary of basic commands

  • git add, git commit: create a commit
  • git status, git diff, git show: show status and changes

Handling branches

  • checkout
  • merge
  • rebase

Using git with other people

  • pull
  • push
  • remote
  • tag

Handling small mistakes

  • commit --amend
  • reset HEAD~
  • restore

Look at the history

  • log
  • diff
  • checkout
# Some practice

Your choice!

  • a game to practice
  • setup of git and gitlab/github on your computer
## Some practice with ## Oh My Git! - open [ohmygit.org](https://ohmygit.org/) - click on the button labelled "Download the game!" below "Play the game!" - and follow the instructions for your OS
## Setup of git and gitlab/github - linux with your favorite package manager - macos with [brew](https://brew.sh/) or Xcode - windows with [git bash](https://gitforwindows.org/)

But first

Thank you!

![gnugen logo](assets/gnugen_white_trans.svg) ## just do *git* * Slides: https://gnugen.ch/en/guides/