background-image: url("images/bg_web.png") background-size: cover class: left, middle <a href="https://github.com/edgar-treischl/Workshop_GitHubIntro" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#C51717; color:#ffff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style> <br/> # .white[Git(Hub)] ## .white[A Gentle Introduction] ## .white[for R Users] <h3><a href="http://www.edgar-treischl.de" target="_blank" style="color:#C51717;">Dr. Edgar J. Treischl</a></h3> <br/> <br/> .white[Last update: 2025-01-25] <br/> <br/> <h4 style = "color:#ffff; text-align: right;">Press ⭕ or ➡️</h4> <div class="remark-footer"><a href="https://unsplash.com/de/@synkevych" target="_blank" style="color: gray;">Images: Roman Synkevych</a></div> --- ## Agenda .pull-left[ ### 1️⃣ Git/GitHub ... What? ### 2️⃣ Getting Started ### 3️⃣ Basics of Git ### 4️⃣ Gist ] .pull-right[ <a href="https://edgar-treischl.github.io/PracticeR/" target="_blank"><img src="https://edgar-treischl.github.io/PracticeR/reference/figures/cover.png" alt="Practice R by Edgar Treischl" width="200"/></a> This workshop is based on Practice R. ] --- background-image: url("images/ibm.gif") background-size: cover class: bottom, center # .white[1️⃣ Git/GitHub ... What?] --- ## (01) Git/GitHub ... What? .left-column[ <br/> <a href="https://git-scm.com/" target="_blank"><img src="https://git-scm.com/images/logo@2x.png" style="width: 70%"/></a> <br/> <br/> <br/> <a href="https://github.com" target="_blank"><img src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png" alt="Github" width="60%"/></a> ] .right-column[ - Git is a version control system to track changes over time - Continuous integration (CI) and continuous deployment (CD) - GitHub (GitLab) is an host (website) for Git-based projects - Each project is stored in a repository (folder with all files) - A repository can be *public* or *private* ### Why Hosts like GitHub? - Easier to track code changes - Share work - Automated workflows - Websites for projects - Work together - ... ] --- ## (01) Git/GitHub ... What? .panelset[ .panel[.panel-name[Track Code] <img src="images/Fig_0901.png" style="width: 90%"/> ] .panel[.panel-name[Share Code] .pull-left[ ``` r # Source the script link <- paste0("https://raw.githubusercontent.com/", "edgar-treischl/edgar/refs/heads/", "master/inst/graphs/long_wide.R") devtools::source_url(link) showplot() ``` ``` r # Run a shiny app from GitHub shiny::runGitHub("power-app", "edgar-treischl", ref="main") ``` ] .pull-right[ <img src="images/plot-last-1.png" style="width: 90%"/> ] ] .panel[.panel-name[Automate workflows] - Deploy a website, book, presentation - Run tests - Create a package .pull-left[ ```yaml on: push: branches: [main, master] jobs: pkgdown: runs-on: ubuntu-latest steps: - uses: my_script.R ``` ] .pull-right[ <img src="images/workflow.png" style="width: 100%"/> ] ] ] --- background-image: url("images/g3.gif") background-size: cover class: bottom, center # .white[2️⃣ Getting Started] --- ## (02) Getting Started ### Push and pull 💪 on a regular basis: 1. Install Git: - Install on your local machine - Introduce *yourself* - Learn some *Git* 2. Connect to GitHub - Create a *Personal Access Token* (PAT) - Create a *Repository* 3. Connect GitHub and R Studio - *Clone* the newly created repository - *Setup R Studio* and connect to GitHub - Get in touch with the R Studio *Git pane* --- ## (02) Install Git on your local machine .left-column[ <br/> <a href="https://git-scm.com/" target="_blank"><img src="https://git-scm.com/images/logo@2x.png" style="width: 70%"/></a> <br/> <br/> <br/> <a href="https://github.com" target="_blank"><img src="https://github.githubassets.com/assets/GitHub-Mark-ea2971cee799.png" alt="Github" width="60%"/></a> ] .right-column[ - Windows: https://git-scm.com/download/win - OS X: https://git-scm.com/download/mac - Debian/Ubuntu: sudo apt-get install git-core ``` bash # I am git code to check if git is installed # Go to the console and type: git --version ``` ``` ## git version 2.39.5 (Apple Git-154) ``` - Next, create a free GitHub account ] --- background-image: url(https://usethis.r-lib.org/logo.png) background-position: 90% 5% background-size: 10% ## (02) Install Git: Introduce yourself ### Via the shell ```bash #In the console git config --global user.name "User Name" git config --global user.email "email@adress.com" ``` ### Or with the 🚀 usethis package ``` r library(usethis) use_git_config(user.name = "Jane Doe", user.email = "jane@example.org") ``` --- ## (02) Install Git: Introduce yourself ### Check global configuration settings ``` bash git config --global --list ``` ``` ## filter.lfs.clean=git-lfs clean -- %f ## filter.lfs.smudge=git-lfs smudge -- %f ## filter.lfs.process=git-lfs filter-process ## filter.lfs.required=true ## user.name=Edgar Treischl ## user.email=77931249+edgar-treischl@users.noreply.github.com ## core.excludesfile=~/.gitignore ``` --- background-image: url("images/cat.gif") background-size: cover class: bottom, center # .white[Git can be scary, but ...] --- ## (02) Install Git: New Vocabulary 🤺 .pull-left[ Wait, Git is scary because it's full of technical jargon<sup>1</sup>: - **Repository**: A folder with all project files - **Push**: Send code (files) to Git - **Pull**: Get code (files) from a Git repository - **Commit**: A snapshot of a repository at a certain point in time - **Branch**: A parallel version within the repository to test new features ... .footnote[ [1] As often, R Studio helps us to work with Git. ] ] .pull-right[ <figure> <img src="images/allison_horst_web.png" style="width: 100%"/> <figcaption style = "color: darkgray;">'Artwork by @allison_horst'</figcaption> </figure> ] --- ## (02) Install Git: New Vocabulary 🤺 .pull-left[ - **Merge**: Combine two branches - **Merge conflicts**: When two branches have changed the same part of a file, Git will not be able to automatically determine what is correct - **Forks**: Forks are interconnected repositories, for example, to examine a repository from someone else - **Pull requests**: A way to suggest changes to a repository ] .pull-right[ <figure> <img src="images/allison_horst_web.png" style="width: 100%"/> <figcaption style = "color: darkgray;">'Artwork by @allison_horst'</figcaption> </figure> ] --- ## (02) Connect to GitHub: Create a PAT ### Create a PAT in R Studio: ``` r # The create_github_token() function will open a browser window to create a token usethis::create_github_token() #The set_github_pat() function will store the token in the .Renviron file credentials::set_github_pat("token") ``` ### Or manually via: On the GitHub website, go to: - Settings > Developer settings > Personal access tokens: - https://github.com/settings/tokens --- ## (02) Connect to GitHub ### Check if R Studio is connected to GitHub: ``` r # Actually, whoami will show you who you are on GitHub ... gh::gh_whoami() ``` ``` ## { ## "name": "Edgar Treischl", ## "login": "edgar-treischl", ## "html_url": "https://github.com/edgar-treischl", ## "scopes": "gist, repo, user, workflow", ## "token": "ghp_...Cmqg" ## } ``` #### 🎌 ... you're connected! --- ## (02) Connect to GitHub: Create a repository <img src="images/Fig_0903.png" style="width: 60%"/> --- ## (02) Connect to GitHub: Create a repository <img src="images/Fig_0904.png" style="width: 80%"/> --- ## (02) Connect GitHub and R Studio .pull-left[ #### 1. Create a new project in R Studio and click next. #### 2. Pick Version Control and cick next. #### 3. Pick Git and click next. #### 4. Paste the repository URL: ➡️ ] .pull-right[ <img src="images/Fig_0905.png" style="width: 100%"/> ] --- ## (02) Connect GitHub and R Studio: Git Pane ### Once R Studio is connected, search for the Git pane: <img src="images/Fig_0906.png" style="width: 80%"/> --- ## (02) Connect GitHub and R Studio: Git Pane ### You gonna stage, commit, push, and let it all out 💃 <img src="images/Fig_0907.png" style="width: 75%"/> --- background-image: url("images/practice.gif") background-size: cover class: bottom, center # .white[Practice Makes Perfect] --- ## Practice Makes Perfect ### ➕ Create a new repository on GitHub ### 💻 Clone the repository on your local machine ### 📁 Add a new repository file ### 🈯 Commit the changes ### 📭 Push the changes to the remote repository --- background-image: url("images/merge.gif") background-size: cover class: bottom, center # .white[3️⃣ Basics of Git] --- ## (03) Git basics ```bash # Initialize a new repository git init <your repository name> # Clone a repository git clone <git-repo-url> # Create a new branch git branch <branch-name> # List all branches git branch --list # Delete a branch git branch -d <branch-name> # Switch to a branch git checkout <branch-name> ``` --- ## (03) Git for the daily workflow .pull-left[ ```bash # Add a file to the staging area git add <file-name-1> # Add all files to the staging area git add -A # Commit changes git commit -am <commit-message> # Push changes to the remote rep git push <remote> <branch-name> # Pull changes from the remote rep git pull # Merge two branches git merge <branch-name> ``` ] .pull-right[ <br/> <br/> <figure> <img src="images/allison_horst_gf.png" style="width: 100%"/> <figcaption style = "color: darkgray;">'Artwork by @allison_horst'</figcaption> </figure> ] --- ## (03) Even more Git? 💥 .pull-left[ #### Do I need all the code if I am happy with the Git Pane? - The Pane simplifies common Git operations, but Git Code gives you more control, flexibility, and the ability to handle complex operations - **Common example**: Merge conflicts can occur when two branches have changed the same part of a file and Git will not be able to automatically determine what is correct - Git commands give you a more detailed understanding of where the conflicts are happening. This is particularly useful when the conflict isn't straightforward to resolve. - Of course, we don't have to remember in detail 😉 ] .pull-right[ #### Merge Conflict: ```bash # <<<<<<< HEAD # print("This is my feature branch.") # ======= # print("This is my master branch.") # >>>>>>> master ``` #### Typical workflow to solve the merge conflict: - Manually resolve the conflict in the file(s) and run: ```bash git add script.R git commit -m "Resolved merge conflict" ``` ] --- ## (03) Git for troubleshooting 💀 .pull-left[ ```bash # Show who changed what and when git blame file.txt # Shows the working tree status git status # Shows changes between commits git diff #💀💀💀 DANGER # Revert some existing commits git revert SHA # Reset the repository to a previous commit # Take care: This will discard any local changes git reset #Ultima ratio ;-) # A "hard reset": forcefully discard any local changes ``` ] .pull-right[ <br/> <br/> <figure> <a href="https://git-scm.com/book/de/v2" target="_blank"><img src="https://git-scm.com/images/progit2.png" alt="Pro Git by Chacon and Straub" width="50%"/></a> <figcaption style = "color: darkgray;">'Pro Git by Chacon and Straub'</figcaption> </figure> ] --- ## (03) Commit history in R Studio <img src="images/Fig_0908.png" style="width: 80%"/> --- background-image: url("images/bite.gif") background-size: cover class: bottom, center # .black[4️⃣ Gist] --- ## (04) Gist .panelset[ .panel[.panel-name[Package] .pull-left[ GitHub Gist is a service provided by GitHub that allows you to share and store snippets of code, text, or any other kind of file. - Quick sharing of code (with your future self) - Code embedding - No need for a full repository ] .pull-right[ <div class="info-box"> <i>🦜️️</i> The gist package provides a set of functions to work with GitHub Gist. It allows you to copy, create, and delete gists via the GitHub API. Moreover, it comes with an R Studio addin to get access and manage your Gists. </div> <a href="https://edgar-treischl.github.io/gist/" target="_blank"><img src="https://edgar-treischl.github.io/gist/logo.png" alt="The ggplot2 package" align="center" style="width: 25%"/></a> ] ] .panel[.panel-name[R Studio Addin] .pull-left[ #### R Studio Addin: ``` r #You can install the development version via: devtools::install_github("edgar-treischl/gist") ``` ``` r #The gistAddin() gist::gistAddin() ``` ] .pull-right[ <img src="https://edgar-treischl.github.io/gist/reference/figures/gistapp.png" style="width: 75%"/> ] ] .panel[.panel-name[Via the console] .pull-left[ ``` r library(gist) #Create a gist gist_create(name = "A_Test.R", code = '#Test print("Hello world")', description = "Test Gist") ``` ``` r #gistfiles lists all files and returns names and ids gist_get(filename = "A_Test.R") #> ✔ Copied A_Test.R from your GitHub account. ``` ] .pull-right[ <a href="https://gist.github.com/edgar-treischl" target="_blank"><img src="images/gist_screen.png" alt="The ggplot2 package" align="right" style="width: 100%"/></a> ] ] ] --- class: right, middle ## Thank you for your attention! <img style="border-radius: 50%;" src="https://avatars.githubusercontent.com/u/77931249?s=400&u=eaf1d0871b643dd32cc0ff9f777edef28e6569a8&v=4" width="150px"/> [<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="M505.12019,19.09375c-1.18945-5.53125-6.65819-11-12.207-12.1875C460.716,0,435.507,0,410.40747,0,307.17523,0,245.26909,55.20312,199.05238,128H94.83772c-16.34763.01562-35.55658,11.875-42.88664,26.48438L2.51562,253.29688A28.4,28.4,0,0,0,0,264a24.00867,24.00867,0,0,0,24.00582,24H127.81618l-22.47457,22.46875c-11.36521,11.36133-12.99607,32.25781,0,45.25L156.24582,406.625c11.15623,11.1875,32.15619,13.15625,45.27726,0l22.47457-22.46875V488a24.00867,24.00867,0,0,0,24.00581,24,28.55934,28.55934,0,0,0,10.707-2.51562l98.72834-49.39063c14.62888-7.29687,26.50776-26.5,26.50776-42.85937V312.79688c72.59753-46.3125,128.03493-108.40626,128.03493-211.09376C512.07526,76.5,512.07526,51.29688,505.12019,19.09375ZM384.04033,168A40,40,0,1,1,424.05,128,40.02322,40.02322,0,0,1,384.04033,168Z"></path></svg> www.edgar-treischl.de](https://www.edgar-treischl.de) [<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> edgar-treischl](https://github.com/edgar-treischl) [<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="M464 64H48C21.49 64 0 85.49 0 112v288c0 26.51 21.49 48 48 48h416c26.51 0 48-21.49 48-48V112c0-26.51-21.49-48-48-48zm0 48v40.805c-22.422 18.259-58.168 46.651-134.587 106.49-16.841 13.247-50.201 45.072-73.413 44.701-23.208.375-56.579-31.459-73.413-44.701C106.18 199.465 70.425 171.067 48 152.805V112h416zM48 400V214.398c22.914 18.251 55.409 43.862 104.938 82.646 21.857 17.205 60.134 55.186 103.062 54.955 42.717.231 80.509-37.199 103.053-54.947 49.528-38.783 82.032-64.401 104.947-82.653V400H48z"></path></svg> edgar.treischl@isb.bayern.de](mailto:edgar.treischl@isb.bayern.de) --- class: right, middle ## Licence This presentation is licensed under a CC-BY-NC 4.0 license. You may copy, distribute, and use the slides in your own work, as long as you give attribution to the original author on each slide that you use. Commercial use of the contents of these slides is not allowed. <br/> <br/> <img src="images/by-nceu.png" alt="Image left" width="150px"/> --- ## References <p><cite>Chacon, S. and B. Straub (2014). <em>Pro Git</em>. Englisch. 2nd ed. New York, NY: Apress. ISBN: 978-1-4842-0077-3.</cite></p> <p><cite>Treischl, E. J. (2023). <em>Practice R: An interactive textbook</em>. Publication Title: Practice R. De Gruyter Oldenbourg.</cite></p> <p><cite>Wickham, H., J. Bryan, and M. Barrett (2022). <em>usethis: Automate Package and Project Setup</em>. R package version 2.1.6. URL: <a href="https://CRAN.R-project.org/package=usethis">https://CRAN.R-project.org/package=usethis</a>.</cite></p> <p><cite>Wickham, H., J. Hester, W. Chang, et al. (2022). <em>devtools: Tools to Make Developing R Packages Easier</em>. R package version 2.4.5. URL: <a href="https://CRAN.R-project.org/package=devtools">https://CRAN.R-project.org/package=devtools</a>.</cite></p>