Mastering git Lesson 3 (working in teams) Irina Gulina Tomas Tomecek Schedule change 2 No class on October 25th, but on November 1st? The first branch is created with the first commit 3 Questions Any questions or suggestions? 4 Questions ▸ What commands do you use to create a new branch? ▸ Describe a merge commit: what is it and why do we have such a thing? ▸ What is fast-forward? We do have questions! 5 Git Basic Commands ▸ help ▸ init ▸ clone ▸ config 6 ▸ add ▸ status ▸ diff ▸ commit ▸ reset ▸ mv ▸ rm ▸ branch ▸ switch ▸ checkout ▸ merge ▸ log ▸ stash ▸ rebase ▸ restore ▸ fetch ▸ pull ▸ push ▸ remote ▸ Working as a team with a git repository ▸ Deep dive into remotes. ▸ What’s upstream and a fork? ▸ How to work with multiple remote repositories? ▸ Moving changes between remote repositories: push, pull and fetch ▸ How can I check out someone else’s changes locally? ▸ The golden rule of push ▸ Tracking remote repositories. ▸ Labs: be comfortable with creating pull requests Today’s class 7 8 Git workflow Git workflow Remote 9 How to contribute to a team/community repo? FORK IT10 11 ● Click Fork in a team repo ● Go to your personal Git space ● Git clone to your local PC ● Add upstream How to fork git remote add upstream https://github.com/upstream/project.git $ git config --list | grep remote remote.origin.url=git@github.com:TomasTomecek/ansible-language-server.git remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.main.remote=origin remote.upstream.url=https://github.com/ansible/ansible-language-server.git remote.upstream.fetch=+refs/heads/*:refs/remotes/upstream/* 12 Remote URLs Git remote git remote + (git fetch + git merge) or git pull + git push How to work with a remote, syncing changes 13 Git remote Create, view and delete remote connections in ./.git/config git remote git remote -v git remote show git remote add git remote rm git remote rename git remote set-url git remote 14 Git remote $ cd grit $ git remote -v bakkdoor https://github.com/bakkdoor/grit (fetch) bakkdoor https://github.com/bakkdoor/grit (push) cho45 https://github.com/cho45/grit (fetch) cho45 https://github.com/cho45/grit (push) defunkt https://github.com/defunkt/grit (fetch) defunkt https://github.com/defunkt/grit (push) koke git://github.com/koke/grit.git (fetch) koke git://github.com/koke/grit.git (push) origin git@github.com:mojombo/grit.git (fetch) origin git@github.com:mojombo/grit.git (push) git remote -v 15 Git remote * remote origin Fetch: origin http://someurl_git/application Push: origin http://someurl_git/application HEAD branch: main Remote branches: Branch1 tracked develop tracked Branch2 tracked Local branches configured for 'git pull': develop merges with remote develop main merges with remote main Local refs configured for 'git push': develop pushes to develop (local out of date) main pushes to main (up to date) git remote show origin 16 Git push Write to remote connections defined in ./.git/config git push git push 17 Git fetch Download commits, files and refs from remote connections defined in ./.git/config. No merge! git fetch git fetch git fetch --all git fetch --dry-run git fetch 18 Git fetch Download commits, files and refs from remote connections defined in ./.git/config git remote + git fetch + git branch + git switch + git reset git fetch 19 Git push Upload local repository content to a remote repository git push refspec = [:] git push 20 Golden rule of push You can force push on only to ▸ Local branch which is not upstream yet ▸ Unmerged PR You can’t force push to upstream public branches. git push --force 21 Git pull fetch and download content from a remote repository and immediately update the local repository to match that content, i.e. git pull = git fetch + git merge git pull = git fetch && git merge origin/ git pull --no-commit git pull --verbose git pull --rebase git pull 22 Git pull Before -> git pull -> After 23 Git pull Before -> git pull --rebase -> After 24 Fetch vs Pull git fetch vs git pull 25 26 Git workflow Git workflow Remote PR A user-friendly way for discussing changes before integrating them into the official project. PR/MR opening -> PR/MR notification -> CI/CD -> Discussion -> Follow-up Commits -> Merge Pull/Merge request 27 PR ● Share changes ● Get review and feedback ● Encourage quality ● Get better at coding Why do we use PR/MR workflow? 28 PR PR/MR opening -> PR/MR notification -> CI/CD -> Discussion -> Follow-up Commits -> Merge ● Create a dedicated branch in a local repo with the code change ● Push that branch to the remote ● File a PR ● Review a PR ● Merge a PR PR/MR workflow (simplified version) 29 30 ● When you push from your local to your origin, every time it will ask you to create a MR/PR Creating a PR 31 ● When you push from your local to your origin, every time it will ask you to create a MR/PR Creating a PR 32 ● When you push from your local to your origin, every time it will ask you to create a MR/PR Creating a PR 33 ● When you push from your local to your origin, every time it will ask you to create a MR/PR Creating a PR 34 ● Complete piece of work about ONE logical change ● Adds value in some way ● Solid title and body ● Clear commit history ● Small ● Meets project’s contribution guidelines What constitutes a good PR? 35 ● Clarity - “main” is not descriptive ○ “fix-1338” tells a better picture what’s inside ● You cannot have 2 pull requests created from the same branch at the same time ● You can work in parallel on 2 branches at the same time ● git switch -c my-feature-branch ○ is just one extra command for all 3 benefits Creating a PR from main vs. a feature branch THE END Test your knowledge now! 36 Default branch ▸ https://gitlab.com/redhat/research/mastering-git/-/blob/main/labs/lab3.md Class 3 lab 37 THE END Questions? 38 HW2 ▸ https://gitlab.com/redhat/research/mastering-git#class-3-homework Class 3 homework 39 THE END Questions? 40 THE END THANK YOU! 41