Mastering git Lesson 1 Irina Gulina Tomas Tomecek XKCD Source: https://explainxkcd.com/wiki/index.php/1597:_Git Intro Who are the lectors? 2 Who are the lectors? Tomas Tomecek Sr. Principal Software Engineer Field: Automation, Python, Infrastructure, AI Interests: Mushrooms obviously 😁, hiking, gardening, snowboarding ttomecek@redhat.com tomas@tomecek.net 3 Who are the lectors? Irina Gulina Sr. Software Quality Engineer Field: RHEL + SAP + Cloud Interests: DIY, knitting, baking, via ferrata igulina@redhat.com 4 How did it start? ▸ Irina: Workshop "Git troubles: How to find, fix and avoid them", DevConf.CZ 2019 ▸ Irina: "OSSDev: Advanced Git", October 2019 ▸ Irina: Talk "Git Etiquette: Best Practices or Mind your Git Manners", Open House Red Hat, April 2020 ▸ Tomas + Irina: Workshop "If you do force push…. May the force stay with you", DevConf.CZ 2020 ▸ Tomas + Irina: “OSSDev: Advanced Git”, April 2021 ▸ Tomas + Irina: “OSSDev: Mind your Git manners”, March 2022 ▸ Tomas + Irina: “Mastering Git”, 6 weeks course in MUNI, winter 2022 How did it start? 5 All octocats illustrations come from: https://octodex.github.com/ Congrats 2nd iteration Git is easy to use, if you know a few basic concepts Focus on practical application These slides are NOT study materials (explained later) We don’t know MS Windows nor MacOS (containers/VMs) In 6 classes you’ll learn the essentials so you can be productive Tell us what you want to learn about Homeworks: 5 + bonus task (1st class doesn’t have a homework. Yupeee!) To pass the course: complete all 5HW, or 4HW + bonus Congratulations! 6 About the course Class objective 7 About the course Lesson 1 - Introduction of this course, organization, motivation, basics, commits Lesson 2 - How does branching work in git Lesson 3 - Working as a team with a git repository Lesson 4 - Fixing mistakes Lesson 5 - Git Etiquette Lesson 6 - Git features and common open source git workflow About this course 8 Today’s class ▸ Version Control and why should you care? ▸ Installing Git. ▸ The basics of Git Workflow. Cloning Repositories. ▸ Index. ▸ Art of commits. ▸ HTTPs and SSH. ▸ Lab: Installing Git. Configuring Git for local repositories. Securing your Git repo with SSH keys. Creating local repositories, adding files locally. ▸ Bonus: How to write a good README for your project. Today’s class 9 Distributed version control system for managing source code, i.e. it’s a system that provides three important capabilities: ▸ Reversibility ▸ Concurrency ▸ Annotation About git What is git? 10 Version control system is a system for managing the source code providing three important capabilities: ▸ Reversibility - the ability to back up to a saved, known-good state when you discover that some modification you did was a mistake or a bad idea. ▸ Concurrency - the ability to have many people modifying the same collection of code or documents knowing that conflicting modifications can be detected and resolved. ▸ Annotation - attaching explanatory comments about the intention behind each change to it and a record of who was responsible for each change. About git What is VCS? 11 ▸ Keep track of code history ▸ Collaborate on code as a team ▸ See who made which changes ▸ RECOVER VCS What problem does VCS solve? 12 VCS Eric Raymond’s “Understanding Version-Control Systems” History 13 Generation Networking Operations Concurrency Examples 1st None (Local) One file at a time Locks RCS, SCCS 2nd Centralized Multi-file Merge before commit CVS, SourceSafe, Subversion, Team Foundation Server 3rd Distributed Changesets Commit before merge Bazaar, Git, Mercurial VCS 14 A C B 15 VCS 16 Before Version Control VCS 17 Before Version Control ¯\_(ツ)_/¯ VCS Reference: https://en.wikipedia.org/wiki/Punched_tape This 1959 IBM 1620 relied on paper tape to store data and programs 18 Before Version Control VCS Reference https://www.reddit.com/r/ProgrammerHumor/comments/psqlij/chad_programmer/ 19 With VCS VCS Midjourney prompt: “a picture of women sitting in front of computer and collaborating on a software project” 20 With git About git man 1 git NAME git - the stupid content tracker DESCRIPTION Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals. 21 Source: https://octodex.github.com/ How old is git? 22 ▸ 11 August 1995 ▸ 22 December 1999 ▸ 14 April 2001 ▸ 7 April 2005 ▸ 13 June 2011 ▸ 5 February 2016 Source: https://octodex.github.com/ How old is git? 23 ▸ 11 August 1995 ▸ 22 December 1999 ▸ 14 April 2001 ▸ 7 April 2005 ▸ 13 June 2011 ▸ 5 February 2016 What is git? 24 Snapshots, not differences B A 25 Git workflow Git workflow Remote 26 Git workflow What happens locally? Remote Install git $ $package_manager $install_command git E.g: dnf install git 27 Install git locally (Linux) Install git https://gitforwindows.org/ 28 On Windows $ brew install git $ sudo port install git Or a binary package shipped with Xcode On Mac git Setup 29 git config git Setup Sets conf variables determining git behavior ▸ System ▸ Global ▸ Local 30 git config git Setup Sets conf variables determining git behavior ▸ System - all users and all repositories ▸ Global - current user and their repositories ▸ Local - specific repository 31 git config git Setup A System B Global C Local 32 git config 1. .git/config 2. [path]/etc/gitconfig 3. ~/.gitconfig or ~/.config/git/config git Setup ▸ System - all users and all repositories [path]/etc/gitconfig ▸ Global - current user and their repositories ~/.gitconfig or ~/.config/git/config ▸ Local - specific repository .git/config 33 git config git Setup Configure git with config command or directly editing the conf file. ● Identity ● Editor ● Commit ● Default branch name ● Merge tools ● Colored outputs (formatting and whitespace) ● Aliases34 $ git config git Setup Configure git with config command or directly editing the conf file. $ git config –-list <--show-origin> <--system|global|local> $ git config --global user.name "Mary Jane" $ git config --global user.email mjane@example.com $ git config --global init.defaultBranch main $ git config --global --unset user.name35 $ git config git Setup Configure git with config command or directly editing the conf file. $ git config core.editor $ vi ~/.gitconfig 36 $ git config git Setup If one git conf variable defined multiple times, which command will show the final say? For example, for a case of core.editor? 37 $ git config - Tricky question git Setup If one git conf variable defined multiple times, which command will show the final say? For example, for a case of core.editor? $ git config –-show-origin core.editor 38 $ git config - Tricky question HELP ▸ Google ▸ Stackoverflow ▸ $ git --help and git help , e.g. ・ $ git help config ・ $ git config –-help ▸ $ man git ▸ $ man git-, e.g. $ man git-config ▸ User manual: file:///usr/share/doc/git/user-manual.html ▸ Pro Git Book by Scott Chacon and Ben Straub: https://git-scm.com/book/en/v2 Documentation 39 XKCD Source: https://explainxkcd.com/wiki/index.php/1597:_Git Repository ▸ Create a new git repository locally ▸ Clone an existing repository 40 Git repository Local repository $ mkdir my_first_git_project $ cd my_first_git_project $ git init $ git init 41 Create a local git repository git init ▸ convert an existing, unversioned project to a Git repository ▸ initialize a new, empty repository creates a .git subdirectory in the current working directory with metadata 42 git init Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 43 Record changes to the repository Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 44 Record changes to the repository git status Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git $ git status <-s> 45 What does this command do? Create a file Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git $ echo ‘Mastering Git. First Lecture.’ > README.md 46 Create file Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 47 $ echo ‘Mastering Git. First Lecture’ > README.md Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 48 $ git add README.md Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 49 $ git commit -m “Initial commit” Local Repository Reference: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository 50 $ echo $'\nThis is a new line' >> README.md Local Repository 51 Local workflow Reference: https://stackoverflow.com/a/3690796/909579 Art of commit Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git $ echo ‘Mastering Git. First Lecture’ > README.md $ git status $ git add README.md $ git status $ git commit -m “Initial commit” 52 First commit 53 Commit ID $ git commit -m 'init commit' [main c6b75cd] init commit <...> $ openssl zlib -d < ./.git/objects/61/8c0a1b1ed51b1f7a3456fc135311331ce41dbf | sha1sum c6b75cd8d61e5be49fa4f23ee2025fed952e0aef - 54 Commit ID = SHA-1 hash Commit ID $ openssl zlib -d < ./.git/objects/61/8c0a1b1ed51b1f7a3456fc135311331ce41dbf | sha1sum c6b75cd8d61e5be49fa4f23ee2025fed952e0aef ▸ Full content ▸ The ID of the previous commit or its merge ▸ Commit and author date ▸ Committer and author’s name and email addresses ▸ Log message 55 Commit ID content Commit ID $ openssl zlib -d < ./.git/objects/61/8c0a1b1ed51b1f7a3456fc135311331ce41dbf | sha1sum c6b75cd8d61e5be49fa4f23ee2025fed952e0aef commit c6b75cd8d61e5be49fa4f23ee2025fed952e0aef parent 6294ij1k53w43uf2fd94168ac410eca1e98 er34q Author Irina Gulina 1659644787 -0700 committer Irina Gulina 1659644787 -0700 feature: my awesome feature 56 Commit ID content 57 Git workflow What happens locally? Remote 58 Git workflow Git workflow Remote Commit What VCS hosting platforms do you know? 59 git Setup ● Create a new repository on GitHub (remote origin) ● Add remote origin locally ● Push the changes to remote origin 60 Origin + Local git Setup ● Create a new repository on GitHub (remote origin) ● Add remote origin locally ・ $ git remote add origin git@github.com:/.git ● Push the changes to remote origin $ git branch -M main $ git push -u origin main 61 Origin + Local git clone $ exit from the previous repository (cd ..) $ git clone https://github.com/libgit2/pygit2 62 Clone someone’s git repository clone vs git init git clone is dependent on git init 63 Clone vs git init clone vs git init Do it yourself, connect locally cloned repo with its origin remote version… 64 Local + Origin remote HTTPS or SSH $ git rm $ git mv 65 Delete and move files Stage changes $ git add --all $ git add -A $ git add . 66 Add files Stage changes $ git commit -m 'repo cleanup' [main c6b75cd] repo cleanup <...> delete mode 100644 foo rename myfile.txt => roles/myfile.txt (100%) 67 Add files HTTPS or SSH HTTPS or SSH? 68 git clone $ git clone git@github.com:libgit2/pygit2.git 69 Clone someone’s git repository HTTPS or SSH Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ▸ Both are communication protocols. ▸ Both work for providing a reliable and secure connection ▸ Encrypted 70 HTTPS or SSH? HTTPS or SSH Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ▸ Smart - HTTPS ▸ username/password authentication ▸ Pros and Cons ● Fast, efficient, firewalled approved ● Writing username/password for authentication 71 HTTPS HTTPS or SSH Reference: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git ● Smarter ● Private key-based authentication ● Pros and Cons i. Fast, efficient, firewall approved ii. One-time association: your key in a git forge iii. Auth is mandatory 72 SSH Commit Do NOT use git:// and http:// 73 HTTPS or SSH ● gitlab.com account ● SSH key is in your account ● class repo is forked ● We have a UCO - GitLab mapping i. https://gitlab.com/redhat/research/mastering-gi t/-/issues/2 ii. https://gitlab.com/redhat/research/mastering-gi t/-/issues/3 74 No homework but requirements: Commit LAB 75 76 Bonus README How cool is Your README? 77 README Detailed description of a git project ▸ What is the project about ▸ What are the user cases ▸ How it is organized ▸ How to install and use it … and more 78 What is README? HTTPS or SSH ● the first impression about your project ● improve engagement with the project ● help others get involved 79 Why is README important? HTTPS or SSH ● It should exist! ● No unique guide. ● Fairly brief but detailed. ● Answer what, why, and the how of the project. ✓ TItle, description, demo, table of contents (optional), how to install and run the project, how to use the project - examples, user/pass requirements, credits, references, license (ref), badges (optional), howto contribute and project organization, TESTS, feedback, buy_me_coffee… 80 How does a good README look? HTTPS or SSH ● Check out big popular projects. Use templates and generators, e.g.: https://github.com/kefranabg/readme-md-generator ● Located at the top level of the project directory ● Keep it up-to-date ● Peek a language ● Format and structure it 81 How does a good README look? THE END Questions? 82 THE END We have Questions! 83 THE END THANK YOU! 84