Mastering git Lesson 2 Irina Gulina Tomas Tomecek Questions Recap Lab 1 2 Today’s class ▸ Slides 41 and 52 from Lecture #1 ・ Create a local repository ・ Make it a version controlled ・ Create a file with some content. What will show git status? ・ Notify git about that new file ・ Save your changes in a repository ▸ Share your local repository publicly ・ Create an origin ・ How to connect your origin with a local? What protocol? Why? ・ Sync origin and local. How? Lab 1: Local => Origin 3 Today’s class ▸ Create an origin repository in UI ▸ Create a local version of it. How? ▸ Difference between init and clone Lab 1: Origin => Local 4 Questions Any questions or suggestions? 5 Questions We have questions! 6 Today’s class ▸ What is a staging area? ▸ In what 4 states can a file be when running git status? ▸ What does git commit do? Questions 7 Today’s class ▸ How does branching work in git ▸ Best practices for branching ▸ Git tags: how to use them and what’s their use case? ▸ Stash: put your current work on a shelf and restore it later. ▸ Labs: branching!! ▸ Homework 1 assignment Today’s class 8 Git Branching 9 Branching More info: https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell What branch is? 10 main Why branching ▸ Work in parallel ▸ Keep main branch free from questionable code ▸ Experiment easily Why do we need branches? 11 First branch Git version 2.41.0 What is the first branch? 12 Branching ● Default branch ● Create a new branch ● List branches ● Switch branches ● Work in parallel on different branches ● Merge branches ● Delete a branch ● Rename a branch ● *Stash changes and tags ● Push and pull a branch to a remote server Branching operations 13 Create a branch $ git branch testing Create a branch 14 main List branches $ git branch <--list> $ git branch -v $ git branch -vv $ git branch -a List branches 15 main Switch branches $ git switch testing Switch branches 16 Switch branches $ git switch -c testing Create and switch 17 Switch vs Checkout $ git switch = git checkout $ git switch -c = git checkout -b git-checkout - Switch branches or restore working tree files git-restore - Restore working tree files git-switch - Switch branches Switch vs Checkout 18 Head HEAD 19 .git/refs/heads/ .git/HEAD Work in parallel $ touch file.txt $ git commit -a -m “add file.txt” Work in parallel 20 Work in parallel $ git switch main Work in parallel 21 Work in parallel $ touch file2.txt $ git commit -a -m “add file2.txt” Work in parallel 22 Rename branches $ git branch -m Rename a branch 23 Merge branches $ git switch main $ git status $ git fetch $ git pull $ git merge hotfix Merge branches 24 main Merge branches $ git switch main $ git status $ git fetch $ git pull $ git merge hotfix Merge branches (ff) 25 main Merge branches $ git switch iss53 $ vi index.html $ git commit a -m “fix link [issue 53]” Merge branches 26 main Merge branches $ git switch main $ git status $ git merge iss53 Merge branches (merge commit) 27 main Merge branches Merge branches 28 Delete branches $ git branch -d testing $ git branch -D testing $ git push origin --delete testing $ git push origin :testing Delete a branch 29 Stash Stash 30 Default branch A special “branch” git has high-level operations to work with it Handy to put things on side git stash 31 Default branch git stash list git stash show git stash [push] git stash apply vs. git stash pop stash@{2} git log stash git stash (commands) 32 THE END Tags 33 Default branch Points to a commit and doesn’t change as you commit more Used mainly to track releases and deployments Lightweight vs. Annotated Push/pull tags git tag 34 THE END Test your knowledge now! 35 Default branch ▸ Fork this repo and clone it: https://gitlab.com/redhat/research/mastering-git/ ▸ Create a branch ▸ Switch to that branch ▸ List branches ▸ Create another branch ▸ Switch to it and create a commit ▸ Switch to the previous branch and merge the last branch into it ▸ Delete the previous branch ▸ Special task: create a new branch and merge it into the first branch in a way so it’s not fast-forward: there is a merge commit Task 36 THE END Questions? 37 HW https://gitlab.com/redhat/research/mastering-git#class-2-homework All homework info will be in README.md Class 2 homework 38 Bonus Task Make a contribution to an Open Source project. ▸ Not an University Project ▸ Not owned by you ▸ MR/PR doesn’t need to be merged by the Task/Course deadline ▸ A change can be of any content (not necessary code, it can be docs for example), but it must be meaningful, positive ▸ See details in “Mastering git” Readme First Timers Only 10 C++ open source projects welcoming contributions Contributions-welcome topics on GitHub Hacktoberfest - 10th anniversary. Check on participation info Bonus Task 39 THE END THANK YOU! 40