class: center, middle, inverse, title-slide # Introduction to Git and GitHub ### Computation Skills Workshop --- # Why Git? <img src="img/bryan-1.jpeg" width="445" style="display: block; margin: auto;" /> .footnote[Source: [Excuse Me, Do You Have a Moment to Talk About Version Control?](https://www.tandfonline.com/doi/full/10.1080/00031305.2017.1399928)] -- - Version control system - Repository --- # Why GitHub? <img src="https://octodex.github.com/images/original.png" width="60%" style="display: block; margin: auto;" /> .footnote[Source: [GitHub](https://www.github.com)] --- # Why bother? .pull-left[ ## Will it hurt? **Yes** 🤷 - More complicated workflow - Clunky at first - Need to break bad habits ## What's the payoff? - Ease of access - Collaboration ] -- .pull-right[ <img src="https://media.giphy.com/media/JuWT34QY0AodfuweLa/giphy.gif" style="display: block; margin: auto;" /> ] --- # Install Git ```bash which git ## /usr/bin/git ``` -- - [Install Git](https://git-scm.com/) --- # ⏱ Configure Git .pull-left[ ## `usethis` ```r # install.packages("usethis") library(usethis) use_git_config( user.name = "Jane Doe", user.email = "jane@example.org" ) ``` ```r # default branch named 'main' git_default_branch_configure() ``` ] -- .pull-right[ ## From the shell ```bash git config --global user.name 'Jane Doe' git config --global user.email 'jane@example.com' git config --global init.defaultBranch main ``` ] --- # ⏱ Create a local Git repo - Run `usethis::use_course("css-skills/practice-repo")` in the console - Create Git repo with `use_git()` - Practice generating **commits** --- # 🔑 Key terms - Stage - Commit - Push --- # `.gitignore` ```r usethis::edit_git_ignore(scope = "project") ``` --- # Connect RStudio and GitHub - Interacting with GitHub - Providing **credentials** -- - Personal access token (PAT) --- # ⏱ Painless authentication with PAT ```r usethis::create_github_token() ``` ```r gitcreds::gitcreds_set() #> ? Enter password or token: ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx #> -> Adding new credentials... #> -> Removing credentials from cache... #> -> Done. ``` --- # ⏱ Connect project to GitHub .pull-left[ ```r usethis::use_github() ``` ] -- .pull-right[ <img src="https://media.giphy.com/media/vmon3eAOp1WfK/giphy.gif" style="display: block; margin: auto;" /> ] --- # Get a sitrep ```r usethis::git_sitrep() ``` --- # ⏱ New project, GitHub first - Create a Git controlled project already connected to GitHub 1. Make a repo on GitHub 1. New RStudio Project via `git clone` 1. Copy files from the first project to the new project - Stage - Commit - Push --- # ⏱ Incorporating changes from GitHub 1. Make a change on GitHub and commit 1. **Pull** changes from GitHub locally --- # Git for collaboration <img src="img/bryan-2.jpeg" width="445" style="display: block; margin: auto;" /> .footnote[Source: [Excuse Me, Do You Have a Moment to Talk About Version Control?](https://www.tandfonline.com/doi/full/10.1080/00031305.2017.1399928)] --- # Git for collaboration <img src="img/bryan-5.jpeg" width="445" style="display: block; margin: auto;" /> .footnote[Source: [Excuse Me, Do You Have a Moment to Talk About Version Control?](https://www.tandfonline.com/doi/full/10.1080/00031305.2017.1399928)] --- # Branching <img src="img/01 Git branch.svg" style="display: block; margin: auto;" /> .footnote[Source: [Bitbucket tutorials](https://www.atlassian.com/git/tutorials/using-branches)] --- # ⏱ Create a branch 1. Create a branch using RStudio 1. Modify a file in the repo 1. Stage, commit, and push to GitHub 1. Check the repo on GitHub -- what has changed? --- # Merging branches <img src="img/02 Branch-1 kopiera.png" width="533" style="display: block; margin: auto;" /> .footnote[Source: [Bitbucket tutorials](https://www.atlassian.com/git/tutorials/using-branches)] -- ```bash git checkout main git merge BRANCH-NAME ``` --- # Merge conflicts <img src="img/car-merge.gif" style="display: block; margin: auto;" /> --- # Merge conflicts ```bash git merge documentation # Auto-merging README.md # CONFLICT (content): Merge conflict in README.md # Automatic merge failed; fix conflicts and then commit the result. ``` -- ```bash <<<<<<< HEAD:README.md Contact: ben@bensoltoff.com ======= Please contact me at ben@bensoltoff.com >>>>>>> documentation:README.md ``` -- - Resolving merge conflicts --- # ⏱ Practice a merge conflict - Generate distinct changes in the README file - One form locally - Another form on GitHub - Attempt to push to GitHub - Pull from GitHub, resolve merge conflicts - Stage, commit, and push --- # Pull requests <img src="https://www.plannedparenthood.org/uploads/filer_public_thumbnails/filer_public/b8/06/b806c909-7071-47aa-81ba-add09b3f0a07/fries.png__800x600_q75_subsampling-2.jpg" style="display: block; margin: auto;" /> --- # Pull requests - Inform others about changes you've pushed - Discuss and review potential changes with collaborators - Easily add follow-up commits before changes are merged --- # ⏱ Create a pull request 1. Open a pull request from your new branch 1. Merge the pull request on GitHub 1. Pull the revised `main` branch to your local repo --- # Forking <img src="https://media.giphy.com/media/U4XP86hPyfy29A9tbl/giphy.gif" style="display: block; margin: auto;" /> --- # Forking <img src="img/fork-and-clone.png" width="1280" style="display: block; margin: auto;" /> .footnote[Source: [Happy Git with R](https://happygitwithr.com/fork-and-clone.html)] --- # Forking ```r usethis::create_from_github("OWNER/REPO") ``` - Or interactively on GitHub --- # ⏱ Forking 1. Create a fork of [`css-skills/practice-repo`](https://github.com/css-skills/practice-repo) 1. Stage, commit, push a change to GitHub 1. Open a pull request to the original repo --- # Other useful GitHub features - Issues - Private repos - [Free GitHub Pro while you are a student](https://education.github.com/discount_requests/student_application) --- # Other useful `usethis` functions - [Pull request helpers](https://usethis.r-lib.org/articles/pr-functions.html) - [All the `usethis` git functions](https://usethis.r-lib.org/reference/index.html#git-and-github) --- # Don't use RStudio much? Not required for Git! -- - Pick a [Git client](https://git-scm.com/downloads/guis) (or do it all in the shell!) - Check if your IDE has Git integration - Use a [project-oriented workflow](https://rstats.wtf/project-oriented-workflow.html) --- # When in doubt... <img src="https://imgs.xkcd.com/comics/git.png" style="display: block; margin: auto;" /> .footnote[Source: [xkcd](https://xkcd.com/1597/)]