class: center, middle, inverse, title-slide .title[ # Git for Newbies ] .subtitle[ ## CorrelAid Project Cycle Kickoff ] .author[ ### Michael Aydinbas ] .date[ ### 2023-09-23 ] --- ## Who am I? - Michael Aydinbas - Senior Data Engineer at New Work SE, 4 years of consultancy - Project lead at CorrelAid and CorrelAidSwitzerland since 2021 - Add me: - LinkedIn: [Michael Aydinbas](https://www.linkedin.com/in/michael-aydinbas/) - GitHub: [pmayd](https://github.com/pmayd) --- ## Agenda .pull-left[ #### Basics - **Motivation**: Why Git? - **Authentication** - **"Downloading" Repositories**: Forking and Cloning - **local workflow**: `git add` and `git commit` - **"Uploading" and "Downloading" Commits**: `git push` and `git pull` #### Advanced topics - **gitignore** - **Merge conflicts** - **GitHub issues** - **Working with branches** ] .pull-right[  ] --- ## Why Git? .pull-left[ ### 2014/15 - My BA ```bash WS2014-15/BA/R ├── BA.R ├── Weighting.R ├── `droppedcode`.R ├── edvreg_Linzer-Lewis.R ├── edvreg_Linzer-Lewis_`modified`.R ├── edvreg_Linzer-Lewis_modified`1`.R └── `final`models.R ``` ] .pull-right[ ### 2017 - Hiwi job ```bash hiwi/{prof}/eufunds/code/ ├── data_wrangling.R ├── data_wrangling_nat.R ├── data_wrangling_`old`.R ├── map.R ├── maps`2`.R └── plots.R ``` ] --- class: center, middle  .footnote[ [Help Me Cat Gif](https://giphy.com/gifs/cat-fire-rescue-phJ6eMRFYI6CQ) via [Giphy](https://giphy.com), [original source](https://www.reddit.com/r/gifs/comments/3i5ynf/fire_cat_to_the_rescue/) ] --- ## Version Control to the Rescue! - Example: [https://github.com/CorrelAid/git-newbie-playground](https://github.com/CorrelAid/git-newbie-playground) - just a simple README.md for you - or: use the repository of your project --- class: center, middle, inverse # Requirements --- ## GitHub and Git - Please see the [README](https://github.com/CorrelAid/git-newbie-playground) for the requirements - create GitHub account - install Git - introduce yourself to Git --- class: center, middle, inverse # Authentication --- ## Authentification **GitHub needs to know that you are you!** .pull-left[ ### HTTPS - with GitHub username + password -> easy to understand (not recommended anymore) - with GitHub username + Personal Access Token (PAT) -> recommended - store PAT in git credential store to avoid entering over and over - `git clone https://...` ] .pull-right[ ### SSH - with a *keypair* (`id_rsa.pub` and `id_rsa`) - public key, private key cryptography (see e.g. [Youtube](https://www.youtube.com/watch?v=AQDCe585Lnc)) - advantage: only set up once, no need to remember password - `git clone git@github.com:...` ] --- ## Hands On: Authentication Setup Go to https://github.com/CorrelAid/git-newbie-playground and follow the instructions. Scream if something does not work! --- class: center, middle, inverse # Downloading Repositories - Fork and Clone --- ## Cloning a Repository ### 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. <sup>1</sup> .footnote[ [1] [Setting up a repository](https://www.atlassian.com/git/tutorials/setting-up-a-repository) by Atlassian Bitbucket, [Creative Commons Attribution 2.5 Australia License](https://creativecommons.org/licenses/by/2.5/au/) ] -- 💡 a repository can live at two places: - only **local**: files are on your computer and nowhere else - only **remote**: files are stored online at GitHub/GitLab/... - **local and remote**: files are both on your computer and stored online, **and the repositories can be different** --- ## 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. <sup>1</sup> A *fork* can only be created online and not locally. ### Clone > When you create a repository on GitHub, it exists as a remote repository. You can clone your repository to create a local copy on your computer and sync between the two locations. <sup>2</sup> .footnote[ [1] [Fork a repo](https://docs.github.com/en/github/getting-started-with-github/fork-a-repo) by GitHub Docs team and contributors, [Creative Commons Attribution 4.0 International](https://github.com/github/docs/blob/main/LICENSE) [2] [Cloning a repository](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) by GitHub Docs team and contributors, [Creative Commons Attribution 4.0 International](https://github.com/github/docs/blob/main/LICENSE) ] --- ## Hands On - Clone a Eepository ### Clone your project repository - https://github.com/CorrelAid/{project-name}/ - check the slack channel of your project for the exact URL (it should be at the top in the "Topic" box) ### Clone your own `git-tryout` repo - see instructions here [https://github.com/CorrelAid/git-newbie-playground](https://github.com/CorrelAid/git-newbie-playground) --- ## Hands On - Clone a Repo with the Command Line .pull-left[ 1. Use `cd` in your terminal or Git bash to navigate to your target location (use the tab key for autocompletion) 2. Use `git clone` and either the HTTPS (`https://github.com/{username}/git-newbie-playground.git`) or SSH (`git@github.com:{username}/git-newbie-playground.git`) URL of the repository ] .pull-right[  ] --- ## Hands On - Clone a Repo with RStudio .pull-left[ 1. In the top right corner, click on the dropdown and choose New project 2. Select "Version Control" 3. Follow the dialogue ] .pull-right[  ] --- class: center, middle, inverse # What is Git? --- ## Git Snapshots > Git thinks of its data more like a series of snapshots of a miniature filesystem. With Git, every time you commit, or save the state of your project, Git basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. <sup>1</sup>  .footnote[ [1] [Getting Started - What is Git?](https://www.git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F) ] --- ## The Three States Git has three main states that your files can reside in: modified, staged, and committed: - **Modified** means that you have changed the file but have not committed it to your database yet. - **Staged** means that you have marked a modified file to go into your next commit snapshot. - **Committed** means that the data is safely stored in your local database.  --- ## Git History > Everything in Git is checksummed before it is stored and is then referred to by that checksum. This means it’s impossible to change the contents of any file or directory without Git knowing about it.<sup>1</sup> Git stores a history of snapshots / commits in its graph. ```bash git graph ```  .footnote[ [1] [Getting Started - What is Git?](https://www.git-scm.com/book/en/v2/Getting-Started-What-is-Git%3F) ] --- ## Git history - Show a commit the "letter 🥗" is the short version of the so called "SHA hash" or "commit hash" -> it comes from "hashing" the snapshot the commit represents. We can have a look at a commit using `git show {hash}`  --- ## Hands On: Investigate the Commit History Investigate the commits made so far in your repository. How many are there? Who has made them? What changes did they introduce? .pull-left[ ### Command line - use `git graph` and `git show` as described in the slides before - if you want to "break out" of a `git show` or `git graph` display which is too long, you can - hit enter until it ends - hit q - hit ctrl+c ] .pull-right[ ### RStudio - open the Git pane (next to "Environment", "History", etc.) - click on the small clock icon 🕙 to open the history - close the extra window once you have investigated enough ] --- class: center, middle, inverse # The Local Workflow - Add und 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. <sup>1</sup> --> Commit = a "save point" or snapshot in Git. The basic Git workflow goes something like this: 1. You modify files in your working tree. 2. You selectively stage just those changes you want to be part of your next commit, which adds only those changes to the staging area. 3. You do a **commit**, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory. .footnote[ [1] [Saving changes](https://www.atlassian.com/git/tutorials/saving-changes) by Atlassian Bitbucket, [Creative Commons Attribution 2.5 Australia License](https://creativecommons.org/licenses/by/2.5/au/) ] --- ## Commit - a commit stores **a full snapshot** of the current state of the repository - to the user, it is shown as so-called "diffs", i.e. what has changed since the last commit - edits of file(s) - creation of new file(s) - deletion of file(s) - renaming of file(s) --> making a commit = taking a snapshot --- class: center ## Steps to Create a Commit .pull-left[  [Staging Area illustration](https://git-scm.com/images/about/index1@2x.png), from [About - Staging Area](https://git-scm.com/about/staging-area) by Scott Chacon and contributors, licensed under [Creative Commons Attribution 3.0 Unported License](https://creativecommons.org/licenses/by/3.0/) ] .pull-right[ **The "repository" is stored in the `.git` folder in your your project folder** ] --- ## Making a Commit - Adding In the staging area, you **draft** your commit and choose which of your changes you want to "save" in Git **Most important Git command**: see what is currently going on 👀 ```bash git status ``` ```bash git diff git diff --staged ``` Add individual files or folders ```bash git add {path} (-p) ``` or everything ```bash git add . ``` --- ## Making a Commit - Adding ```bash git status ```  We add `file.txt` to our "commit draft": ```bash git add file.txt ``` --- ## Making a Commit - Adding `.gitignore` and `new_file.txt` will not be part of the commit:  Before you run `git commit` make sure you always! inspect the changes first with `git status`. --- ## Making a Commit - Commit Now, we can commit: ```bash git commit -m "change file.txt" ``` A commit must have a commit message. If you omit `-m`, `git` will open the default editor so you can enter a message. --- ## Making a Commit - Shortcuts We can add all changes with: ```bash git add . ```  ... or even commit all our changes directly without `git add`: ```bash git commit -a -m 'directly committing all changes' ``` <font color="red">it's tempting but use both with care!</font> --- ## Making a Commit in RStudio .pull-left[ - Git pane, then "Commit" button -> extra window opens - `git add`: tick checkboxes - `git commit`: commit message in box, then press commit ] .pull-right[ <img src="images/rstudio/git_commit.png" width="100%" /> ] --- class: center, middle, inverse # Detour: Markdown --- ## Markdown - a markup language - easy way to write... - notes - texts - presentations - ... - and *render* them to good looking documents! - In R: RMarkdown --- ## Markdown Syntax - `#(###)`: make a header - `*` or `-`: make bullet point lists - `[click here](https://git-scm.com/docs/)`: add a link - ... many more, see [https://www.markdownguide.org/basic-syntax/](https://www.markdownguide.org/basic-syntax/) --- ## Hands On - Make a Commit .pull-left[ ### command line 1. Change the content of `README.md` and create a new file `test.py` with a simple print statement 2. commit only the new file, not your changes to the README 3. Run `git graph` to see your commit ] .pull-right[ ### RStudio 1. Change the content of `README.md` and create a new file `test.R` with a simple print statement 2. commit only the new file, not your changes to the README 3. go to the Git Pane ] --- class: center, middle ## Git Quizzed!  --- class: center, middle ## Git Quizzed!  --- class: center, middle, inverse # Sync Files - Push and Pull --- ## Git Hosting - GitHub: <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> - GitLab: <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M105.2 24.9c-3.1-8.9-15.7-8.9-18.9 0L29.8 199.7h132c-.1 0-56.6-174.8-56.6-174.8zM.9 287.7c-2.6 8 .3 16.9 7.1 22l247.9 184-226.2-294zm160.8-88l94.3 294 94.3-294zm349.4 88l-28.8-88-226.3 294 247.9-184c6.9-5.1 9.7-14 7.2-22zM425.7 24.9c-3.1-8.9-15.7-8.9-18.9 0l-56.6 174.8h132z"></path></svg> - Bitbucket: <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M22.2 32A16 16 0 0 0 6 47.8a26.35 26.35 0 0 0 .2 2.8l67.9 412.1a21.77 21.77 0 0 0 21.3 18.2h325.7a16 16 0 0 0 16-13.4L505 50.7a16 16 0 0 0-13.2-18.3 24.58 24.58 0 0 0-2.8-.2L22.2 32zm285.9 297.8h-104l-28.1-147h157.3l-25.2 147z"></path></svg> - Keybase: <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M286.17 419a18 18 0 1 0 18 18 18 18 0 0 0-18-18zm111.92-147.6c-9.5-14.62-39.37-52.45-87.26-73.71q-9.1-4.06-18.38-7.27a78.43 78.43 0 0 0-47.88-104.13c-12.41-4.1-23.33-6-32.41-5.77-.6-2-1.89-11 9.4-35L198.66 32l-5.48 7.56c-8.69 12.06-16.92 23.55-24.34 34.89a51 51 0 0 0-8.29-1.25c-41.53-2.45-39-2.33-41.06-2.33-50.61 0-50.75 52.12-50.75 45.88l-2.36 36.68c-1.61 27 19.75 50.21 47.63 51.85l8.93.54a214 214 0 0 0-46.29 35.54C14 304.66 14 374 14 429.77v33.64l23.32-29.8a148.6 148.6 0 0 0 14.56 37.56c5.78 10.13 14.87 9.45 19.64 7.33 4.21-1.87 10-6.92 3.75-20.11a178.29 178.29 0 0 1-15.76-53.13l46.82-59.83-24.66 74.11c58.23-42.4 157.38-61.76 236.25-38.59 34.2 10.05 67.45.69 84.74-23.84.72-1 1.2-2.16 1.85-3.22a156.09 156.09 0 0 1 2.8 28.43c0 23.3-3.69 52.93-14.88 81.64-2.52 6.46 1.76 14.5 8.6 15.74 7.42 1.57 15.33-3.1 18.37-11.15C429 443 434 414 434 382.32c0-38.58-13-77.46-35.91-110.92zM142.37 128.58l-15.7-.93-1.39 21.79 13.13.78a93 93 0 0 0 .32 19.57l-22.38-1.34a12.28 12.28 0 0 1-11.76-12.79L107 119c1-12.17 13.87-11.27 13.26-11.32l29.11 1.73a144.35 144.35 0 0 0-7 19.17zm148.42 172.18a10.51 10.51 0 0 1-14.35-1.39l-9.68-11.49-34.42 27a8.09 8.09 0 0 1-11.13-1.08l-15.78-18.64a7.38 7.38 0 0 1 1.34-10.34l34.57-27.18-14.14-16.74-17.09 13.45a7.75 7.75 0 0 1-10.59-1s-3.72-4.42-3.8-4.53a7.38 7.38 0 0 1 1.37-10.34L214 225.19s-18.51-22-18.6-22.14a9.56 9.56 0 0 1 1.74-13.42 10.38 10.38 0 0 1 14.3 1.37l81.09 96.32a9.58 9.58 0 0 1-1.74 13.44zM187.44 419a18 18 0 1 0 18 18 18 18 0 0 0-18-18z"></path></svg> --- ## Git local and Git remote - **local**: your laptop/machine - **remote**: in the Cloud (GitLab, GitHub...) - you typically only have one remote (called "origin") but you can have multiple ```bash $ git remote -v origin https://github.com/CorrelAid/git-newbie-playground.git (fetch) origin https://github.com/CorrelAid/git-newbie-playground.git (push) ``` Both `git status` and `git graph` can show you the local HEAD and the remote HEAD:  --- ## Git HEAD > How does Git know what branch you’re currently on? It keeps a special pointer called `HEAD`. In Git, this is a pointer to the local branch and commit you’re currently on. In this case, you’re still on master. <sup>1</sup>  .footnote[ [1] [Git branches](https://www.git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell) ] --- ## Sync: Git Pull and Git Push "Download" new commits from GitHub ```bash git pull ``` "Upload" your local commits to GitHub ```bash git push ``` --- ## Pull and Push - RStudio - Git pane / Git commit window - button down (blue): pull - button up (green): push --- class: center, middle # Hands On: Push!  Upload your latest local changes to GitHub and check the results in the browser. .footnote[ [Go Do It Gif](https://giphy.com/gifs/ren-and-stimpy-139lMwJ9ow7bKE) via [Giphy](https://giphy.com), [original source](https://www.fanpop.com/clubs/ren-and-stimpy/images/35557868/title/ren-stimpy-photo) ] --- ## Pull, Commit, Push Workflow 1. Run `git pull` before you resume your work to get the updates from your team mates that they have uploaded 2. Run `git add` whenever you feel like you have achieved something or you want to "save" some intermediate code snippets 3. Run `git commit` when you are ready to save your changes as a new snapshot (commit often!) 4. Run `git push` to share your commits with others or to "backup" your work --- class: center, middle ## Git Quizzed!  --- ## Git Quizzed!  --- class: center, middle, inverse # When Things Go Wrong... --- class: center, middle  --- ## 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 3. read the error messages, they can be quite useful! 4. if in doubt, ask the other team members or reach out to me. :) There are special Git commands to help you in case of unwanted changes like [git restore](https://www.git-scm.com/docs/git-restore), [git reset](https://www.git-scm.com/docs/git-reset), and [git rebase](https://www.git-scm.com/docs/git-rebase). However, you have to really understand them before you apply them! --- ## 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. <sup>1</sup> ```bash $ git merge whitespace Auto-merging hello.rb CONFLICT (content): Merge conflict in hello.rb Automatic merge failed; fix conflicts and then commit the result. ``` .footnote[ [2] [Resolving a merge conflict using the command line](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line) by GitHub Docs team and contributors, [Creative Commons Attribution 4.0 International](https://github.com/github/docs/blob/main/LICENSE) ] --- ## 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  .footnote[ Source: [you decide season 5 GIF by Portlandia](https://giphy.com/gifs/portlandia-season-5-episode-4-xUOxfcCFf8z89a6KTC) via [Giphy](https://giphy.com) ] --- ## Solving Merge Conflicts - **you** need to solve merge conflicts on your machine -> decide which version / changes you keep - options: - edit files manually and look out for *conflict markers* (explanation [here](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/resolving-a-merge-conflict-using-the-command-line#competing-line-change-merge-conflicts)) - use a merge conflict tool in the command line (`git mergetool --tool-help`) - VSCode has an integrated [merge conflict tool](https://www.youtube.com/watch?v=kBIMGOxqqnk) which is very nice - other Git GUIs also might have one - RStudio does not have a merge conflict tool :( -> have to resolve manually ``` <<<<<<< HEAD i added this line in Rstudio without knowing that someone else edited it ======= introduce a merge conflict, hihihihiihi >>>>>>> 6185e4e97db38d774fdcd2e41a31788362856d51 ``` ⚠️: a file can have **multiple** conflicts! --- ## Solving Merge Conflicts ``` <<<<<<< HEAD i added this line in Rstudio without knowing that someone else edited it ======= introduce a merge conflict, hihihihiihi >>>>>>> 6185e4e97db38d774fdcd2e41a31788362856d51 ``` - `<<<<<<<`: my current changes (remember: `HEAD` points to your local current commit) - `=======`: seperates my changes from the "incoming" changes - `>>>>>>>`: end of "incoming" changes, shows the commit hash or the branch name (more on this later) my new version: ```md merge conflicts might be a bit frightening but you can do it! :) ``` **save, add, and commit!** --- ## Abort Merge You can also abort a Git merge any time! ```bash $ git status -sb ## master UU hello.rb $ git merge --abort $ git status -sb ## master ``` The `git merge --abort` option tries to revert back to your state before you ran the merge. --- ## Hands On: Merge Conflicts 1. Edit `README.md` **on GitHub** in a certain line 2. Edit `README.md` locally on your machine **in the same line** (edit it differently!) 3. Commit your own changes (don't push yet!) 4. Run `git push` -> you should get an error that the push was rejected due to changes on the remote 4. Run `git pull` -> this should also fail due to merge conflicts 5. Solve the merge conflict locally and push your own version. 😈 --- class: center, middle, inverse # Work with GitHub --- class: center, middle, inverse # Issues --- ## Issues - issues: todos / bugs / ideas / tasks - can be assigned to a team member (but sadly only to a single person) - allows for detailed discussions and supports Markdown - can be assigned to a Kanban board in GitHub Projects - each issue has a number - \#<issueno> in commit message associates commit with issue - example: [https://github.com/CorrelAid/hugo-website/issues](https://github.com/CorrelAid/hugo-website/issues) --- ## Hands On: Issues 1. create an issue "add a GIF" 2. go to [giphy.com](giphy.com), find a funny gif and add it to the README (-> copy link) 3. add + commit. link the issue number in the commit message (#issueno) 4. push 5. reload the issue page (ctrl + r) --- ## Recap Issues - very useful for project organization - discussion is centralized (**but**: don't put sensitive information in GitHub) - Tip: create a GitHub *board* for process tracking --- class: center, inverse, middle # Branches and Pull Requests --- class: center, middle  --- class: center, middle  .footnote[ [Bored Come On GIF](https://giphy.com/gifs/HfFccPJv7a9k4) via [Giphy](https://giphy.com), [original source](https://imgur.com/gallery/5OI0pWP) ] --- ## Branches .smaller[ ### 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.<sup>1</sup> ### 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.<sup>2</sup> .footnote[ [1] [Git Branch](https://www.atlassian.com/git/tutorials/using-branches) by Atlassian Bitbucket, [Creative Commons Attribution 2.5 Australia License](https://creativecommons.org/licenses/by/2.5/au/) [2] [Git Checkout](https://www.atlassian.com/git/tutorials/using-branches/git-checkout) by Atlassian Bitbucket, [Creative Commons Attribution 2.5 Australia License](https://creativecommons.org/licenses/by/2.5/au/) ] ] --- ## Why Branches? - stability: only have working code on "main" branch - e.g. CI/CD directly to dev - users download your code from GitHub / GitHub (e.g. `devtools::install_github()`) -- - collaboration: independent development of code ("feature branches") -- - experiments: try it out in a branch -- - example: [https://github.com/tidyverse/dplyr/branches](https://github.com/tidyverse/dplyr/branches) --- ## Branches Workflow 1. create a branch in Git with a meaningful name (e.g. `issue1-add-favorite-gif`) ``` git switch -c "#1-add-favorite-gif" ``` ``` git checkout -b "#1-add-favorite-gif ``` -- 2. checkout branch: branch is automatically checked out (check with `git graph`: `HEAD` points to `#1-add-favorite-gif`) -- 3. continue working as usual (pull-commit-push cycles) -- 4. (optional): merge other branches into your branch to get their updates: `git merge {branch_name}` -- 5. merge your branch into the main branch (checkout main, then see 4.) (better: Pull-Request) ✅ You can always switch between branches using `git checkout`. This will modify your working tree and load all files for this branch. However, uncommitted files will be unaffected and stay! --- ## Branches Workflow - RStudio .pull-left[ - Git pane, then on the right - creating a branch: click on purple icon - switch branches: click on dropdown / branch name ] .pull-right[  ] --- ## 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. <sup>1</sup> 🔥 The most useful GitHub feature for your whole project (besides GitHub Projects...). Use it! - Useful "review" and feedback functionality - [GitHub Hello World](https://guides.github.com/activities/hello-world/#pr) - Make the life of reviewers easier and don't open huge PR with a lot of changes. Instead, commit often and do small PR. - Open PR early (first commit of a new branch) so that people can comment on your work while you are still working on it to avoid reverting changes that were a lot of effort .footnote[ [1] [About pull requests](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/about-pull-requests) by GitHub Docs team and contributors, [Creative Commons Attribution 4.0 International](https://github.com/github/docs/blob/main/LICENSE) ] --- ## Hands On - Branches and PRs 1. Create a new issue (think of something!) 2. Create a branch called `issue2-{description}` locally 3. Work on issue 2 4. Push the branch 5. Open a pull request 6. Merge the PR --- ## 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 main clean of non-working code, backup (!) - merge requests - centralize discussion - find and discuss best approaches to a problem - code review stage --- class: center, middle, inverse # Miscellaneous --- ## .gitignore The special `.gitignore` file lets you exclude certain files from Git (and GitHub). This is especially useful for... - sensitive data (e.g. GDPR data that should not be uploaded to GitHub) - user configuration files (e.g. `.Rproj.user`) - data that can be fetched from elsewhere (data sets like CSV files downloaded from the Internet) - big files The `.gitignore` file is just a list of files and paths that you don't want to add to Git. -- ### Accidents happen If you accidentally committed files to the repository: ``` git rm --cached [file path] ``` followed by a commit and adding the file to `.gitignore`. (learn more at the bottom of the page [here](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository)) --- ## Git Stash > `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.<sup>1</sup> .footnote[ [1] [Git stash](https://www.atlassian.com/git/tutorials/saving-changes/git-stash) by Atlassian Bitbucket, [Creative Commons Attribution 2.5 Australia License](https://creativecommons.org/licenses/by/2.5/au/) ] --- ## Git Stash .center[  [Doc Savage Hiding GIF by Warner Archive](https://giphy.com/gifs/warnerarchive-warner-archive-doc-savage-l0HlxJfVUKhtR8Jna?utm_source=media-link&utm_medium=landing&utm_campaign=Media%20Links&utm_term=) via [Giphy](https://giphy.com) ] - put it away for now! - useful for (temporarily) storing stuff not ready to commit --- class: middle, center, inverse # Recap --- ## What we learned today - Inspect Git history and single commits -- - Fork and clone a GitHub repository -- - Make a commit -- - Pull and push commits from / to GitHub -- - Solve merge conflicts -- - Work with issues, branches and pull requests --- class: middle, center, inverse # That's it! --- ## If you ever need help...contact me! ### Frie Preu + <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M320 336c0 8.84-7.16 16-16 16h-96c-8.84 0-16-7.16-16-16v-48H0v144c0 25.6 22.4 48 48 48h416c25.6 0 48-22.4 48-48V288H320v48zm144-208h-80V80c0-25.6-22.4-48-48-48H176c-25.6 0-48 22.4-48 48v48H48c-25.6 0-48 22.4-48 48v80h512v-80c0-25.6-22.4-48-48-48zm-144 0H192V96h128v32z"></path></svg> COO at CorrelAid / @frie on Slack + <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> [friep](https://github.com/friep) + <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M105.2 24.9c-3.1-8.9-15.7-8.9-18.9 0L29.8 199.7h132c-.1 0-56.6-174.8-56.6-174.8zM.9 287.7c-2.6 8 .3 16.9 7.1 22l247.9 184-226.2-294zm160.8-88l94.3 294 94.3-294zm349.4 88l-28.8-88-226.3 294 247.9-184c6.9-5.1 9.7-14 7.2-22zM425.7 24.9c-3.1-8.9-15.7-8.9-18.9 0l-56.6 174.8h132z"></path></svg> [friep](https://gitlab.com/friep) + <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M286.17 419a18 18 0 1 0 18 18 18 18 0 0 0-18-18zm111.92-147.6c-9.5-14.62-39.37-52.45-87.26-73.71q-9.1-4.06-18.38-7.27a78.43 78.43 0 0 0-47.88-104.13c-12.41-4.1-23.33-6-32.41-5.77-.6-2-1.89-11 9.4-35L198.66 32l-5.48 7.56c-8.69 12.06-16.92 23.55-24.34 34.89a51 51 0 0 0-8.29-1.25c-41.53-2.45-39-2.33-41.06-2.33-50.61 0-50.75 52.12-50.75 45.88l-2.36 36.68c-1.61 27 19.75 50.21 47.63 51.85l8.93.54a214 214 0 0 0-46.29 35.54C14 304.66 14 374 14 429.77v33.64l23.32-29.8a148.6 148.6 0 0 0 14.56 37.56c5.78 10.13 14.87 9.45 19.64 7.33 4.21-1.87 10-6.92 3.75-20.11a178.29 178.29 0 0 1-15.76-53.13l46.82-59.83-24.66 74.11c58.23-42.4 157.38-61.76 236.25-38.59 34.2 10.05 67.45.69 84.74-23.84.72-1 1.2-2.16 1.85-3.22a156.09 156.09 0 0 1 2.8 28.43c0 23.3-3.69 52.93-14.88 81.64-2.52 6.46 1.76 14.5 8.6 15.74 7.42 1.57 15.33-3.1 18.37-11.15C429 443 434 414 434 382.32c0-38.58-13-77.46-35.91-110.92zM142.37 128.58l-15.7-.93-1.39 21.79 13.13.78a93 93 0 0 0 .32 19.57l-22.38-1.34a12.28 12.28 0 0 1-11.76-12.79L107 119c1-12.17 13.87-11.27 13.26-11.32l29.11 1.73a144.35 144.35 0 0 0-7 19.17zm148.42 172.18a10.51 10.51 0 0 1-14.35-1.39l-9.68-11.49-34.42 27a8.09 8.09 0 0 1-11.13-1.08l-15.78-18.64a7.38 7.38 0 0 1 1.34-10.34l34.57-27.18-14.14-16.74-17.09 13.45a7.75 7.75 0 0 1-10.59-1s-3.72-4.42-3.8-4.53a7.38 7.38 0 0 1 1.37-10.34L214 225.19s-18.51-22-18.6-22.14a9.56 9.56 0 0 1 1.74-13.42 10.38 10.38 0 0 1 14.3 1.37l81.09 96.32a9.58 9.58 0 0 1-1.74 13.44zM187.44 419a18 18 0 1 0 18 18 18 18 0 0 0-18-18z"></path></svg> [friep](https://keybase.io/friep) + <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> [ameisen_strasse](https://twitter.com/ameisen_strasse) + <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M94.12 315.1c0 25.9-21.16 47.06-47.06 47.06S0 341 0 315.1c0-25.9 21.16-47.06 47.06-47.06h47.06v47.06zm23.72 0c0-25.9 21.16-47.06 47.06-47.06s47.06 21.16 47.06 47.06v117.84c0 25.9-21.16 47.06-47.06 47.06s-47.06-21.16-47.06-47.06V315.1zm47.06-188.98c-25.9 0-47.06-21.16-47.06-47.06S139 32 164.9 32s47.06 21.16 47.06 47.06v47.06H164.9zm0 23.72c25.9 0 47.06 21.16 47.06 47.06s-21.16 47.06-47.06 47.06H47.06C21.16 243.96 0 222.8 0 196.9s21.16-47.06 47.06-47.06H164.9zm188.98 47.06c0-25.9 21.16-47.06 47.06-47.06 25.9 0 47.06 21.16 47.06 47.06s-21.16 47.06-47.06 47.06h-47.06V196.9zm-23.72 0c0 25.9-21.16 47.06-47.06 47.06-25.9 0-47.06-21.16-47.06-47.06V79.06c0-25.9 21.16-47.06 47.06-47.06 25.9 0 47.06 21.16 47.06 47.06V196.9zM283.1 385.88c25.9 0 47.06 21.16 47.06 47.06 0 25.9-21.16 47.06-47.06 47.06-25.9 0-47.06-21.16-47.06-47.06v-47.06h47.06zm0-23.72c-25.9 0-47.06-21.16-47.06-47.06 0-25.9 21.16-47.06 47.06-47.06h117.84c25.9 0 47.06 21.16 47.06 47.06 0 25.9-21.16 47.06-47.06 47.06H283.1z"></path></svg> #help channel ### Resources Check the README of the [git-newbie-playground repo](https://github.com/CorrelAid/git-newbie-playground) for some more resources!