![gnugen logo](assets/gnugen_white_trans.svg) # just do *git* *Collaborative Workflow*
## Slides Available at: [gnugen.ch/en/guides/](https://gnugen.ch/en/guides/)
## 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) * [Best practices](#best-practices) * [Useful commands](#useful-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 remote ``` bash $ git push ``` ### update your local commits ``` bash $ git fetch # update your local copy of origin/branch $ git pull # fetch and merge origin/branch into branch ```
### 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) ```
### 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`~~ Instead, git lets us reconcile the two versions nicely
## merge vs. rebase
#### Sidenote: what is a commit? ```diff $ git diff HEAD~ diff --git a/myfile.txt b/myfile.txt index f8266af..8f12655 100644 --- a/myfile.txt +++ b/myfile.txt @@ -1,2 +1,4 @@ Hello! -This is a file containing a demo for Just Do G(it)! +This is a file containing a demo for Just Do Git! + +I hope this makes sense :) ```

Current situation

After rebase

After merge

If no new commits were made, this is a fast-forward

## get latest changes from the remote branch
### Rebase your commits on top of the remote If you have made commits already ```bash $ git pull --rebase # solve the conflicts $ git rebase --continue # if you had conflicts $ git push --force # here, --force is necessary ``` Tip: Some code editors help you solve conflicts
### 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 --force # here, --force is necessary too ```
##### 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
						

Pull/merge requests


							$ git checkout feature
							$ git rebase main
							// if there are conflicts, solve them and continue with: 
							// git rebase --continue
							$ git push
							// 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

Pull/merge requests

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

## 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
## 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 (unstaged) 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
## Some practice
## Practice on the EPFL GitLab - we prepared a lab! - setup of git and gitlab/github on your computer - we will help you with any git related issues
## Some practice using our lab - instructions on how to setup GitLab and git on your computer - excersises to practice the collaborative workflow - available on: [gnugen.ch/en/guides/](https://gnugen.ch/en/guides/)

just do git



Survey: