Everything You Need to Know about Git

61.12k views4498 WordsCopy TextShare
ForrestKnight
The first 500 people to use my link https://skl.sh/forrestknight08241 will get a 1 month free trial ...
Video Transcript:
hey my name is farce welcome back so this is more or less a uh crash course in git arguably one of the most important tools or most used tools we use as software Engineers it's an open- Source distributed version control system and fun fact is developed by the same guy who created the Linux konel lenus tvols and this isn't overly complicated it is a Version Control System it controls the versions of your project so you can reference previous versions whenever you want it's like when you're writing a paper for school or something you know you
have your rough draft and then you turn that in and you have to make some changes to the rough draft but you want to keep that version of it that you already turned in so you save the next one save as rough draft V2 and then now if you want to keep that one you do rough draft V3 so now you have three versions of the rough draft and now it's time for your final draft so then you'll save as that so that way you can have all of your previous rough draft versions as well
as the final draft version Oh but you forgot something but you want to keep that version so then you do final draft V2 that's what it is all of a sudden you have five versions of this paper that you're writing saved and then you can revert back or reference any previous version that you have saved that's Version Control you've been doing it your entire life just not in a proper system although I guess now if you use like Google Docs it has Version Control built in kind of doesn't it doesn't have like a history of
changes anyway and the reason I said distributed version control system is because well instead of you just working on on a single project locally and you're the only one that can work on it instead how you work as a software engineer is that you have some sort of remote repository on a platform like GitHub or gitlab or bitbucket or anything like that it stores your code based in a remote repository so that way your Dev team of let's say five people can all clone that remote repository onto their local machines and then you can all
work independently on the same code base without messing up what everybody else is working on and then once you have written your code that you want to be part of the remote repository the codebase itself then you will push that up but it will make a lot more sense if I can show you how all of this is done I just wanted to give you an overview so you can kind of get a mental mapping of it before we actually dive in so if use an IDE or code editor probably has some sort of verse
control or in this case we're calling it Source control buil in so if we were to make a change here and save it you can see that there have been changes to this file navb bar. jsx and we can see those changes right here we deleted this semicolon we don't actually want to do that just wanted to show you as an example however it feels way cooler using the terminal here so that's more or less what I'm going to do and then I'll reference back to this Source control over here just know that this Source
control you can actually click these three dots right here views and more actions and you can see a lot of the very commonly used git commands pull push clone check out to fetch commit changes staging and things of that nature anyway so if gits not already installed what you would want to do is pseudo at install G all and this is if you're on a Debian based drro like auntu which is what I'm using with wsl2 if apt doesn't work or if you know you're on a different drro then dnf is probably what you're going
to use or if you are on Mac OS then you're going to go ahead and use Buu and if you're on Windows well install wsl2 and get that set up with auntu that's that's the only advice I give for that now we'll do something you should only have to do once and that's setup credentials so get config d-g Global credential Doh helper store and what this will do is the first time you interact with a remote repository what happens is that you have to put in your credentials your username and your password and this store
right here will store those credentials in credential help ER and you will never have to do this command again and you will never have to put in your credentials to access a remote repository again because it'll just use this credential helper file right here now the way I do it when building a new project is I set up the remote repository in GitHub first so I'll come right over here create new repository I'll give it a repository name of learn get I am going to make this one private and then I'm not going to add
a readme file because I want this repository to be empty and then I'll come on down here create repository and then you can follow these commands right here where you create a new repository on the command line a local repository remember this is the remote repository the one that'll be on your machine is the local repository which is very straightforward given the names and if you have an existing local repository that's already initialized and everything you just follow this right here where you connect to the remote repository and push to the branch or let's say
you have an existing remote repository but not a local repository you would just come to the repository click on code and then you would want to copy this SSH right here this is the repo URL and then where you want that project to be you would get clone and then paste that repo URL and what this will do is it'll pull down the entire code base initialize a local repository for you and then connect to the remote repository all with this right here however I do it a little bit different than this not completely different
but a little bit so what I've done locally here set up just a little simple V project with the react and typescript so that we have you know a local code base but as you can see it is there's no git repository local yet what we're going to do is set everything up locally so that way we can push that empty repository all via the command line over here so what I want to do first is initialize the local repository with Git in it and then as you can see right here in Source control now
we have all of this as you can see we have all of those code files that are in our working directory that will'll then need to stage and then commit and then once we commit they will be removed from this section here and what I want to do is get add period which adds our changes here in the working directory to the staging area and you can see what it's doing right now see these are now staged changes instead of just changes and what the staging area is for is to store information about what will
go into our next commit which is what we'll do now and by the way I only keep referencing this just as a visual way for you to understand what is happening typically you're not going to be referencing that if you are in the terminal you'll only be referencing this over here if you are using this over here which by the way see I can unstage all changes and now that just undid the get ad Plus or sorry get ad period and then if I wanted to actually do the get ad period in other words stage
all of those changes all I would have to do is hit that plus right there and that's doing this Command right here and the period just adds all of the modified files but you can specify which files if you want and then what we can do is get status you don't have to do this but get status just to see changes to be committed which is everything we just these are all of the modified files that we just added to the staging area get status as your best friend it'll let you know what's staged what's
not and if there are any untracked files so just keep that in mind now in order to commit these we want to get commit - M the- M is saying Hey I want to include a message here and that message goes in quotes and we're just going to call this one initial commit now when it comes to a commit message we want the message to be descriptive to what we did the initial commit that's kind of you know tradition initial commit as the first initial commit is tradition but if I wanted to let's say uh
maybe I should have not done that yet anyway get commit DM we have nothing to commit right now I'm just doing this as an example if I wanted to uh let's say I fixed a null pointer exception in user login that's what I'd write as opposed to fix bug because what was that bug you want to be able to describe exactly what you did with as few words as possible while it still making it clear and specific because fixed bug is what you did but it wasn't specific there are also conventional commit guidelines you can
actually see those right here whereas they really like this format you know if you have a feature and that feature is an API what did that what is that API specifically send an email to the customer when a product is shipped or if we added a feature a new language we added Polish language so some people say you want to you know do something like that or if you fixed you know login resolve race condition in log flow then you would write it like that as well I don't do that but some people like to
do that now the commit that we just did that we me the message for it was initial commit that is still only on the um local repository because we haven't pushed it to the remote repository in fact we haven't even connected to the remote repository but we can actually see the commit history local commit history by using git log and this is the commit hash right here and it is indicating that this is the head of the master Branch this is is where we're at right now and that is the message right there now in
order to connect to that remote repository what we want to do is get remote add origin actually this is the exact command that we're doing which is the same as you can see right here but this is our uh repo URL a repository URL so get remote add origin repo URL enter now we're connected to the remote repository and what we want to do is push the commit which is get push you origin master and when we do that as you can see it has pushed our local repository and those changes that we staged and
then committed so it pushed our commits up to remote repository and now you can see all of those here now let me explain this Command right here for you U is set up stream so what that does is is it configures this local repository this local branch specifically since we are doing origin master this local branch to track the remote Branch so you only need to do this once this right here once and then origin is what we named the remote repository so that is what we referenced locally in order to access that remote repository
and then Master is the name of the branch that we're pushing to and in this single instance we're actually naming it for the first time now I said the term Branch what's a branch in git well let's say you want to add a feature and that feature consists of five to 10 commits so what you just saw as initial commit that was one but let's say you wanted to add a feature for login and you don't want to just do one giant commit you want to be committing along the way so you have that history
so instead of committing every single little change to the master branch that everybody else is using as well what you want to do is create something like a feature branch and in order to do that what you would do is uh get Branch feature login system let's use that for example and then you can use just get Branch without any name after it in order to see what branches you have and as you can see this Asis right here is indicating that we are on the master Branch so what we can do there's two different
ways to do it I think get switch is technically the new way and then get switch to feature and now we switch to Branch feature to login so if we were to run get Branch again you will see that feature login system in this instance it's green I don't know if all terminals do that but just look out for the asteris that will indicate what branch you're on so you know which one you're working on and when we created get Branch feature login system what happened is that we created a new separate version of the
master Branch so this Branch isn't empty it is this it is right now the same exact thing as the master branch in terms of the code that is on it and every change that we make to it we will it'll be occurring on the feature Branch as opposed to the master Branch the master Branch will be staying the same and then the feature Branch will be getting new and new code so now we can make the changes do all of those commits we can test it and then once everything is all good well actually let's
let's do it let's do something over here let's pretend that there are no red lines V plus react plus typescript okay so we made that change and you can see the change right here and what I'm going to do is the same thing all over over again so what do we want to do now we are on the feature Branch we want to get add dot or again we could do let me look at get status so we've modified Source app TSX so what I could do which I never do but some people say you
should get add this modified file right here Source app. TSX and you can do it like that I like to use a period because typically I am committing all of the changes but that's not always the case get commit message Change Hero text because I think that's what I did and then what I'm going to do is get push you origin feature login system and as you can actually see yes I made myself disappear because I was in the way but as you can actually see right here what I described earlier about that Upstream I
did say that you only have to do this once well we are on a new Branch so we're setting a new Upstream for the master Branch we only set it once for that for here we only need to set it once for this but so Branch feature login system set up to track remote Branch feature login system and this actually created that remote Branch we push it it created that remote Branch that's all you have to do and origin is just saying in that remote repository again that's what we named it so if we were
to come over here you can see that feature login system had recent pushes 3 seconds ago and then we can compare and pull request so what did I say before every Dev on your team is going to be working on their own let's say feature or fixing a bug but in this instance a new feature we wrote the code we did all of these different commits well we only did one but pretend we did five or 10 and now we've completed this we've tested it and then we're saying okay we think this is good I
push it up here and then what I want to do is actually see okay it's able to merge there are no merge conflicts which is a good thing and then I can create a pool request and what creating this pool request will do it'll indicate that I want to merge one commit or however commits 510 commits into into master from this Branch so basically I want to merge this entire Branch into Master we have no conflicts and what I'll have is another developer on the team review this code they'll review the code okay if it's
all good then they'll go ahead and merge pool request over into the master Branch merge pool request confirm merge and then is successfully merged and closed and then in theory you could delete the branch because you no longer need it however now when we come over to get switch Master see now we can see that uh we're wait what I spelled that wrong okay we can see we're on the master Branch now we're actually not in line with what is on the remote repository we're actually behind so what we'd want to do is pull those
changes down since everything is in line already we have no changes here so as long as the master branch is the same and the only change is on the remote repository you can get pull origin Master which as you can see it pulled one file Chang two insertions two deletions and that was the change from our feature branch that we merged into our Master Branch remotely and then pulled down locally and what gpol actually is get pool is actually get fetch because we're fetching that information and then and then we're merging that change with our
local code base now one command I want you to really try is get skillshare that'll give you a one Monon free trial of skillshare Premium well not really you you you have to click the link in the description for that skillshare is an online learning community for creatives where you have access to a Litany of courses that can teach you programming Concepts like oop or webd and business and marketing like these courses here or maybe you're a fan of MKBHD and want to become a YouTuber because he's made a course all about that skillshare has
something for everyone from beginners to Professionals in this summer skillshare is offering the first 500 people who use my link in the description a one month free trial so get started learning a new skill today via a class on skill share or a complete learning path big thanks to skillshare for sponsoring this portion of today's video now when you inev L mess up because you will you'll commit something you want to undo or even worse you want to remove the history of the commit alog together we've all been there it's okay it's also kind of
one of the points of why we use verse control in order to revert back to a previous version maybe because you broke something it doesn't matter the reason we can do it that's the whole point so in order to get to a previous version of your code base well there are a million different ways to do it and this is it's one of the issues with get and it anyway it's slightly annoying but whatever one way to do it is get revert commit hash so let's say hash so whenever you see brackets like this that
means you want to replace all of this with whatever is said inside the bracket so in order to get the commit hash we want to go to get log and this right here is the commit hash and this is the one we want to be reverting and then you just hit enter if you want to see any commits before that but this is the one we want to be reverting so we can come here we can copy this I'm going to end that clear get revert paste the commit hash and then you'll get this file
we want to revert Change Hero this reverts Commit This is the previous commit so if I wanted to name this something else I could change this text right here but instead it's all good so I'm going to crol X and as you can see we have reverted that previous change so if we come over here and get log it doesn't remove it in the commit history so as you can see this is our initial Commit This is on a different branch Let's ignore that for now this is us merging that Branch so this is the
second commit on Origin master and then this is the final commit in where we're at right now which is reverting Change Hero text so what should be in our code base is that change that we made previously should not be there remember this was Plus Ts it is not there because now we have reverted back to the same version of the code as what we had in our initial commit because we undid the commit between it now what if we wanted to remove this entire history so we made the change and then we undid the
change so we did the thing and then we undid the thing what if we wanted to just remove all of this stuff up here and just go back to the initial commit in our Master branch and for and delete the commit history well we want to copy the commit hash of the commit we want to reset to so we want to go to initial commit here we were reverting this commit so we paste it in that commit hash instead we want to go to this one reset to this one so we're going to copy this
commit hash and then we're going to run get reset hard so it doesn't ask any questions and then we're going to paste in that initial commit commit hash enter head is now at that get log and as you can see the only commit in our commit history is this one right here now if you wanted to remove all of those other commits from the remote commit history this is only local if you wanted to remove it from the remote one you would do get push Force origin and then the branch so master in this instance
and if we did that we come over here we will refresh and it deleted those other commits typically some people think this is clean and in some instances it is but it it it deletes all of the other versions that you had and you only want to do that if you're the only one working on them and you know for a fact you do not want those versions if you are working on a repository with others and the branch I'll specify with others don't delete any of their versions they won't like that very much now
there is one thing that I did not mention and that is well what if I want to reverse to the initial commit or re sorry reset to the initial commit with this right here commit with this Command right here but I have changes existing in my working directory that I want to keep well in order to do that let me come over and actually make some changes so V plus react plus that I don't want to do the same thing as before so it doesn't get confusing so we have these changes now in our Version
Control well what we want to do is we would want to get stash and it saved the working directory and index State work in progress on master and as you can see we have no changes in the in the working directory because it saved it somewhere else it stashed it away and then we could come in here and run this reset it would reset it all back I'm not going to do it because we already did that and then you would come in here and get stash pop which puts that modified file back into the
working directory it just kind of removes it for a second lets you do all this stuff and then then you put it back stashes it pops it back in done now you can also do that when you're pulling so when you remember we did get pull origin Master when you do this and you had the changes you could still do the get stash then this then get stash pop and that is where I'm going to leave it because while there are there's a lot more to learn when it comes to git and there are some
aspects of git that is just super poorly designed and that's not just me saying this this is common knowledge but it's a very useful tool learn as much as you can this should get you started and this is probably what you're going to be using 90% of the time well I may be missing one or two things but this is all I'm going to go over for now I hope you enjoyed it if you did like the video consider subscribing if you like content like this and I'll see you on the next one
Copyright © 2025. Made with ♥ in London by YTScribe.com