class: left, middle # From Zero to your first PR with Git and Github ### Cesar Aguirre _Software engineer, lifelong learner_ --- ## Why? _Have you ever done something like this?_ ```bash > ls Project-2020-04-01/ Project-2020-04-02/ Project-2020-04-03/ Project-Final/ Project-Final-Revised/ Project-Final-Final/ Project-Final-Final-Final/ Project-ThisIsNOTTheFinal/ Project-ThisIsTheFinal/ ``` You’re doing it wrong! --- ## Version Control System From the book [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control): _Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later._ It allows to * revert a file or an entire project to a previous state * compare changes of a file over time * see who modified something --- ## Version Control System: Analogy  .footnote[Source: [Wikipedia](https://en.wikipedia.org/wiki/Back_to_the_Future)] --- ## Centralized vs distributed ### Centralized
.footnote[Source: [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)] --- ## Centralized vs distributed ### Distributed
.footnote[Source: [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)] --- ## Git: what is it?
* distributed version control system * fast and lightweight * free and open source * flexible branch model * developed by Linus Torvalds for the Linux kernel * the most commonly used version control system today .footnote[Sources: [Wikipedia](https://en.wikipedia.org/wiki/Git), [About Git](https://git-scm.com/about/branching-and-merging), [What is Git](https://docs.microsoft.com/en-us/azure/devops/learn/git/what-is-git)] --- ## How to install Head to [Installing Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) On MacOs ```bash $ brew install git ``` On Windows with [Chocolatey](https://chocolatey.org/) ```powershell > choco install git.install ``` On Ubuntu ```bash $ sudo apt-get update $ sudo apt-get install git ``` --- ## First-time setup ### Your identity ```bash $ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com ``` .footnote[Source: [First-Time Git Setup](https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup)] --- ## How to start ### Create a new repo locally ```bash $ git init ``` ### Clone an existing repo ```bash $ git clone
``` For example, `$ git clone https://github.com/canro91/BaseXml` --- ## Saving changes 1. Create a file ```bash $ echo 'This is a demo of Git basic concepts' >> README.md ``` 2. Save your changes ```bash $ git add README.md $ git commit -m 'Add README file' ``` --- ## Working dir/staging area/commit
.footnote[Source: [Pro Git](https://git-scm.com/about/staging-area)] --- ## Ignore files There are files you don't want to track. For example: log files, files generated after building, folders generated by your IDE Create a `.gitignore` file ```bash [Bb]in/ [Oo]bj/ [Ll]og/ [Ll]ogs/ ``` .footnote[[A collection of templates](https://github.com/github/gitignore), [More Ignore patterns](https://www.atlassian.com/git/tutorials/saving-changes/gitignore#ignoring-a-previously-committed)] --- ## Commit message style guide _Are you commiting with messages like "Uploading changes"?_ A commit message should answer: * Why is this change necessary? * How does it address the issue? * What side effects does this change have? .footnote[Source: [5 Useful Tips For A Better Commit Message](https://thoughtbot.com/blog/5-useful-tips-for-a-better-commit-message)] --- ## Branches _Create an alternate timeline to work without messing with the main line_ The default branch name is `master` 1. Create a branch ```bash $ git branch testing ``` 2. Move to the new branch ```bash $ git checkout testing ``` Alternatively, you can combine these two commands into a single one ```bash $ git checkout -b testing ``` --- ## Merging 1. Move to the desired branch ```bash $ git checkout master ``` 2. Merge a branch ```bash $ git merge testing ``` 3. Delete a merged branch ```bash $ git branch -d testing ``` --- ## Merge and conflicts If you modified the same parts of a file in different branches, Git won't be able to merge them cleanly. *Now you have a conflict.* Git uses markers to delimit conflicted areas. ```bash <<<<<<< HEAD Some content ======= Another content >>>>>>> branch ``` --- ## Gitflow
.footnote[Source: [A successful Git branching model](https://nvie.com/posts/a-successful-git-branching-model/)] --- ## Sync _What if you want your code to have a home outside your computer?_ You need a "remote". For example: GitHub, Gitlab and Bitbucket. 1. Add a remote ```bash $ git remote add
``` 2. Upload your changes ```bash $ git push
``` For example, `$ git remote add origin https://github.com/canro91/GitDemo.git` --- ## Github  Github is a hosting solution for Git, recently acquired by Microsoft. It's kind of a social network * Unlimited private repositories * Bug tracking * Task management * Wikis * Follow developers * Star projects * Be part of communities _Some people use their Github profile as CV_ .footnote[Sources: [Wikipedia](https://en.wikipedia.org/wiki/GitHub), [Your new resume](http://blog.gainlo.co/index.php/2015/11/13/how-to-make-github-as-your-new-resume/)] --- ## Pull Request * A pull request is a request to include some changes from my fork into the original repo * Fork means create an identical copy of a repo and become the owner --- ## Cheatsheet * `git init` * `git clone
` * `git status` * `git add
` * `git commit -m 'A commit message'` * `git log --oneline --graph` * `git branch
` * `git checkout
` * `git merge
` * `git remote add
` * `git push origin
` * `git pull origin
` .footnote[More cheatsheets: [here](https://www.git-tower.com/blog/git-cheat-sheet/) and [here](https://education.github.com/git-cheat-sheet-education.pdf)] --- ## Let's Practice * Install Git locally * Create a Github account * Open your first PR Head to [firstcontributions/first-contributions](https://github.com/firstcontributions/first-contributions) and follow the instructions to open your first PR using either the command line or your favorite IDE. --- ## Want to know more? * [C# Definitive Guide](https://canro91.github.io/2018/11/17/TheC-DefinitiveGuide/) Section about Git and Github * [Git Guide for TFS Users](https://canro91.github.io/2019/11/11/GitGuideForTfsUsers/) If it happens you know TFS * [Flight rules for Git](https://github.com/k88hudson/git-flight-rules) In case of panic * [Pro Git](https://git-scm.com/book/en/v2) An entire book on the subject --- class: center, middle # That's all folks! ## Cesar Aguirre https://canro91.github.io/ https://github.com/canro91