Submind YouTube summaries
Thumbnail for Getting started with Git and GitHub [PythonLiveLessons #3]

Getting started with Git and GitHub [PythonLiveLessons #3]

Watch on YouTube

Video summary

In this episode of Python Live Lessons, Sebastian Mateo introduces viewers to Git and GitHub as essential tools for version control and code management. The main subject is transitioning a local Python project called Markdown Code Exec from a solitary development environment into a shared repository on the cloud. To achieve this, the presenter explains that Git acts as a logging system that tracks every change made to source code, allowing developers to freeze specific versions of their work through "commits." GitHub serves as the remote storage location, similar to Google Drive or Dropbox, where these committed changes are uploaded via the "push" command. This setup ensures that code is backed up and its history is preserved, which is crucial for maintaining a reliable project over time. The tutorial then delves into the specific workflow required to connect a local computer to a GitHub repository. The process begins by initializing an empty Git repository on the local machine using the `git init` command, followed by linking it to the remote cloud storage with `git remote add origin`. To streamline this process and make the interface more readable, Sebastian demonstrates using the Git GUI tool, which visually displays unstaged changes versus files that have already been committed. A significant part of the demonstration involves creating a `.gitignore` file to exclude unnecessary artifacts like Python bytecode (`.pyc`) files and temporary editor sketches from being tracked. This ensures that only relevant source code and documentation are uploaded, keeping the repository clean and focused on the actual project logic. Finally, the episode covers the creation of a README.md file to document the project publicly on GitHub. The presenter emphasizes that a README is vital for providing context, describing how to use the library, and specifying licensing information, such as the GNU General Public License in this example. By adding this documentation file and committing it alongside the code, the project becomes fully documented and ready for others to view or contribute to. The session concludes with a successful push of all changes to the GitHub server, resulting in a live-updated repository that showcases the basic workflow for managing personal projects. While the presenter notes that collaboration with multiple people introduces additional complexity involving forks and pull requests, this episode focuses on mastering the fundamental steps of initializing, ignoring unwanted files, documenting, and pushing code to the cloud.
Read the full video transcript
to join a live broadcast of python live lessons tune in to my youtube channel sebastia amato every tuesday 10 p.m new delhi time 6 pm central european time and 9 00 am los angeles time hope to see you then and for now enjoy this episode [Music] hi welcome to episode three the fourth episode of python live lessons welcome back my name is sebastian mateau as always i would like to engage everyone who is watching to ask questions in the chat this is a live episode and i uh the main reason to do it live rather than just post videos is of course to have some to have a little bit of interaction even though it's just through the chat so welcome um what we're going to cover today is uh working with git and github um but so that's a bit of a different topic than we covered in previous episodes but nevertheless it's uh we're going to pick up where we left off during after the previous three episodes so just to refresh your mind we have been working for the past three episodes on a code project called markdown goat exact what does it do doesn't really matter that much for the purpose of this this episode hi everyone but basically it is a it is a python library that takes a piece of markdown code we mark down text so what is marked down it's just plain text and it's often used to document source code and in the markdown text file there is quite often some example code right imagine that you have a readme that explains how you should use a python library that could be written in markdown and there would be sort of some example code in there and what our project does markdown code exec it actually executes that short scout in the markdown file captures the output that is generated and then includes the output in the markdown take a look at the previous episodes if you're interested but basically it's a way of compiling markdown text files so that the code that is actually shown in the in the text files is actually executed so um what we've been doing so far is create a code sketch so the code sketch is the sort of a working rough unpolished version of the program and then we turned the code sketch into a python package so the stage where we are now right now is that we have a nice python package with the that you can import and that has hello everyone and that has functions that you can execute and it does and it behaves as a python package should but right now i'm being very unsafe by essentially having that package only on my own computer and it's not backed up and i'm not using a versioning system to actually keep track of the source code so right now it's a perfect moment to actually create a github repository for this project so that's what we're going to do we're going to create a github repository and basically upload the code to github um but let's start with a little bit of git and github 101 because git is pretty complex and there's a lot of terminology involved so it's important to kind of get that terminology straight and don't expect if you're unfamiliar with git to ex to explain or to understand everything at once i mean i've been using git for years and i still quite often am confused but i'll try to explain it as best as i can and just to kind of put you at ease in most even though git can be very complicated in most cases using you only use like a very small subset of git functionality and that's quite doable actually and already really useful all right but i've been very abstract let's make it a little bit more concrete with the schematic and as i said don't hesitate to ask questions so git is a versioning tool what does it mean it means that it sort of keeps track of all the changes to your source code mostly that's what it does it basically keeps a log a backlog of everything that's changed in the code where does the code live mostly it just lives on your own computer right you have some files and you have some folders and that is where your python folder files are that's your python project it's just on your own computer then what you will do if you want to work with git and github usually you can also i'm going to talk about github here as though it is sort of the only way to use git it is not i know that but just to keep things simple let's assume that you're going to use github here then you create a github repository and the github repository is kind of like a google drive in the sense that it will create will contain a copy of your source code or a dropbox if you will now you're sort of happily coding on your computer and you're continuously changing stuff right about the source code you're you're editing your files you're adding new functionality etc now every time that you've made a meaningful change in git terminology you commit the change that basically means that you sort of say okay i'm going to give this change in name um and i'm going to actually freeze it and make it part of my source code that is committing in git language and that means that at the end of the day your your computer pro your source code project is no longer just a bunch of files but it's it is a bunch of files plus sort of a history of commits that that explains sort of and shows how the files changed over time so that's very useful sort of a history now and then at some point you're ready to actually upload your project to github to the cloud and you do that with the so-called git push command so we've seen two get commands now committing is freezing sort of keeping track of changes on your computer and pushing is uploading stuff from your own computer upwards to the cloud to github that's what it means and so it is kind of the synchronization actions let's say um so yeah so in many cases and also the situation that we're going to uh discuss today this is all there is to it basically you're working by yourself on your source code project it's kind of a solitary activity and you're just basically committing stuff and pushing it up to the cloud and that's all there is to it it's very simple use case of git but already really useful um but now let's imagine that you're actually collaborating with multiple people on the same code project how what does that look like then things become really complex really quickly but i'll explain it to you and keep in mind that this actually what i'm showing here the simple schematic is already what covers most of the workflow in real life but let's say you're working together with someone else then suddenly you have four places where the code lives you have your computer you have the computer of that other person you have your github repository and you have the github repository of that other person and how do they relate to each other well the action the first first step that the other person needs to take is of course to create a copy of your github repository right to work on so generally speaking that person doesn't work directly in your github repository but rather creates a copy and the action of creating that copy is called the fork the git fork then that person has a github repository but needs to download it of course to their own computer and for that there is the clone action so clone git clone basically means download all the stuff from a repository to my own computer now then the person starts making changes and committing stuff etc um and every time that the commit is that that a meaningful change has been made that person does a git commit um and um and as such right changes accumulate to the source code now once that person is kind of happy he or she also does a git push action to sort of upload the changes from their own computer to their github repository and you see how now we have two github repositories that are a little bit different right you have your github repository that has the original code and the github repository of that other person that has the original code plus some more commits now how do you then sort of synchronize those things the first step is that that person sent you a pull request you see how the schematic starts to become complex now right and a pull request is really literally a request it is sort of telling you okay i made some changes to your code do you want to take them over from me and then you can say yes or no to that to that request if you say yes then what you do is you sort of go diagonally like this and you download you pull as it is called the git pull all the changes from that other person's github repository and you pull it to your own computer that is the getpull action and pulling is actually sort of a composite action of two low-level git actions called fetch and merge so basically you can say git pull or you can say git fetch return git merge return so this is a pretty complex schematic right so but that's kind of how how the relationship between repositories is right so pushing refers to the action of moving stuff up up in the cloud pooling refers to downloading stuff from the cloud and committing refers to keeping track of changes that you make to your code that's those are the basic the basic the basic actions to know um so i hope that's clear if not and don't hesitate to shoot i'm trying to monitor the chat here um so far i'm just seeing highs so i'm hoping that it's clear um so let's take actually a look let's first actually take a look at what the source code does just uh just to kind of remind you let's remove this before we're going to upload it to git so the goal basically we have here we have a markdown file and it is just text right and embedded in the markdown file there are pieces of code like this wait i should actually switch my up like this up there we go um you should there are sort of pieces of code embedded in here and what should happen is that those pieces of code should basically be included below or the output from this code should be included here below um so the the the source code should be executed basically how does that work just to remind you how our lovely markdown code exec module work works this by the way what you're seeing now is basically me in a series of episodes making a python module that is that i'm actually going to use right this is a meaningful python module and you're just basically seeing me developing code as i also would normally in for other projects so um it works i can just say from markdown code exec import parse file that's how we programmed it go back to oh um code exec why doesn't it work oh wait i need to change the directory sorry working directory parse file and then i can say parse file demo dot md which is this file that you're seeing here and then i say okay demo oh out dot md so you see it's a very user friendly module that we're making and then what it does is basically it compiles it takes this this file up let's put them side by side demo out here up and you see that here we have this python capture thing and then basically below that we we insert the output of that of that of those commands that's what our module does um so but that's not really why we're here why we're here is because we want to see how uh how we can actually upload this to github so let's switch to a browser because github is after all a website up close the slides so here we have github and github.com you can create a free account there and then you have some kind of dashboard here you see i have a lot of stuff on my dashboard and related to stuff that i do on github but what i want to do now is basically click here on the plus but button and then i can choose new repository and i'm going to create a new repository and that is where on my github account markdown code exec is going to live what is the repository name markdown underscore go to exact is available right you are not allowed to have names that don't exist a description um execute python executes python code embedded in markdown files um then you can indicate some things public do you want everyone to be able to see your github project or do you want it to be private let's just make it public you can initialize your repository then github will already put some stuff in there for you i won't do that i don't think that's very useful but you can do it um and then i just say create repository it's easy as easy as that i'll zoom in a little bit actually yeah maybe even more yeah now then actually github already the repository is empty now right we have stuff on my computer but we haven't yet uploaded it to github so first um github is giving you some instructions on how to actually connect your code project to to a github repository and it is giving you some steps it's saying okay you should first run the get init command on your own computer to indicate that you want to have a github repository you want to make some commits and then you want to [Music] add a remote this sounds very abstract but i'll explain all the steps to you in more detail and then you want to push we've already learned what push is right okay let me just show you basically how you very easily can upload your stuff to a github repository and then it will become not so magical up here i have a terminal and this is the terminal that i opened in the in the in the folder for the markdown code exact project and right now there is no git repository on my own computer right i have to create it i have to initialize it by saying git space init okay um and then it says it gives you some suggestions but it also says okay i initialized an empty git repository so far so good then we need to say okay this this git repository that lives on my own computer needs to be connected to the git repository that lives actually in uh on on github how do we do that well i do i just typed here this slash so that i can continue typing on the next line otherwise it's a bit messy i say git remote git remote refers to the stuff that is living somewhere in the cloud git remote add i want to add a new remote so i want to basically add my github repository as a remote to my own computer what is it a name origin why origin well by consensus you you call these things a you call these things origin that is just you can do any name right you can do it bunny you can do you know your favorite color but by consensus people used to name origin for their own github repository and then here we need to put some kind of address where the origin actually is now what is that address for that i switch back to github and github actually gives me the address here right that's it that's the address of the github repository sorry copy it i switch back up i say bom git remote at origin now um so say then what we want to do now we basically have an empty git repository and we have connected it to github but we haven't done much else and then for the next step i'm actually going to use a graphical tool called get gui because it makes it a little bit more clearer what is happening here there are by the way many different tools to interface with git there's the github desktop application which is pretty fancy there is good cracking there is there's just a command line get i happen to use quite often get gui i think it's quite nice but regardless of what software you use to interact with git the commands and the logic is always exactly the same it just is sometimes a little bit shielded for from you if you use get get the desktop get a github desktop but the idea is the same all right i launched git gui it's a bit small actually really for you right that's annoying i didn't really think that through um so what you're seeing here though is can i can i say options and make the font size much bigger no i don't yeah okay i think i can yes all right up okay save all right even bigger right options okay i'm happy that i was able to do this on the fly all right so this is pretty 80-ish 80-ish font right but at least we can read it okay so what we have here now is an overview of the git repository and here in this red box that you're seeing here are the so-called unstaged changes so those are things that get that seem to be sort of new or changed and gets but we're not really doing anything with it what we want to do then is actually look at it and select those changes that we're interested in so it is not automatically picking up changes you have to sort of manually do that you see also that there are a bunch of things that we're not really interested in things like dot by c that is python python byte code and we actually don't really want that to show up there so um what can we do let's first actually clear things up a little bit just so that we're only seeing things in here that we're interested in and for that i'm again going to switch back to the text editor this time i'm opening here this is the folder in the text editor the folder that contains the git repository and i'm going to create a special file that is called git ignore and in git ignore you can indicate the stuff that git should ignore star dot pi c that's not really we we don't care about python byte code so everything that is the extension dot by c should be ignored we also don't really want to have the sketch.pi file we made that but we're not really interested in it and we also don't really want to have to get ignore file itself in the git repository so that's what we're ignoring and then if i switch back to git and i see this and i press rescan this button button here you see that now suddenly a lot of the stuff has disappeared why has it disappeared well because the stuff that actually matches the contents of the git ignore is not shown here okay and now the next thing that we want to do is say okay let's click on this and let's click on this then we're cherry picking those two files here on the right hand side you see basically what what has changed in those files and for that actually let me make the phone bigger again because otherwise you can't see up go thick this is a horrible fault actually can we do better gothic well i'm i'll have to know let's just have this 80s font all right um so and then here in the init file you see basically it's just one line and here ever there's a whole bunch of lines and they're all new those are the changes here in the initial in the commit message i can say okay i'm going to have a description here and i'm going to say add markdown code exact package and then i click on the commit button and as soon as i do that i have sort of finalized this change i've added the commit that's what it would mean we also have the demo i think it also makes sense to actually have the demo file so i also click on the demo it also goes to the stage changes and i say okay add demo build md and i click on commit i actually don't think we need to demo out so let me go switch back up and demo out i also add that to the git ignore save it rescan and now we have a completely empty uh git gui interface um usually okay i see a few questions actually now we have a completely empty git git interface meaning that everything is either nicely in the git ignore or it is actually already committed and then morpheus says usually you do want to commit dot git ignore so the same files are ignored for all developers working and set working with the repository and set stencil makes the same point actually without uploading the get ignore others who try and contribute will have their own or what's not the same point but related will have to create their own correct get ignored locally yeah so this basically there is a difference i guess of taste some people indeed add the dot get ignore to the project um and then the advantage of that is indeed that everyone automatically gets that git ignore and also that all developers working on the same project as morpheus is saying have the same dot git ignore etc and that can be convenient i personally don't like it so much because in my experience different developers use different tools and they end up with different kinds of temporary files etc and it's nice to give everyone the flexibility i think to have their own.kit ignore file but it's not unreasonable actually to commit the git ignore file either it's a bit a matter of taste but i never do it in any case um so all right now let's actually switch back to the terminal up well that's close git ignore switch back to the terminal and so now we have made some changes to the to the source code locally and but it's not yet on on github but if i now say git push and i run enter oh git push and i need to specify where actually origin master origin is the remote and master is the branch on the remote i'm not really touching upon that here and a message that you get here from github or from git is that well you didn't specify a a where you want to push but you can also set a default by running this command right now i'm not setting a default i'm just being explicit and i'm running it here git push origin master and if i now go back to github and i reload the page you see that suddenly now we have very nicely here a um a the demo.md and a markdown code exec and morpheus is making a point that seems kind of important you can have both dot git ignore in the repository with files that should be ignored everywhere and a global.get ignore on every developer system where they can add their own file patterns is is that possible i genuinely didn't know that actually maybe you can explain in the chat how that works and actually morpheus do you create do you give that another name or so i didn't know that if that's the case that would make it more reasonable actually to commit to git ignore i agree okay so now basically just to switch back to our github repository it's already looking kind of nice but it's it's missing something actually and github is saying that also is missing a readme and it's really quite important to have a readme why well because it specifies the license information for one thing usually and also it tells you a little bit about the source code so let's as a last step go actually back to our repository here we're going to say up file and then i say readme.md so the readme.md is actually going to be is a markdown file and it's going to document markdown code exec um and um so i think morpheus has blown everyone's mind by the concept of having a separate global.git ignore including mine actually so markdown code exec um then some description i say execute python code code executes python code in markdown files files and captures the output this guy for description i put it make it italic like this i say copyright 2022. that's me of let's make it so better license i'm you could do this nicer actually really um [Music] but i'm just going to say for now because i don't really have the the information handy to do this really nicely i'm just going to say uh say that this is uh the gnu general public license since three normally you would indicate a sort of a more concrete blurb here and also include a file with the license information in the code repository i will i will do that later but i think for this for the purpose of illustrating that it's not so necessary and i will also say okay um example usage and then i will say okay i will add some in the markdown file i would say okay to actually use python markdown code exec you do from markdown code exec exec import parse file and then parse oops parch file input dot md output dot md up dot python capture all right in previous episodes we've seen how that basically means that this is python source code and we want to capture the output okay i saved this readme file and now i split i switch back and morpheus says the setting for the user's global.get ignore path is chordal excludes file i guess so you can do it in the git config settings apparently all right cool um all right i'm going to start get gooey again now we suddenly have one extra file that appears to read me yep i click on the readme um it says new file mode why doesn't it actually have any rescan ah here it is okay and read me i commit and instead of typing git push in the terminal i can also just click here push button up push to master it's the exact same thing as doing git push in the terminal or doing something equivalent in the get desktop file etc up i get a nice green box saying that it was successful i switched to github and actually if you're watching this live you can actually see now if you go to github as matteo markdown code exec here it is right here's the code execute the the github project all right um so this is uh this is a very basic introduction to git that i've given you right here there's much more to it right basically this is just the workflow from for you working on your own computer uploading stuff but you can also collaborate with other people if there is an interest in that maybe in future episodes i will go into more detail when it comes to using git but it becomes really complicated really quickly so i'm not sure there's a lot of interest in that um other than that um i this is all i had to share with you for this episode for the for the people who are watching the recording afterwards this is where the episode ends so thank you very much for uh for watching episode three the fourth episode of bite and life lessons for the people who are watching through the chat live feel free to ask some some questions still [Music] you