Git Essentials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video is proudly sponsored by lenode the note has been doing cloud computing since 2003 which is actually before amazon web services was even a thing on the notes platform you can get your server up and running in minutes and they include all the popular distributions such as debian fedora ubuntu and get this even arch linux and let's be honest what could be better than a linux focus cloud server provider that lets you tell all of your friends i run arch the note has multiple server plans available to make any app scalable and flexible you could use it to host a blog a vpn server a minecraft server and much more in fact lenode is the platform of choice to host the entire web presence of learnlinux tv in addition the note offers 24x7 365 support regardless of plan size so you can get help from a live person when you need it new users can get started right now with one hundred dollars towards your new account and i highly recommend you check them out because lenode is awesome and now let's get started with today's video [Music] hello again everyone in today's video we're going to talk about git git is a version control utility the most popular version control utility and also one of my favorite things even though git is primarily catered towards software engineers we linux administrators we have a use for it too it helps us keep track of files and changes to files and that's what version control actually means and on our linux systems keeping track of config files is very important now before we can actually use git on our linux servers we have to understand well how to use git and in this video i'm going to give you guys an introduction now this is just an introduction the very basics if you do already understand the basics of git then this video is not really for you this is more for those of you out there that you really don't know much or anything about git and you want to get up to speed with it if there's enough demand i'll even consider doing a follow-up video to this one but for now let's go ahead and take a look at the basics of git to get you started now of course in order to utilize git you first have to make sure that it's installed and on a linux system that's pretty easy to do we just type which and then get and if we receive output like you see here that means that git is available and ready for use now if you don't see any output when you run the which get command then that probably means that you don't have git installed installing git is very easy though all you have to do is install the git package on my end i'm running on ubuntu and on ubuntu systems you can run sudo apt and then install and the package name is simply git this command also works on debian and any other distribution that is based on debian and if you're running on a centos red hat or fedora system then you can simply change apt to dnf and if your system is even older you could change that to yum i'll leave it up to you to make sure that you're using the proper package manager but in my case git is installed so there's nothing that i have to do on my end now that we have git installed how do we use it since git is a version control utility it works with repositories and we'll need a repository in order to use git we can either download a repository from somewhere like github or we can create our own so how about we see an example of creating a repository with git i'll create a blank repository a repository that we can use to track the changes for whatever files are important to us so to do that i'll run git init and then next we provided the name of the repository that we want to create i'm not feeling particularly creative today so i'll just call mine my repo i think that's good enough for an example i'll press enter and we see a bunch of information here but more importantly on the bottom it says that it initialized an empty git repository that's exactly what we were hoping for now this verbage up here what that's actually talking about is the main branch when it comes to a git repository although we're not going to cover it in this video since this is more of an essentials video one thing to understand about git is that branches are a feature that's available a branch allows you to temporarily take your repository in a different direction maybe you want to develop a feature or something like that and you want to test that feature before you merge it back to the main branch now the reason why i'm bringing it up in this video even though we're not going to be covering branches in more detail is because the name of the default branch is actually different depending on the version of git you're using now here as you can see is telling me that the main branch is called master that's the old name we're transitioning to main as the default branch and regardless of which of the two names you use they mean the same thing as talking about the default branch if you guys want to see a follow-up video to this one then make sure you click that like button if you like this video that helps me understand that there's actually demand for this content and if there is demand i'll consider doing a follow-up and if i do a follow-up i'll cover branches anyway if i list the storage of my current working directory we now have a directory right here called my repo and that's the same name that i gave the repository itself that's not a coincidence when you use the git init command it actually creates a directory for your repository so now if i want to work with that repository all i have to do is just change directory to go inside that repository directory if i list the storage you can see that it's actually an empty repository and that makes sense because well it told me that it initialized a blank repository and this is a blank repository because as you can see when i listed the storage nothing came up but actually the directory is not completely empty though as you can see here i have a git directory and if i list the contents of that directory we can see that there's a bunch of things inside the dot git directory includes all the information about the repository that's required to make it a repository all the history the settings the database of the files itself everything is in the dot git directory if you delete the dot git directory to this directory itself is no longer a repository that means if i deleted this folder right here then my repo is just a standard directory the only thing that really separates a repository directory from any other directory is the presence of a git directory right here when you use the get command it's looking for this that's how it knows the state the history the status everything about the repository so don't delete that directory unless you no longer want it to be a repository so now we have a repository what can we actually do with it the whole purpose of git is to keep track of version changes for files as far as what kinds of files you can include in a repository it's pretty much up to you for example if you're developing an application you can include all the source code files for that application right here in the repository you can then commit those files to the repository and push the repository to a remote a remote such as github gitlab or even your own custom git server if you wanted to build one is a place where people can then download your code and run it themselves if they notice something wrong with the code or they want to give you some kind of feature improvement they can issue a pull request and then you can accept their changes or deny the changes if it's not something that you're looking for but it all starts with files and even though most of the concepts that i just gave you are all about software engineering you don't have to actually use git for software engineering i'm going to show you how to use it in general in this video but just think in terms of config files on your linux server that you want to keep track of to keep it simple though what i'm going to do right now is create a text file right here inside this directory and i'll name it hello.txt although the name really doesn't matter so now i have a blank file i'll add some text to it and i typed hello from learn linux tv i'll hold ctrl and press o to save the file enter and then ctrl x to exit out and now as you can see we have hello.txt right here now nothing that i just showed you has anything to do with git i just opened a text editor i wrote some text inside a file and i saved that file something you could do all over the linux system nothing to do with git but since the purpose of git is to keep track of version changes let's see what git thinks about this particular file and that brings me to the first get command that i'm going to show you git status it does exactly what you might think it does based on the name it gives you a status of the repository you have to be inside the repository directory for this to work and then when you execute it if there's anything unique or changed it'll let you know now here it's actually coloring the hello.txt file in red you may or may not have colorization on your end but the most important thing here is that it's actually listing that file underneath on tracked files and the takeaway here is that simply saving a file inside the repository directory doesn't itself make that file a part of the repository you actually have to tell git manually that you want a file to be a part of version control and this is really important because you might have files inside the repository directory that you really don't want anyone else to see for example if you have information inside your repository that is private or company confidential then you'll probably not want to include it in version control there's actually a way in git to ignore particular file names but unless you actually add a file to version control it's not a part of version control so as far as git is concerned this hello.txt file is an alien object it's just unwelcome unwanted unexpected it doesn't understand why it's here what its purpose is because like i mentioned it's basically a foreign object but what if we do want this file to be a part of version control how do we add it well the verbage for that is actually very easy it's just simply git add just like this and then the name of the file that you want to add to version control so i'm going to add the file that we've just created if i run git status again the output's going to be different as you can see here hello.txt is now green instead of it showing underneath untracked files it instead says changes to be committed so git add doesn't actually add the file to version control it's not added just yet but it is adding the file to the current commit that we're working on you could think of a git commit as a packaging of changes you could have 10 different files with changes and have them all under one commit it's going to make more sense here in just a moment but for right now you could think of a get ad as adding a file to version control or the file is already in version control you are then adding the changes to version control and a commit in git is pretty much the process of packaging up the changes in preparation for finalizing those changes so we've added the hello.txt file to the current commit that we're working on so how exactly do we finalize this commit now before we do that there's actually a few commands that we need to run first before we can make a commit the thing is when we're working with code generally speaking other people that are working on the repository they want to know who's doing what you might have one person working on one feature and another person working on another feature so it's really important to understand who's doing what now for all intents and purposes you could be the only one working on the repository but either way git wants some information about you the person that's working on the code so what i'm going to do is paste in a command right here that i'm going to have you run and here's the first command git config allows you to basically set a configuration parameter for git itself and this is a user-wide change this isn't specific to the repository this change is going to be for all repositories that you commit to so what we're doing is we're setting a global value of user.name and we'll set it equal to our actual name so i'll press enter and now i'll go ahead and paste in the next command and here it is it's very similar to the other command but instead of user.name it's user.email so we're setting the email address for the user that's working with repositories on this system i'll go ahead and press enter and now that's done and don't worry about jotting down all of these commands you can check out the blog article for this video i'll go ahead and put a link in the description below to that article and you can get all the commands from there if you want to copy and paste anyway let's continue so we've added our user information but let's get back to our commit we were working on a commit we took a little bit of a detour to change the configuration for our user as far as git is concerned but now let's go ahead and finalize our commit to do this we can run git commit just like that dash m and then inside the quotes we could type a little message as far as what this commit is for what's changed basically what do you want someone to know about this commit if you're working on an ansible script for example maybe you've added a task book that does something really cool such as install a bunch of packages configure open ssh you're adding a feature you're making a change what change did you make now for me this is the very first commit of this particular file so i'm just going to keep the message fairly simple this is the first time that this particular file has ever entered version control so i think that's good enough and it looks successful to me it repeated our message right here is telling me that one file has changed there's one insertion to that file and everything should be all set as far as the commit is concerned for run git status again is telling me that i'm on branch master but there's nothing to commit the working tree is clean which means there's been no changes since the last commit now at this point the hello.txt file has never left this computer yes i did make a commit but a commit does not send the changes to a remote server i'm sure most of you guys are at least somewhat familiar with github github is a remote gitlab an alternative to github is also remote and you can run your own git server if you wanted to or perhaps you don't want any remote servers at all you just want all the changes here locally but what's the point of git if you're not going to send your changes anywhere well the thing is when we make a change to a file inside the repository git is paying attention to that so if i bring up the hello.txt file back into a text editor yet again and i add a new line to it and then save it let's see what happens it actually noticed that the file was modified and this is really important because that's one of the major benefits of using version control it helps you keep track of well versions now this hello.txt file is very useless it doesn't really give us any benefit at all but let's say for example that we are maintaining a config file for a server application maybe that application is no longer working if we have the config files for that application in version control we can run git status and see what's changed now specifically if we want to know what in particular has changed we can run git and then diff and then the file name just like that and press enter and then right here in green we have a plus sign which means that a line was added and the line that was added was this one so if this was a server application and we ran something like this a git diff on a config file for an application then we saw a line of config that was not expected then that really helps us understand as linux administrators what has changed when it comes to that file and then we can go ahead and revert that change back if we don't have the config files for the application that's important to us under version control we're going to have to go line by line through the file to find what's changed and i don't know about you but i really don't want to do that and the ability to track changes is a very important feature of git regardless of whether or not you decide to push your changes to a remote even if your changes are here locally it still has value you can see what's changed now in my case i'm going to assume that this particular insertion into the filehello.txt is welcome it's something that i like and i want to finalize that so what i can do is type git add i'll type the file name again and press enter then if i run git status we can see that we have changes that are pending being committed and the change in particular is the fact that the hello.txt file was modified the first time that we ran this git add command we were actually initially adding the file it wasn't included in version control and now we're running git add again to add the changes to version control so on your end you run git add when you want to include a file into version control for the first time and you also run it when you want to accept the changes that have been made so now we have the modified version of hello.txt that's pending being committed and just like before we can run git commit to make the change final last time i ran git commit dash m and then a message in double quotes but this time i'm just going to run git commit by itself and as you can see here it opened up a text editor personally i like to include the commit message right on the command line but if you want to do some formatting or something like that or maybe it's going to be multi-lines then you could just use git commit without any arguments or options and what that's going to do is pop open a text editor so i'm going to keep the commit message fairly simple here and obviously if this was a real repository that was actually important you would definitely want to be more descriptive when it comes to your commit messages but anyway i'll save the file and then exit out if i run git status again we can see that there's nothing to commit the working tree is clean so it did actually finalize the changes so what do we do when we want to actually revert something back to a previous commit i did mention that one of the major benefits of git is being able to control versions so if the current version of the file is being problematic then it makes perfect sense that you might want to revert that change to a previous commit now to get started down this path what we want to do is run git log dash dash one line and as you can see it gives us a list of the commits that have been made to that repository and consolidates them to one line each now in my end the commit messages weren't all that long anyway but as you can see i have a list of commits now this one is where we are right now the head of the repository is pointing at master master is the default branch in my case again it might be called main but head is the current version of the repository so the current version the repository is pointing to master and then on the left we have a hash for each commit so how do i actually return to this one right here if i cut out the contents of hello.txt we can see that it has two lines so if we want to go ahead and revert a change how do we do that for that we can run git revert and then give it a hash and the hash that we want to give it is the most recent the one that we want to revert now this is the one that we actually want to go to we want to return to this but in order to get back to this commit we have to undo this one we have to revert this commit right here this hash so you might think that you should actually revert to this one but what you actually want to do is use the most recent hash the one you don't want because you're going to revert that particular change so paste it in and press enter and as you can see it opened up a text editor and it's telling me that it's going to revert this commit hash right here which is much longer than the original the thing is it doesn't need to show you the entire hash it only needs to show you the first portion or so of that and that's enough for it to know which commit you're talking about and then i could add a little message here and i just added a hypothetical ticket number here i mean if you do have a ticketing system it probably makes sense to include the ticket number here in the message so anyone looking at this knows which ticket they can go to for more information of course i just made up a random number but you get the point so let's save the file and close out now what i'll do is cut out the contents of that file yet again and we're now down to a single line now let's talk about cloning cloning is the ability to pull down a repository that exists in a remote and save it locally this allows you to work on the repository and then you can push the changes back up to the remote now here on another workspace have the official github page for learn linux tv so what i'm going to do is just click on this repository right here it really doesn't matter which one you click on i'm going to click on that one and what i want to do is click on code right here and then we have an option for clone now i'm going to use https you could also use ssh as well if you have that set up but for our purposes in this video i think this is good enough i'll click the copy button and let's go back up to the terminal now it's very important i'm going to go outside of our current repository i don't want to clone a remote repository into an existing repository that would be a bit confusing so to clone a repository i could type git clone and then i could paste in the url right here so i'll press enter and it's cloning it down and as you can see here we have personal ansible desktop configs which is the repository that i pull down and i can go inside there list the storage and as you can see we have all kinds of things in there for example we have a file named local.yml and this is a repository that i used in one of my earlier videos but we have some actual code in here ansible is awesome i have a bunch of videos about ansible and this is a playbook for one of the ansible videos right here but the main takeaway here is that we were able to pull down a repository that exists on github here locally to my laptop now what might be more interesting is to go into the dot get directory and like i mentioned earlier the dot get directory is beyond the scope of this video i can't go over everything but one thing i do want to mention is the config file this one right here and this file is actually very important this is the contents of that file right here this helps us understand what the origin is where this content is actually going to be pushed to if we make a change and then push it to a remote so we have the url right here as you can see so if i was to actually do a commit and then try to push to this repository it's going to attempt to send it to this url now there's other information here as well that i'm not going to go over in this video but if you are curious how to find out where a repository came from then you could just inspect the config file and it's going to have the url right there so what i'm going to do is go back to the repository that we were working with earlier in the video this one right here and what i want to do is show you how to push this repository to a remote specifically i'm going to go ahead and push this up to github now you don't have to follow along with me i just want to show you guys how the process works but if you do want to follow along with me make sure you create a github account of your very own and we'll go through the process so what i want to do here in my browser is create a brand new repository because a repository does have to exist up at the remote so i'm going to go ahead and sign into my account and then i'll be right back all right so now i'm signed in with my get user and what i'll do is click the plus sign right here i'll create a new repository i'll just give it the same name my repo and i'll create it so we have a url right here for the repository but what we want to do is actually push our existing repository to this url so let's take a look at how exactly we do that so to set this up we're going to run git remote we want to add an origin and then right here what we'll do is paste the url to the repository that we've just created on github so i'm going to copy it and then i'll paste it in right here and i'll press enter now it doesn't actually look like anything happened it just returned to the command line if i go ahead and refresh this page there's still nothing here so the changes have still not made it out of my laptop nothing was actually pushed to the remote so what we just did is we changed the origin for this repository and we set it to the url to the repository that we've just created but that's all we did we changed the origin we didn't actually push anything so now that we've set the origin for the repository how do we actually push code to the repository in order to do that what i'll need to do is set up a personal access token of course you could use ssh as well that's beyond the scope of this video if i do a follow-up i'll probably cover that in the next one but for now what i want to do is click on my profile icon and then go down here to settings then i'll scroll down i'll click on developer settings and then personal access token and then i'll generate a new token i'll give it a name i'll just name it my repo and i'll give it access to everything you probably want to be more granular on your side but this is just an example so what i'll do is scroll all the way down here and generate the token so we have this token right here i'm going to copy it and you should never show this in the clear to anybody ever that's very important so that's why i had to blur that out but i'll delete that token anyway so it really doesn't matter but anyway what i should be able to do is run git push i want to push to the origin and the branch that i want to push to origin is master so i'll press enter and now it's asking me for the username for my github account so i'll type it in press enter and then i'll paste in the deploy token right here you know what it looks like it was successful to me it shows new branch it was sent to this url right here so it looks like it worked let's see now here in github i have the page pulled up for the repository there's nothing here yet so let's click refresh and look at this we have hello.txt right here and we also have the string that we added to the file right there as well so as you can see we were able to push our code up to github and other services such as gitlab and many others those will have very similar workflows it's the same idea for the most part but as you can see we were successful we were able to push code up to github and that's pretty cool so there you go in this video we covered the essentials of git i hope you enjoyed it if you did like this video please click that like button to let youtube know that you want to see more content just like this definitely subscribe to this channel if you haven't already done so i have some awesome content coming very soon thank you so much for watching [Music] you
Info
Channel: LearnLinuxTV
Views: 10,964
Rating: undefined out of 5
Keywords: Linux, Tutorial, Review, Howto, Guide, Distribution, Learn Linux, open-source, open source, gnu/linux, LearnLinuxTV, git, github, gitlab, version control, push, pull, clone, git tutorial, github tutorial, how to use git, full guide, tutorial, guide, git guide, git for beginners, git basics, git commit, git commands, git crash course, learn git, git tutorial for beginners, what is git, git course, what is git and how to use it, introduction to git, software engineering, git introduction
Id: u1Zvkqjx0Ik
Channel Id: undefined
Length: 30min 3sec (1803 seconds)
Published: Mon Oct 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.