class: center, middle, inverse, title-slide # Let’s git together ### Frie Preu ### codecentric AG ### 2019-09-06 --- class: middle, inverse, center # alternative title: Let's git our sh*t together! --- ## Who am I? - Frie Preu - data scientist / IT consultant / software developer at codecentric - volunteer at CorrelAid: IT infrastructure, internal projects (R packages), miscellaneous mischief - Add me: - LinkedIn: [Friedrike Preu](https://www.linkedin.com/in/friedrike-preu-a2bb46a7/) - Twitter: [@ameisen_strasse](https://twitter.com/ameisen_strasse) - GitHub: [friep](https://github.com/friep) - GitLab: [friep](https://gitlab.com/friep) --- ## Slides, installation and account creation - slides: [https://gost.netlify.com/en/rladies/](https://gost.netlify.com/en/rladies/) - see `README` of the repo - [https://gitlab.com/friep/git-our-shit-together](www.gitlab.com/friep/git-our-shit-together) --- ## Partner up! .pull-left[ ### Mona Lovalace Octocat ![Mona](../../images/github/mona-lovelace.jpg) -> [Ada Lovelace](https://en.wikipedia.org/wiki/Ada_Lovelace) ] .pull-right[ ### Grace Hopper Octocat ![Grace](../../images/github/gracehoppertocat.jpg) -> [Grace Hopper](https://en.wikipedia.org/wiki/Grace_Hopper) ] --- ## Why Git? - masterthesis.docx -- - masterthesis_v1.docx -- - masterthesis_FINAL.docx -- - masterthesis_FINAL_TimsEdits.docx -- - masterthesis_FINAL_FINAL.docx --- class: center, middle ![Help](https://media.giphy.com/media/phJ6eMRFYI6CQ/giphy.gif) --- ## Version Control to the Rescue! - Example: [https://github.com/friep/git-our-shit-together-test-repo/](https://github.com/friep/git-our-shit-together-test-repo/) - RMarkdown presentation (more on this later!) --- class: center, middle, inverse # Download data - fork and clone --- ## Fork and clone ### Repository > A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed. ([Source](https://www.atlassian.com/git/tutorials/setting-up-a-repository)) --- ## Fork and Clone ### Fork > A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project. ([Source](https://help.github.com/articles/fork-a-repo/)) ### Clone > Cloning a repository syncs it to your local machine. After you clone, you can add and edit files and then push and pull updates. ([Source](https://confluence.atlassian.com/bitbucket/clone-a-repository-223217891.html)) --- ## Hands On 1 - fork a repo ### Mona 1. [https://github.com/friep/git-our-shit-together-test-repo/](https://github.com/friep/git-our-shit-together-test-repo/): Fork (top right) 2. `https://github.com/{USERNAME}/git-our-shit-together-test-repo` opens 3. go to Settings->Collaborators and add Grace as a collaborator --- class: center, middle, inverse # Detour: SSH vs HTTPS --- ## SSH vs HTTPS ![Git clone](../../images/github/gitclone_https.png) -> influences how GitHub *authenticates* you --- ## Authentification .pull-left[ ### HTTPS - with GitHub username + password - Gitkraken: just magically works 🎉 - Command line: possibly need to enter username + password ] .pull-right[ ### SSH - with a *keypair* (`id_rsa.pub` and `id_rsa`) - public key, private key cryptography (siehe z.B. [Youtube](https://www.youtube.com/watch?v=AQDCe585Lnc)) - advantage: only set up once, no need to remember password - clone `ssh://...` ] --- ## Hands On 1.1: connect Gitkraken with GitHub 1. Gitkraken profile (top right) 2. Preferences->Authentification->GitHub 3. connect to GitHub 4. Generate SSH key and add to GitHub --- ## Hands On 2 - clone a repo ### Mona & Grace ![Git clone](../../images/github/gitclone.png) 1. Gitkraken: Clone Repo -> Clone with URL 2. enter copied link to URL field --- ## Real life - open source contribution: fork + clone - job / collab with friends: only clone --- class: center, middle, inverse # Save your files - Add und Commit --- ## Commit ### Commit > A commit is the Git equivalent of a "save".[...] Git committing is an operation that acts upon a collection of files and directories. ([Source](https://www.atlassian.com/git/tutorials/saving-changes)) --> Commit = a "save point" in Git. --- ## Commit - a commit saves **changes** w.r.t. the previous commit - edits of file(s) - creation of new file(s) - deletion of file(s) - renaming of file(s) - a commit can contain several changes! --- ## Adding and Staging Area ![Staging Area](../../images/other/staging.png) (Source: [https://git-scm.com/about/staging-area](https://git-scm.com/about/staging-area)) --- class: center, middle ## Commit history ![Git commits](../../images/gitkraken/gitkraken_commits.png) --- class: center, middle ## Go back in time! ![Time Machine](https://media.giphy.com/media/Vqvr9BGv1vhDi/giphy.gif) --- ## Hands On 3 - Go back in time 1. `reset master to this commit` 2. play with: `hard`, `mixed`, `soft` 3. `fast forward master to origin/master` (commit at top) --- class: center, middle, inverse # Detour: RMarkdown and xaringan --- ## RMarkdown - R *flavour* of Markdown (a markup language) - easy way to write... - notes - texts - presentations - ... - and *render* them to good looking documents! - RMarkdown can **include R code** ... - [https://rmarkdown.rstudio.com/lesson-1.html](https://rmarkdown.rstudio.com/lesson-1.html) --- ## xaringan - package to make RMarkdown presentations - `install.packages("xaringan")` - `xaringan::inf_mr()` with `index.Rmd` open --- ## Hands On 4 - make a commit ### Grace + Mona 1. Change stuff (e.g. `README.md` or `index.Rmd`) 2. **GIT ADD** the "unstaged files" you want to commit / "save in Git" 3. write a (at least partly) informative "commit message" 4. **GIT COMMIT** --- class: center, middle ## Git quizzed! ![Lokal](../../images/other/git_workflow_lokal_without_solution.png) --- class: center, middle ## Git quizzed! ![Lokal](../../images/other/git_workflow_lokal_with_solution.png) --- class: center, middle, inverse # Sync files - Push and Pull --- ## Git Hosting - GitHub:
- GitLab:
- Keybase:
--- ## Git local and git remote ... what? - **local**: your laptop / machine - **remote**: in the Cloud (GitHub, GitLab, ...) ![Gitkraken Lokal Remote](../../images/gitkraken/gitkraken_remote_lokal.png) --- ## Sync: Git Pull and Git Push - Git Pull: "download" new commits from GitHub / GitLab - Git Push: "upload" the commits you made locally to GitHub / GitLab --- class: center, middle ## Sync: Git Pull and Git Push ![Push Pull](../../images/gitkraken/push_pull.png) --- class: center, middle ![Push the button](https://media.giphy.com/media/139lMwJ9ow7bKE/giphy.gif) --- ## Hands On 5 - Pull and Push 1. Grace: Push 2. Mona: Pull 3. Mona: Push 4. Grace: Pull --- class: center, middle ## Git quizzed! ![Push Pull](../../images/other/git_workflow_with_github_without_solution.png) --- class: center, middle ## Git quizzed! ![Push Pull](../../images/other/git_workflow_with_github_with_solution.png) --- class: center, middle, inverse # When things go wrong... --- class: center, middle ![git google](../../images/other/giterrors.png) --- class: center, middle ![xccd](../../images/other/xkcd_comic.png) --- ## When things go wrong... 1. as long nothing is pushed, all is (kind of) ok -> don't push if things are messed up! 2. if things are really messed up: save your code somewhere else and clone again --- ## Merge conflicts > Merge conflicts occur when competing changes are made to the same line of a file, or when one person edits a file and another person deletes the same file. ([Source](https://help.github.com/en/articles/resolving-a-merge-conflict-using-the-command-line)) --- ## Merge conflicts 1. you commit changes -- 2. you pull to get the newest updates -- 3. your coworker has changed something that clashes with your changes -- 4. git: --- class: middle, center ![up to you](https://media.giphy.com/media/xUOxfcCFf8z89a6KTC/giphy.gif) --- ## Solving merge conflicts - you need to solve merge conflicts on your machine -> decide which version / changes you keep - Gitkraken has an integrated [merge conflict tool](https://blog.axosoft.com/merge-conflict-tool/) --- ## Hands on 6: Merge conflicts 1. Mona or Grace: edit `README.md` **on GitHub** in a certain line 2. Mona & Grace: edit `README.md` locally on your machine **in the same line** (edit it differently!) 3. Monat & Grace: commit your own changes (don't push yet!) 4. Mona & Grace: `pull` 5. Mona & Grace: solve the merge conflict locally and push your own version. --- ## Sidenote: Git stash -> something Gitkraken often does for you to keep your changes safe. > git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit. ([Source](https://www.atlassian.com/git/tutorials/saving-changes/git-stash)) --- class: center, middle ## Sidnote: Git stash ![stash](https://media.giphy.com/media/l0HlxJfVUKhtR8Jna/giphy.gif) --> put it away for now! --- ## Git stash for solving merge conflicts 1. git stash 2. git pull 3. apply stash 4. solve merge conflicts 5. (delete stash) --- class: center, middle, inverse # work with GitHub --- class: center, middle, inverse # Issues --- ## Issues - issues: todos / bugs / ideas - each issue has a number - \#issueno in commit message associates commit with issue - example: [https://github.com/jandix/sealr/issues](https://github.com/jandix/sealr/issues) --- ## Hands On 7: Issues 1. Mona: create an issue "add Grace's favorite GIF" 2. Grace: go to [giphy.com](giphy.com), find a funny gif and add it to the presentation (-> copy link) 3. Grace: add + commit. link the issue number in the commit message (#issueno) 4. Grace: push 5. Mona: reload the issue (ctrl + r) --- ## Recap issues - very useful for project organization - discussion is centralized (**but**: don't put sensitive information in GitHub) - Tip: create a GitHub *project* with a kanban board for process tracking --- class: center, inverse, middle # Branches and Pull Requests --- class: center, middle ![Gitkraken branching](../../images/gitkraken/screenshot_branching_gitkraken.png) --- class: center, middle ![Come on](https://media.giphy.com/media/HfFccPJv7a9k4/giphy.gif) --- ## Branches ### Branch > A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history. ([Source](https://www.atlassian.com/git/tutorials/using-branches)) ### Checkout > The git checkout command lets you navigate between the branches created by git branch. Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch. Think of it as a way to select which line of development you’re working on. ([Source](https://www.atlassian.com/git/tutorials/using-branches/git-checkout)) --- ## Why branches? - stability: only have working code on "master" branch - e.g. CI/CD directly to dev - users download your code from GitHub (e.g. `devtools::install_github()`) -- - collaboration: independent development of code ("feature branches") -- - experiments: try it out in a branch -- - example: [https://github.com/jandix/sealr/branches](https://github.com/jandix/sealr/branches) --- ## Branches Workflow 1. create a branch in Gitkraken with a meaningful name (e.g. issue1-add-favorite-gif) -- 2. checkout branch (double-click on branch name in Gitkraken) -- 3. continue working as usual (pull-commit-push cycles) -- 4. (optional: merge other branches into your branch to get their updates) -- 5. merge your branch into the master branch --- ## Merging branches - Gitkraken: right-click on branch name or master - depends on which branch is ahead of which and which branch is currently checked out (?) - right-click on master: merge master into issue1-add-favorite-gif -> branch is updated - right-click on branch: merge issue1-add-favorite-gif into master -> master is updated (usually via Pull Request) --- ## Pull Requests (PRs) > Pull requests let you tell others about changes you've pushed to a branch in a repository on GitHub. Once a pull request is opened, you can discuss and review the potential changes with collaborators and add follow-up commits before your changes are merged into the base branch. ([Source](https://help.github.com/en/articles/about-pull-requests)) - to be used in combination with branches - useful "review" and feedback functionality --- ## Hands on 8 - branches & pull requests 1. Grace & Mona together: create a new issue (think of something!) 2. Mona: create a branch called `issue2-{description}` locally 3. Mona & Grace: work together on issue 2 on Mona's machine 4. Mona: push the branch 5. Mona: open a pull request and assign Grace as a reviewer 6. Grace: review Mona's PR and merge it --- ## Real life: Branches and Pull Requests - branches - very useful when developing R packages / applications / shiny apps in a team: feature branches - relevance for data analysis projects: enables working in parallel on several methods, keep master clean of non-working code, backup (!) - pull requests - centralize discussion - code review stage --- class: center, middle, inverse # Recap --- ## What we learned today - Fork and clone a GitHub repository -- - make a commit in Gitkraken -- - pull and push commits from / to GitHub -- - work with branches and pull requests --- class: middle, center, inverse # That's it! --- ## If you ever need help...contact me! ### Frie Preu +
data scientist / IT consultant @ [codecentric AG](https://www.codecentric.de) +
[friep](https://github.com/friep) +
[friep](https://gitlab.com/friep) +
[friep](https://keybase.io/friep) +
[ameisen_strasse](https://twitter.com/ameisen_strasse) +
[RLadies Slack](https://rladies-community-slack.herokuapp.com/)