A Git Tutorial Based On Examples (Code Along!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're looking at a piece of software known as git and git is very popular and i'm sure many of you already know what it is but it's essential to learn and use if you're working in a collaboration of people working on code i originally intended this video just to be for people in my research group i was going to teach git but i decided to also publish on my youtube channel as well so hello youtube the first thing we have to ask ourselves is what exactly is git well when you're working on code with people and you know one person's working on one thing one person's working on another thing the code is getting updated over time you want to keep track of the changes that are happening to the code you also want to make sure that there's no conflicts when people are working on code for example if two people are working on the same file and they each edit a bit of code differently and then they want to put that in the main file of all their code well there'll be a conflict because each person will have edited it differently the question is which one do you use and these are all issues that you run into when working with a team of people git is a piece of software that helps manage all those things essentially it's version control meaning if you have a file on your computer it tracks what does that file and all the contents of the file look like as a function of time so for example here's the file at one time the file gets updated all its contents are different at another time and then at another time and it sort of continues that way and the beautiful thing about git is that it tracks changes so efficiently that if you're at one you know state of the file structure where all the code is a certain way and whatever you can track backwards to get to earlier versions of what the file and all the code in the file look like you'll see what that means in a second when we get started so let's get to it so the first thing you need is to have downloaded git and i'll put a link in the description for how to download git but essentially you have a terminal like this and what you do is like you know there's files on your computer they're just regular files and what git does is you can turn a file into a git repository so that means git knows it will track all the files in that folder and what i mean by that is as follows so for example i can go to this is my directory on my computer and i'm going to go to the get tutorial folder here so cd get tutorial and now i'm in this folder here it's an empty folder now i'm going to make a folder inside this folder which i'm going to make the git repository so let's make a directory and i'm going to call it um git repo one so now i have a new directory here so i can cd get repo one and it's just an empty folder so it's a folder somewhere on my computer it's empty there's nothing special about it but i have git installed so if i go get i'm going to make this a little bit bigger so people can see so if i go get init what this is going to do is it's going to take that folder and it's going to become a special folder it's going to become part of a git repository so i go get in it it's initialized and empty git repository here so this means that all the changes that i make over time to files are going to be tracked we'll look in that a little bit more that's the special thing but we really like in essence that's sort of abstract what it's actually doing is if i look at the contents of this directory so i'm not sure the actual command for that but if i go to my folder and get repo one what it's actually doing what git is actually doing is it's creating this dot get file here and this contains all this information and this says given the folder which is a git repo and all the contents all the files and subfolders inside this folder how is that changing as a function of time i say time but what i really mean is like iterations and you'll see what that means and so this contains all the information for example i change all the stuff in the the folder over time and then this provides all the information to go back i can go to any sort of instant in time sort of to see what the contents of my folder look like so now this folder since i called git and knit in the terminal when i was inside the folder it's now become a git repository so now we can do things in this repository for example let's make a file and let's actually track the changes that happen so here i am inside my folder git repo one here's the folder and i'm going to make a new text file i'm going to call it sample.txt so it's just a simple text file and i can open it and we can edit it and i'm going to write some text here essentially so i have a text file i've saved some stuff in it and so this is now part of my folder and remember that git tracks all the changes that we make to things so the question is how does this work well let's look into it with a little more depth so the number one command that you use in git if you want to see what's going on is you go get status and this tells you all the information about what's happened we need to help get along a little bit it's not automatically going to track everything as a function of time it's going to track snapshots that we take so we'll change the contents of the folder we'll take a snapshot and get remembers that snapshot then we change things a little more we take another snapshot which is an instant in time of all the files and all the subfolders inside our git repository so what git's telling us right now is that it has an untracked file so the way that it works we need to set up our folder so that we can take a snapshot that's the whole point of git in a way and so for example you might change a whole bunch of things um and you take a snapshot now you may not want to you may change 20 things right say there's 20 different files and you take a snapshot you might want to take a snapshot where you've only changed one of those things or maybe you change a bunch of you know dot pi files a bunch of c plus plus files a bunch of other files and you want to take a snapshot for you know each of those files that you change separately so what git has is it has i like to call it a stage it's the where you stage uh the things so here we've only made one change so what we need to do is we need to say okay in our next snapshot that we take of the folder and all the contents this is going to be one of those things that's included in our snapshot so i need to go get add that's the command we're adding it so that we can take the picture instant of time of all the contents in our file and we're going to add sample.txt so sample.txt so now it's added so now i can go get status again again git status is going to be your favorite command of git and it says okay well there's a new file sample.txt this is a change to be committed so we've added it right and the question is how do you take a snapshot right we want git to take a snapshot of an instant in time of what's happening to the folder well there's this new thing sample.txt and if i want to take a snapshot i go get commit that's the command to take a snapshot so git will memorize the entire structure of your file and all the contents when i go git commit so i go get commit and typically what you want to do is you want to go dash m and you want to put a message you want to put like a caption for this snapshot that you take what's so special about the snapshot what did you change to go from the previous snapshot to the new snapshot so here i would say add sample.txt file so we've added a new file that's what we've done when we've set up this snapshot so now one file changed it's taken a snapshot so this is like a new snapshot now suppose we make another change right i go back to my folder um in my git tutorial git repo one maybe i'll make another change to sample.txt some text here now more text right so i make another change to my file i save it and suppose also i create a new file so i'll create another text file i'm going to call this python script pi so now i have my new script here pythonscript.pi and sample.txt and i've changed this and i've added this new file so if i go git status so what's happened since the most previous snapshot that i took up here uh get status it says okay i've modified sample.txt and i've also added this new file now this is what i mean when i say the staging part which is get add that's the first command git add we're gonna add everything before we take the snapshot so maybe for the this snapshot i don't want the both of these changes to be tracked at the same time in the snapshot maybe i want um you know the fact that this was modified i want that first so i'll go get add sample.txt so this is one of those files in our folder that we're going to add to the snapshot and so if i go get status again it says okay well now this is going to be included in the snapshot this isn't yet so i go all right git commit dash m um message modified sample.txt file so now we've taken a snapshot so now we have our first snapshot where we created sample.txt put some initial text in we have another snapshot where we modified sample.txt so now i can go get status again okay they're still on track file so i want to take another snapshot and that snapshot includes the python script dot pi file so i'm going to go git add and by the way instead of typing out all this dot pi if i want to include all files that end in dot pi i can just go star dot pi so this will automatically add this file so we'll add this file i get status you don't need to type this every time this just tells you sort of what's going to happen i could just go get add and then git commit m added python file so now we have another snapshot so the question is how do we see the history of what's happened well i can type in uh get log and this tells us all the different changes that we've made throughout so our first commit was at 1601 2022 and it just says add sample.txt so that was the first snapshot we took of our repository then there was this next commit where i modified sample.txt and then there was a third snapshot a moment in time of all the structure of my repository where i added the python file now the beautiful thing about git is because this is version control i may be like if i look at my folder structure now i have python script and sample.txt but i can go back in time to one of the older versions of my file structure and i'll show you how to do that so in order to go back to one of these old commits i go get checkout that's the checkout command and i'm going to add like the first few characters of one of these commits you don't need all of them it just git will automatically know what i'm talking about so suppose i want to go back to before i added the python file so i'll type in this number here c7 fc 3d5 it's the first few characters of this and we'll check out this branch and so i'm in what's called a detached head state don't worry about what that means but it's basically like a trial thing where i can look around make experimental changes play around with the code but it's not going to like save anything permanently and so if i go here you'll see that i'm now missing my python file i've gone back in time to an older version of my directory and if i go sort of get and branch so what i've sort of done here is i've created a separate branch and i'm at so here's the master branch that's the one that you are typically using and then there's this branch here this is that detach branch so i went back in time and rather than changing everything i'm sort of on a new what's called a branch in git and i'll get a little into that later but the main thing of why i'm saying this right now is that you know i have this and you might think oh crap i've lost all my files in progress but if i want to switch back i can just go get checkout master so now it's going to send me back from here to here and this is sort of the all up-to-date code now it's back to master i can type in git branch and you'll see that i'm back on master and it's deleted this right so this branch was just temporary i went back in time i could look at the history of the code i could try out things add new code and once i went back to master i'm now on master and i look at my file again and sure enough i have the python script dot pi back now you notice when i switch back i lost this branch i no longer have this here but suppose i wanted to create and this is what you do in git you create separate branches so that you know you have maybe a source of code and one person's working on you know one version of the code one person's working on another version of the code they're making separate changes and then in the end you merge them back into the master branch that's sort of the the general idea of git and i'll get into that but you know suppose i wanted to actually make this a permanent branch so i can go get cycle through i want to check out this part of the code again so i type in this i'm if i go get branch i'm on this what's called the temporary branch here in order to make it permanent i go as follows i go get switch c and i'm going to call this before python script so now i'm on a new branch and so i can switch between branches to switch um sort of the file structure and i'll show you what that means so here i'm on my before python script branch and i can go get checkout master and now i'm on my master branch and if i look at the file structure here i have the python script back and if i again i can go get checkout so i can look at all my branches using git branch it tells me i'm on the master branch and there's this other branch here i can go get checkout before python script now i'm on this branch and i don't have it anymore so again i'm going to go back to get check out faster and i'm back on the master branch so i have two separate branches of my code i have a before the python script and i also have after the python script right and so these are like different states of the git repository they're sort of parallel you can think of the version control and time as serial it's sort of in order but when i branch i take it and i sort of create a new timeline of the git repository so two branches can be created and you can make totally independent changes on these branches so the reason you might do this is because one person might be working with one part of the repository they're working on one job another person is working on a different thing you develop them independently and in the end you merge them back together that's the idea with git so i'm going to make a new branch called get checkout b for new branch and i'm going to call it c code development like that so now i'm on a new branch and i can go get branch and you'll see that i'm on the c code development branch and so now i can start adding some code here so now i can open up a file go here and i can create a new file and maybe it's some c code so called code dot c so i have a new file i've added a new file to my directory so i can go get status and there's a new file that's not being tracked by git yet right it's not going to be included in any snapshots or anything so i need to go git add and i'm going to go star.c that'll add all files ending in dot c i'll add it here it's now part of the thing now i can make a commitment so that i take a snapshot in time for this particular branch of the c code development so i go get commit dash m added c file right i've taken a snapshot in time and i can go get log and it tells me that i'm on my c code development branch i've taken a snapshot at 1642 where i added the c file whereas in the master file here that was where i added the python file and there's other stuff that's been done so i've added a little script to my git config file on my computer it's usually under like local disk users and then your user name and there's something called dot git config you can click on it and i've added this this is from an online thing i'll post the uh code in the description of this video but this just gives you a little way of visualizing the changes that you've made to your git code and that's the command here git lg3 and so you can see this is the initial commit this is where i first made the repository then i made some changes here i modified sample.txt i added the python file then i made a new branch and i added the c file and you can't really see that here it looks like it's all one branch but typically what you do is you're working on another branch and you have your master branch and that's where like if you have a bunch of code people will always download from the master branch so you have your separate branch and every now and then you're in your own branch i'm in my uh c development branch right where i'm adding c code and i say okay i have a bunch of c code i've developed i'm in my separate branch and i'm going to merge that to the master branch so how do you do that i'm going to go back to master so then i'm on my master branch i can check that by going git branch i'm indeed on my master branch there's the c code development branch and i want to merge this c code development to my master branch and for example right now if i look at my code here if i go to git tutorial my git repo i don't have my c code i'm on my master branch and the c code doesn't show up and i want to from that development branch of the c code i want to merge it to master so for that i go git merge c code development so now it's merged all the code if i go to my file structure i now have my c code now when you're working in a team it very well may be that the master branch changes as you're working on your own branch so you take all the code from the master branch put it in your branch you work on stuff other people are working on things the master branch gets updated and then you want to merge back so let's look at what happens there so again i'm on the master branch right now and i'm on master and i'm going to add a file and i'm going to say new text document and i'm going to say other work by other people dot text this is stuff that other people have done in the meantime and so i've added that new file now i need to make sure that it's been added to git so that git tracks it again if i go get status you'll see it's not being tracked so git add star.txt will add that file right so now the file is added and what i want to do is commit so git commit uh dash m other work work by other people in the main repository so stuff has been updated here right so now if i look at my folder structure and i go git repo you can see that there's work that's been done but if i switch back git checkout c code development branch so i'm on this branch here you'll see that i no longer have that work by other people and maybe in the meantime i'm do some new code for example i could go new text document i'll go more c code dot c so i'm adding stuff to the repository right and now the question is well i don't have all the same stuff that master does but maybe i still want to merge this to master am i still allowed to do that well the answer is yes so i have my branch here i can go get uh branch i'm on my c code development and i want to merge all the code from c code development back to master so again to do that i go get checkout master so i'm on the master branch and i can look at that that's all the code that if you sort of deploy your code that's the main branch that people look at to take your code and i can go all right so git merge and i'm going to merge with c code development oh and i need to also add that file so i go get checkout c code development this is a common mistake and i can go get status and this new code hasn't been added yet so again i caught myself in a mistake here i created the new file i have to add it so it can be tracked by git so i can go get add star.c git commit i'm going to take a snapshot in time get command m added more c code now i've added this file and i can go get status everything is updated git checkout master now i'm on the master branch and you'll see that my other c code is not here anymore and so if i want to merge from that branch to master i go get merge c code development so let's summarize what's been done here the master branch was updated in the middle of the c code development being changed and now we want to merge them back together and now i can go uh i look here and i have my more c code and i have my work by other people as well in the master branch and so now i can go get lg3 to get my log of what's happened and this is where it really starts to become apparent of what's happening so here is the very beginning wednesday may 11th you know it's got a timestamp and everything i added sample.txt i modified it i added a python file i added a c file and then i made this new branch here right so here's where the master here's the master branch on the far left here and you can see that this branch was made and after this branch was made this star here that corresponds to this there was something that was done to the main repository so i'm a worker here i take the code from the the master branch i do some edits on it and while i'm editing it during that week of me developing code well someone else changes the master branch somehow so i'm no longer the same there's like additional stuff in the master branch whereas i'm working with like an older master branch here then i make a change and i add more c code that's where i made that file but even though i did that i can then merge this branch back to the master branch and in the end i have all the code here so throughout this whole example i've sort of been working by myself right and it sort of doesn't make sense to use git if you're a sole entity working on code for example if i'm just a person making changes to code i just do it sequentially right there's no parallel stuff going on i just want to work on my code and make change after change after change after change but if you have a big project of people and they each have their own roles you don't all want to be editing the same thing you want to make your own work development branch make your own changes merge them at the end and it turns out that like if you have administrators or people in your you know group when you merge at the end sometimes there's conflicts maybe two people change the same line of code in file and the administrator would say okay i've seen that these two people are changing it the same way and they have to choose which one they want to accept what i'm going to do now is go through an example sort of imagining that there's two people working on a repository and you'll see what that means so here's my github here github like i said separate from git this is where you store code github is like a hub for you know you have your repository you upload them and then this is where groups of people can work on repositories together so what i'm going to do is i'm going to create a new repository and we're going to download it to our computer so here i have new repository and i'm going to call this git tutorial tutorial group so this is our group exercise for our tutorial i'm going to make it a private repository meaning that only the people that i choose have access to it but in this case it's only going to be me again i'm going to add a readme file this is like something that is just a file that's put in the repository and i'll go create repository so now i have this empty repository so what's going to happen is there's this repository and there's going to be different people working on code and different parts of the repository so what you need to do is take this repository from online and put it on your computer and different people are going to be putting on their own different computers and the way you do that is if you want to clone it that's what it's called cloning repository you clone it from this link here so i copy this and what i'm going to do on my own computer is i'm going to have two separate files corresponding to the same repository but it's like as if two people were working on it separately you'll see what i mean so here's the link that i clone the repository from and i just use the command git clone i copy and paste this link and i'm going to call it person one so that's the name of the folder so here i go like this clones in a folder known as person one takes all the code i look at my directory and i have person one this is like its own git repository so one person has cloned the repository put it into their own folder i'm going to do it again so this is like if i'm simulating two people are working on the code i'm going to clone the same repository but put it in a different folder so there are different folders on my computer so they're separate and it's the same thing as if those folders were on two completely different computers so i have person one and person two i'm gonna make changes in each one of those folders right so it's like two people are working on the code and we'll play around with a few examples so for example suppose person one is in charge of developing python code so i'm gonna cd to person one and again i can go get status and it shows that it is indeed a git repository and we can start working on things so suppose person one right i'm person one here i have the git repository i create some new code i'm gonna call it uh python code dot pi so they're working on all their stuff um they've edited the repository and maybe person two here they're working on c code so i create a new file i'm gonna call this c code dot c so each separately they're both working on their own different parts of the project so now here i am person one i've just made my pythoncode.pi file i'm in my person1 directory i go git status and the pythoncode.pi is there and for person2 again suppose i'm on another computer here so here i am cd get tutorial cd person two so here's person two so this is like terminal here is person two this terminal here is person one so if i go get status here this is person two they say okay well i've added my c code file person one well they've added their python code file these are separate folders it's like if they were on different computers each person working on the code they don't talk to each other each folder is the git repository structure so i make sure to add it i go git add star dot pi and i go git commit add a message and i'm gonna say add python code so this is person one they've added it and now they're gonna make their own development branch so i'm gonna go get checkout b python i'm just gonna call it person one it's typically a good idea if like it's a small group and they're separate people just name the branch after yourself so i'm going to call this git checkout b person one so now it's got all that python code same thing with person two i go get commit dash or git add initial get add star dot c we'll add the c code it gets it ready to be snapshotted git commit dash m add added initial c code and i'm going to go get checkout is gonna be a new branch for the c development see uh i'm gonna call it person two this is the stuff that person's two doing i need to make sure because it's a new branch i add dash b so now i have the person two branch so person one's added their code and now they want to publish it to the main repository right in their own development branch so i go get branch i'm in the person one branch i want to push this to git hub here and there's separate branches on github right now there's only main because it hasn't found any changes if i go get push it will tell me there is no person1 branch on github here but if i want to add it i can type in this command so now it's going to make a new branch on the sort of online hub of where the code is taken so go get push like this and now there's a person one branch up here so if i reload this now there's a couple branches i can go main to person one and now i have python code dot pi here so this is the person one branch again there's nothing new on the main branch and the same thing for person two maybe they do that as well so i go uh git push again it will tell me that there is no person two branch on github but if i type in this command it will give me that it will set that new branch on github so now there's person two on github like this and so if i reload this page you'll see that there's now a person one branch and a person two branch the person two branch contains the c code and the person one branch contains the python code because they're each working on it separately in their own development branches which essentially is what's been done here now if i go to the main branch you'll see that there's none of that code there and in order to add that code to the main branch the best practice is to create what's known as a pull request on github so i'm going to go to pull requests and suppose i'm person one and i say all right i want a new pull request i want to merge person one to main i want all the code that i've developed on my branch to be put on the main branch so i go create pull request and i'm going to say i'm just adding my python code so i create the pull request it's all good there's a pull request that's been generated by person one and now the leader which is also me in this case i'm only a single person i'm just simulating multiple people they'll say okay well person one wants to merge as python code uh there's no conflicts there's nothing that's fine i'll just merge this poll request and i'll confirm the perm confirm the merge so that's good now all that python code has been merged to the main so i can go to my code here and now the python code is there so i go back to pull request i'm person two i want to merge my stuff as well new pull request i want to merge person two to main like this i create a pull request it's able to merge there's no conflicts of any code i create the pull request it's been created checks for stuff okay it's all good you can merge the code there's no you both haven't edited the same file in the same way and i can merge the pull this is the leader of the group that will do this you'll have usually one person in charge for doing this so okay it's been merged and i go to code and you can see that my main branch here now has both my c code and my python code by the way it's called main here it's called master on my computer they recently changed the naming of this so i have an older version of git on my computer but this is what's known as the main branch on github so the next thing is suppose i'm person one and i'm developing all my python code but for some reason i need access to the c code and if i go get branch see i'm on the person one branch and if i go there's the main branch there i can go get checkout main i'm on the main branch and uh i can go get status it's all like this and i go get i can just look at what's in my directory and i don't have the c code i only have the python code and if i want to take that c code right it's the person 2 has uploaded online here so it's here if i want that to be pulled to my repository i need to go get poll so i'm in my main directory here i've switched to main and now i go get poll and now it will take all that code online and it'll push it here and now if i look at my directory i have my c code and my python code so if i go to my folder structure here remember i'm person one i now have my c code my python code and everything so person two's put it online and i've taken it to my computer so i have access to both things so if i look at my sort of whole process of what's going on here i started here this is the bottom and i initially created the repository and then there were two people that were working on it person one and person two and they took from that and they each have their own development branches so person one comes up here and they took a snapshot remember the star is the snapshot and time of the folder structure and here they added the python code right and then it went here like this along this blue path and then it got merged back to main online right i create i created a pull request online and i merged the two branches back together here the same thing happened with person two i went from the main branch here then there's a commit here remember this branch is separate i added my c code and then after the python code was added you'll remember the order of this video i added the python code first online then i created the person2 pull request online and then i merged that to master and so at the end it has both the code from this green branch the added c code and the added python code it's all now on the main branch so now we start getting into a little bit of fuzzy area here and this can get really complicated and this is mostly just an introduction video but suppose i'm person one right and i'm happy in my directory i'm working on my python code and maybe i'm using a little bit of the c code that someone else has written but i notice there's a bug in the c code and it's not my job to work on the c code but i want to fix this bug so that it works with my code so i'm person one here and i open my c code here and maybe there's a bug and i there's a line here and i just fix the bug again this is just notational so i've i've changed this file by fixing the bug and now i've changed the c code so now it's different from person two who's working on the c code so then i add this to my new snapshots i go git add star dot c since that needs to be added again and then i go git commit dash m a fixed bug in c code right so that fixed a little bug and now i'm going to push it to github i'm on my git branch and i've done this in maine you'll see i'm in the main branch but you never want to push directly to main on github it's very bad practice you want to work in your own branch so i'm going to check out person one now i'm in person one branch i'm going to merge this with master so all the code from master gets copied back into person one branch so not master git merge main so i'm merged with the main branch i go get status everything's good and i get push and now this branch will be updated on github the place where all the code is stored so if i go over to github and i reload here okay you can see this is the main branch there shouldn't be any changes here so it's not showing the fixed bug thing that i changed because i only edited my branch the person one branch so i go to person one and now i have c code.c and and you can see the commit where i fixed the code in the the c so there's a line here fixed bug so something was changed in that file and now i put a pull request i say hey you know guy working on the c code there was a bug in your code and i fixed it so i go new pull request i want to merge person one to main i'm fixing the code of your c code that you there was a bug and i want this to go back into main so that you know people who have access to the main branch of the code which is where people download it from they're not running into this bug and i i found it even though i'm working on the python code i found your c bug and i fixed it so i create a pull request and i'm going to say yeah fixed bug in the c code i create the pull request so i'm working on the python code but i fixed the bug in the c code right and so now the leader of the group comes along they say hey oh person one is uh they want to merge a bug in the c code so i would look over and see what they did and i would merge the pull request and i confirm it so now it's been merged and so now i'm back in if i go back to just the the main repository here i go to c code and it has indeed be been put into the main branch here so now i'm back to being person two so maybe i haven't done anything to my code but i know that there was a thing that was changed on github so i want to take my new c code i can go get checkout main i want to you know go back so get a branch i'm now on the main branch and i can pull the new code from github i go get poll so i'm pulling the new main branch right so now i have all my new code and again if i look at my file here and i'm git repo person two and i'm in my main branch and i open c code.c the fixed bug has been put in there so then i say okay now i check out my development branch i'm going to my person2 branch and i go get a branch and i'm in person two and i merge with main so now my branch has that update that person one did to my c code now i'm going to go through another example here that has to do with editing a big file so suppose people are working in the group and they find that they need this big python file that everyone's going to use it's person one's job to create this file so i'm going to go to you know on person one here i'm going to make sure get a branch i'm in the person one branch so i'm person one i'm on my computer i'm in git repo one and i make this new file and i'm gonna call it uh big python file and everyone needs this file it's very important dot pi i'm gonna even add some lines to it i'm gonna go um open i'm gonna go this is line one and they're writing their code and they have a bunch of code they wrote here at one two three four five six seven so there's seven lines of code in this big python file again this is all just for um this is all just a dummy example this isn't actually any code and so i save this file and i want to put this on the main repository so that people can find it i just realized i added that to the wrong directory i'm in person one directory of git tutorial my big python file dot pi i've just added it and i go get status and there's an untracked file so i go git add star dot pi i'm going to add that to the newest uh screen or the newest snapshot i take in my folder get command m add fake python file like that and then i go so it's it's there it's on my branch uh and i can get push that to github so it's on git push now my person one development branch if i go to github you'll see here that i have my bigpython file.pi and now i want to create a pull request so that everyone in the group can see it i want to merge this big python file here to main which currently it is not in main it's only in person one so i go pull requests i create a new pull request and i'm going to say i want you to put the code in person one this includes my big python file i just made i want you to merge this to main i'm going to create a pull request so here i create pull request i'm person one the leader of the group says all right there's a pull request that's been done i want to merge this pull request it looks everything looks good to me and so they add this code to main and now if i go back to the code here on the main branch i have my big python file so person2 might see this and person2 says hey the big python file i've been looking for they go back to their terminal which is here i'm person2 and i get what branch am i on get a branch and i go get checkout main branch so i'm on the main and i go get pull and this will pull the branch now i have my big python file dot pi branch so now i have big python file here and i go get again i can look at my folder here for well get branch i'm in the master branch i have um in the master branch currently i'm person2 i have big python file.pi if i switch branches get checkout person2 i'm in my development branch but you can see that in my development branch i don't have big python file.pi so i have to merge the new code in main to my development branch since person one put it up there it was merged to the main branch online but person two didn't know about that and so they have to get that new code by pulling to their main on their computer and merging to their development branch so i get merge with main so now i have my big python file here and i go and i look at my folder and sure enough here it is so i'm person two and maybe i edit it so this is where things get tricky suppose i'm person one and i add or person two and i go person two contribution so i edit this file in this way right and maybe i'm person one and i do something different i'm also editing this big python file and i say well i'm gonna add something here person one so they've each edited the file in their own different ways the question is will there be a merge conflict on github right so we've each edited the file we've done separate things and now we want to push it and will the combination of those changes be done the answer is yes so i'm person one i made those changes i go um get uh first of all get status to see what's going on that's been modified git add startup pi it's going to be included in our next screenshot git commit dash m edited line seven of big python file so i edited that line and then the same thing and i push that get and again if i look at my branch i'm on person one and i go get push and it pushes to the person one branch on github person does the same thing i go get status okay this file has been modified git add python file now i'm going to take a snapshot and time of my file structure git commit dot m edited line one a big python file and i push that to github git push so both have been added to github and so let's take a look at what actually happened here remember each person did their own changes to the same file so if i look at my person one branch and i look at my big python file they added something to line seven and person two well person two they added something to oh this is the wrong file person two they added something to line one right so they each made their own contribution to the file and we want those you know changes to be both made together so okay suppose i'm person one and i say all right i'm creating a pull request i'm pressing one i want my change in that big python file to be made so i create a new pull request i say all right i want person one to be merged to main and the changes that are occurring are in that big python file i create a pull request all right create the pull request and the lead of the group says all right i see you've changed big python file i'm going to merge it so now it's been merged to main but then person 2 comes along and say well i've also changed that file so i'm going to create a new pull request and i want you to merge person2 domain and you'll see that there are actually no conflicts even though we've changed the same file all right checking for conflicts and there are no conflicts and i'll show you what the end result is so i merge the pull request confirm the merge and let's go back let's see what the main branch actually looks like because we've each made changes to this big python file and you can see that both contributions to the file have both been made now it turns out that those sort of things work if you delete lines git knows if you delete lines it knows to shift everything up it knows how to sort of take into account a lot of changes the only thing it can't do is if you both edit the same line so i can switch to my master branch get checkout or main branch and then i go get pull and i'm going to get all that new code right so if i look at my file here and i'm person one and i look at big python file that pi you know it's been updated for both persons contributions right and suppose i make a change i'm gonna go i want i want person one stuff here right and i save it well actually i make sure to only do this in my development branch so i go get checkout um person one so now i'm in my branch and once i'm in person one you can see that okay the change is no longer there and it's because i need to merge my branch which is older with the new main branch so i go get merge main all right and now i look at big python file again and you can see that it's been done so you see the sort of structure of what's going on here i pull from main i put this into my branch right that's what's being done i never pull branch to branch everything's done in main i pull to maine and i put all those changes into my own branch and that's what's been done here and now i have the contribution from person two as well in my own development branch and so maybe now i want to make a change i go i want person one stuff here save it so i made a change to line four person two they do the same thing they go get check out they're on the main branch get pull so they're pulling stuff from github to that branch get checkout person two so now they're on their own development branch and they go git merge main so now person two also has all those new changes from both person one and person two on their own development branch so i'm person two here i like a big python file okay we have contributions from both people and we're not on the main branch we're in the development branch so then they say i want person or slightly different um no i want person to stuff here so these two files are different right this one says no i want person two stuff here and this person one that says i want person one stuff here so what happens if they both publish to github well let's see what happens so remember they're each on their own development branch so it'll be person one branch on github and person two branch on github right that'll go to each one of these branches and i'll create those pull requests and there'll be a sort of you know issue where they're you know contradicting each other so here i go um get status i need to add it so git add startup pi git commit m edit line 4 of big python file and then i go get push so it'll push that up to github same thing with person two i'm on my person2 branch i go get add startup pi git commit message edited line four of big python file right so i've edited it now i go um get push so now it's pushed to person two's development branch on github so if i look here i'm on person one branch on github i want person one stuff here person two branch no i want person two stuff here and back on the main branch right there's nothing that's been done yet it's still the same as it was before right and now i want to merge each one of those things to main person one and person two have done their own things they each want to merge to maine so i'm person one i create a new pull request i say i want you to merge person one to maine they've won they're ahead of person two so they get their changes first and because you know they're doing their own stuff there's no conflicts or anything they've just changed a line that's fine checking for compatibilities and i merge the pull request i confirm the merge so now if i go back to main here and i go big python file person one got their stuff there but now suppose i'm person two i've also worked on that file and i create a new pull request and person two and i say i want you to merge person2 to main i create this pull request can't automatically merge there's an issue but you can still create a request so i create the pull request and now i'm the person who's in charge of accepting this merge and there's a conflict it says conflicting files big pile big python file dot pi and i have to go to resolve conflicts and you see where the conflict is and this is where git is really genius it knows exactly where that conflicting issue is well person2 branch has this stuff here but the main branch has this here right so this is the main branch what's currently on the main branch this is what person two wants so i can make the change as the head developer the lead developers of this code i can look at these conflicting things and say well actually you know what i don't like so much what person 1 did so i'm going to delete this get rid of this and i'm going to take what person 2 did and so this is the new file and i'm going to mark this as resolved right there was a confliction and now i've resolved it i'm not taking person one stuff i'm taking person two stuff and i commit the merge and now i have to merge the pull request and i confirm merge so now that i've confirmed the merge i go back here i go to big python file and you can see that i've accepted person two's change and not person one's change so anyways this is rarely an issue right usually people are working on their own files but it occasionally happens in sort of more complicated projects that people are working on the same file and it's the role of the person who's in charge of the code to go to the pull requests and say well you know two people have done you know separate things to the same part of the code i have to decide which one is accepted anyways i hope this was a straightforward tutorial to get if you want to see more of this stuff in the future please let me know and i'll see you next time
Info
Channel: Mr. P Solver
Views: 14,395
Rating: undefined out of 5
Keywords: git tutorial, git, git tutorial for beginners, github tutorial, git and github tutorial, learn git, github tutorial for beginners, tutorial, git commit, git commands tutorial, git basics, git for beginners, git commands, git and github complete tutorial, what is git, tutorial git ita, git push tutorial, tutorial git italiano, how to use git, git tutorial for beginners windows, git push, devops tutorial, tutorial git, git tutorials, git tutorial ita
Id: wPq-okUgiUU
Channel Id: undefined
Length: 51min 31sec (3091 seconds)
Published: Mon May 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.