Git Tutorial for Beginners: Command-Line Fundamentals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thank you so much for the helpful video! This was EXACTLY what I needed. I've been looking for a clear and concise tutorial containing the basic steps needed to get started using Git and your video provided exactly that.

By the way, what kind of terminal do you use? It doesn't look like the default Mac terminal.

👍︎︎ 2 👤︎︎ u/toastiemaker 📅︎︎ Aug 04 2015 🗫︎ replies
Captions
hey how's it going everybody in this video we're going to go over get command-line basics we're going to talk a little bit about what git is and then we're going to walk through how to get get installed and then the basic commands for working with local and remote repositories I'm going to use a couple of images in this walkthrough and these images were taken from the probe get book written by these two authors here and it is licensed under the Creative Commons if you want to read the book which is a great resource you can go to the git website click on the documentation and the book is listed there so this video is going to be useful for three different types of people first of all somebody who has never used version control before this is a great place to start because git is the most widely used version control system out there second for people who have used version control but not get so if you're coming from a version control system like SVN then it's going to be great to learn about git also and third this will be useful for people who have used git but have mainly used the GUI visual tools so the GUI tools are really useful but it's better to learn these command-line tools from the very beginning because the gooeys can only do so much and you'll eventually run into a situation where you get stuck and the underlying knowledge of the command line would be extremely useful so with all that said let's go ahead and get started so first of all git is a distributed version control system so if you're coming from a version control system like SVN which is a central version control system then what is the difference between central and distributed version control well a central version control system is located in one place so people can check out from the central location to make their changes and then check everything back in but this can be problematic if you can't get access to that central server or that central repository so for example if that server is offline or you're working somewhere that doesn't have a network connection then you'll only be able to see the files that you've checked out from that repository and no additional information about that central remote repository also if something happens to that central repository or that somehow gets corrupted then you've got to hope that there's a backup of that code somewhere now on the other hand a distributed version control system like it in this situation everybody has a local repository so you can have the option to have that central remote repository which we'll look at here in a little bit but your local repository has all of the information that your that your remote repository has based on the last time that you sync those two together so the good thing about this is that if you don't have access to that remote repository then you can still view every single change that's been made to that repository since it was created so in a way it's almost like every developer has an entire backup of the repository so in the worst case scenario of something happening to the remote repository every developer has a copy of that same repository on their machine so that's why it's called a distributed version control system so now let's go ahead and get started with git now I'm not going to go into too much detail about the installation here if you go to the website which is get a sh s CM comm and then you go to the Downloads tab here then you can find the download and the download for any operating system that you're working with and the installation is pretty self-explanatory so after we get get installed there is a first time setup that we have to go through so first of all after you get get installed then you can check the version to make sure that it installed correctly so if you run this command here get bash - version so I'm going to open up my terminal window here now if I run git bash - version and it pops up with some version here here it says get version two point three point two then that means that git is successfully installed now once you have get successfully installed now we need to set up some global configuration variables now these variables are important because if you're working with other developers then they need to know who is checking the code in and out and making changes and things like that so this is a way to add your name to all of your check-ins and code changes so this is the syntax here to add these variables in here so if I go back to my terminal I can type git config that stash global user dot name and then in quotes I can type in my name here so I'll type in Cori Schaefer hit enter and now we can do the same thing for email so I'll type in user email and then here I'll type in my email address at gmail.com hit enter and now after we've put in those values then to check those values we can do git config - - list and this will list all of our configuration values here now I'm going to have a little bit more than you do you'll probably only have a few values here but I've customized my get a little bit over time so you can see all the values that I have listed here but the important ones to look for are to make sure that we have our username set and our user email set so once you have those values working then you are good to move on now git config was the first get verb that we actually use it was the first action that we used now if you ever need help with any of these actions then you can use this syntax either get help in the verb or get verb - - help and what I mean by that is using config as an example so say I want to see what you can do with the config action so I can do get help config and hit enter and it'll take me to the manual page and you can read all about it here or the other syntax that you can do is get config - - help and read through there and you can do this with any get action so you get ad - - help does the same thing it takes you to the manual page for the add action so that's extremely useful once you start doing more intermediate and advanced things with git it's useful to look at those manuals and see exactly what you can do with them now there are two common scenarios for or people to begin using it so the first scenario is that you have an existing project on your local machine that you want to start tracking using it and the second scenario is whenever there is an existing project remotely that you want to start developing on so first let's take a look at that first example where you have a local code base that you want to start tracking using it so in order to do this it's just as easy as running to get in it command from within the directory in your terminal so for example let me pull up my terminal here now the code that I want to start tracking if I do an LS within this directory the code that I want to start tracking is with us within this local repo directory so if I CD to local repo and then I do an LS - la in here which will list all of the files in the directory you can see that there is a dot project file here and there's a calc py file here so to begin tracking this code with git it's just as easy as running git init and if I hit enter you can see that an edition initialized an empty git repository and now if I do an LS - la then you can see that it placed this dot git file here now this dot git it's actually a directory this dot get directory contains everything that's related to our repository so if we ever want to stop tracking our project with git then all we have to do is remove this get directory just like we would any other directory and if I do an LS L a it's gone and we're no longer tracking this project with git but we don't want to do that we want to track this project so let's do this again now you can see it put that get directory back there and now we are using git within this directory so now we're using git in our directory but we haven't actually committed anything yet so before we commit anything let's first run this get status here so if I pull up my terminal and I run git status you can see here that we have two untracked files dot project and calc py now this dot project file is just a file that I created as an example of the file that we don't want in our repository for other people to see so there's a lot of files that contain personal preferences specific to our machine or our operating system that aren't actually part of the code that we want tracked so for example say that I had my personal preferences within this dot project file I wouldn't want to add those to the repository and whenever other people check those out they would be getting my preferences and we don't want that so what we want to do is ignore these files and to ignore files you just have to create a dot get ignore file so I'm going to do a touch on a dot get ignore file which will recreate which will create that getting more file and now let me go ahead and open this up in my text editor okay so the get ignore file is just a simple text file where we can add files that we want get to ignore and we can also use wildcards so for example here in this slide I have this star TYC which will ignore all files with a pyc extension so that's a Python example but that's not important it's I'm just showing the concept here so let's go ahead and add all these files here to our get ignore file and I'll paste that in and save that and now let me go back to my terminal now that we've added those to the get ignore file and saved it now let's rerun get status and now you can see here that the dot project file is no longer coming up in our list of untracked files it's only the dot yet ignore file and the calc py file and we want to commit the get ignore file because we want the repository to know to always ignore those files ok now let's take a step back here and try to picture exactly where we are right now now with git there are three states that we need to know about there's the working directory the staging area and the committed files and right now we're currently in our working directory so untracked and modified files will be in our working directory and it will list those when get status so you can see here if I open up my terminal then these are the files in our working directory here now the staging area is where we organize what we want to be committed to our repository now the reason for the staging area is so that we can pick and choose what we want committed so if we've done a lot of work spanning multiple files and we want to make multiple multiple commits from those changes then you can stage individual files and commit those in small chunks at once so that's going to allow us to be more detailed with our commits so for example you don't want to make a commit that where you add a message that just say it says like I made a lot of changes to the code you want commits that are very detailed as to what changes were made so now that we know about that staging area let's go ahead and add files to the staging area so if we want to add all the files that are currently untracked or that we've made changes to to the staging area then we could run get add - capital a or we could add these individually so for example here if I did a git add dot get ignore then if I do a git status and that's going to show us okay so here we have kalki Y which is currently untracked and then you can see that our git ignore file is in our staging area because we ran get add dot get ignore and like I said in the slide if we want to add everything to the staging area then we can just do get add - capital a and hit enter and now if I run get status then you can see that all of our files are now in the staging area so if we were to run git commit right now then it would commit these changes to our repository now if you ever want to remove files from the staging area then you can use the git reset command so if I did get reset calc dot py and then did a git status you can see that the get ignore file is still in our changes to be committed which is in the staging area and our kalki Y file is in the untracked files if you want to remove everything from the staging area then you can run get reset without any other options and it will move everything so if I think get status now that you can see that all these files are moved back to our working directory as untracked files okay but we actually want to make a commit now so let's go through this process again what we want to do is we want to run get add - a add everything to the staging area and now if I run a git status you can see that all of these changes will be committed and in order to get these in order to commit these files we run git commit and then - M which will put a message now the messages are important you want to be detailed as to the changes that you made to the code but this is our first commit here so we're just going to put something like initial commit so if I hit enter there you can see that it added the two files to the repository now if I run a git status you can see that it says that the working directory is clean that's because we've committed those files and now we don't have any modified or untracked files those are being tracked now and we haven't modified them since we committed them now another useful command here is a log command so if we run git log then we can see the commits that we just made so this gives us the hash number of that commit now all those hashes will be unique and then you can see that the our author who made the commit and then the message for that commit also so now for this example we are successfully tracking our local project and we have made changes and committed those changes to the repository so now let's talk about the situation that we mentioned earlier where we want to track an existing remote project with git now this is likely how most of you will be using it if you're going to be using it in the work place so for example say that your company has a remote repository and you want to clone that remote repository and begin developing on it now the syntax for this is get clone than the URL and then where you want to clone that repository so in the example that I'm going to use I'm going to use this remote repo here is actually just a directory on my local machine now usually this is going to be a URL so for example if this was coming from github or something like that then it may look something more like this so you do get clone and then the URL of the remote repository and then where you want to clone that repository so in this case that dot is just means in the current directory but like I said in this example I'm going to be using this directory here which is on my local machine but it's going to behave exactly like a remote repository would so let me open up my terminal and let me go back to this basics directory now if I run an LS here then I have this directory called clone repo I'm going to CD into that now if I do an LS - la here then you can see that this is completely empty these are just relative paths here so now I want to clone a remote repository here so I want to do a git clone and then the URL of the repository and then this dot means that I want to clone all the files from that repository into this current directory so if I hit enter you can see that it cloned into the current directory now for doing LS - la then you can see now I have files here now I set up this sample remote repository to be very similar to the example that we just used in the local directory so now that we've cloned that remote repository now let's see how we can view information about that remote repository so in order to do that we can use a couple of commands one of these commands is get remote - V and another is get branch - a so if I was to do get remote - V now you can see it lists the information to the repository here so this is just the location on my local machine of where this repository is and also if I was to do git branch - a then it will list all of the branches in our repository not only locally but remotely as well now we haven't gone over branches yet but we will do that here shortly now let's make a couple of changes to the code base and we can take a look at how to commit those files and how to push them to the remote repository so over here I'm going to go into this clone repo and open up this calc be Y file and now I'm just going to make a simple change here I'm just going to change this multiply function to return what we want it to return so I'll do return x times y and I'll save that so now we've made a change to our code so now that we've made a change to our code how are we going to push these changes to the remote repository so first we need to commit these changes locally just like we did before so if I open up the terminal here then the first thing I can do is get diff and get diff is going to show me the changes that I have made to the code so you can see here that it has this minus sign with the past that's what I removed and then the plus sign here returned x times y that's what I added in so those are the changes that I made to the code now if I run get status we've seen this before and you can see that in our working directory we have this modified dot calc be Wi-Fi and now if you remember we want to add those to the staging directory so that they can be committed so we'll do a get add - capital a to add out all the changes now if I run get status again then we can see that those are ready to be committed and now we can do git commit dash em and then for a message here we can just do a you know multiply function okay so now we have committed these files locally and now we want to push these changes to the remote repository so that other people have access to those now whenever you are ready to commit those changes and push them to the remote repository there are two things that you want to do I get pull and I get push now people a lot of the times forget to get pull but you have to remember that we're now working on a project that could potentially have multiple developers and people have been pushing code to that remote repository while we've been working on our own features so what the get pool does is it will pull any changes that have been made since the last time that we pulled from that repository and as you can see after we ran that pool which said that we were already up to date because there have been no changes to that remote repository since the last time that we pulled from it and now we're ready to push and we will do a get push and this origin here is just the name of our remote repository and then the master is the branch that we want to push to so if we run that then we have successfully pushed those changes that we just made to the master branch of that remote repository so now that we've worked a little bit with local and remote repositories now let's go ahead and take a look at a common workflow that a lot of developers use when working with git so I said before that we were going to take a deeper look into branching so so far we've been working directly on our master branch but this isn't really how you should be using it from day to day so let's say that for example now instead of that multiply function we want to work on our divide function so a common workflow that you're going to use to do this is to create a branch for your desired feature and then begin working off of that branch so the syntax for creating a branch is get branch and then the name of the branch which we'll just call count divide so I'm just going to copy that now let's open up our terminal and now let's create that branch so get branch calc divided and now if I run get branch without the branch name then it's going to list all of my local branches you can see that I have master here which has the asterisks beside it that means that that's the branch that I'm currently working on if I want to start working on the couch divide branch then I have to check that branch out and that's using this syntax here get checkout and then the name of the branch so I'm going to do get checkout calc divide now if I rerun that get branch you can see that I am working on the couch so now let's start working on the changes that we want to make on this branch so the changes I want to make here I want to work on this divide function so I want to return x / y and i'll save those changes and now that I've made those changes now let's just commit this just like we have been doing so if I do a git status you can see that our cow py file has been modified I want to add those changes to our staging areas using the get add command then I want to do a git commit with a message of we'll just call this the vide funk well divide function and now we have successfully committed this change to our local count divide branch now this had no effect on our local master branch and it's had no effect on our remote repository so now let's say that we want to push this branch to our remote repository so this is the syntax for doing that here this get push - u origin which is the name of our remote repository and then the branch that we want to push - now I'm not going to go too far into what that - u does since it's a basic get video but basically the u option just tells get that we want to associate our local cop divided branch with the remote count divide branch and then in the future instead of doing this get push - u origin count divide stuff like that we can just do get push and get pull like this and it will know that those two branches are associated to each other well we're not going to go too far into that right now so if I pull up the terminal here and I do this command get push su origin calc divide hit enter now we have pushed that calc divide branch to the remote repository so now if you remember earlier we did this get branch - a command and if we run this again this command was to see all of our branches so if we run this you can see that we can see our two local branches here calc divide which has the asterisks which means we're currently working on that branch then we can see that in our remote repository we have a calc divide branch and the master branch now there's a lot of reason for why you'd want to push your branch up to the remote repository and a lot of companies that's where they run their unit tests and things like that to make sure that all the code runs well before it's merged with master well let's say that all of those unit tests and everything ran really well and now we're ready to merge that branch with master now the way we want to do this is we want to check out our local master branch so we'll do get checkout master you can see here that we switch to our master branch now we want to pull all the changes down remember before we commit anything to master we always want to pull down the changes in case any changes were made while we were working on our feature you can see here that it says that we're already up to date because nobody else has made changes to that remote branch since we've been working on the couch divide branch and now we can do this option on git branch here which is - - merged and what that's going to tell us is the branches that we've merged in so far and you can see here that if I run that calc divide doesn't pop up here because it hasn't been merged yet so in order to merge that branch with master you do a git merge and then the name of the branch so I want to merge Calif ID with master so I'm in the master branch I do a git merge out divide and it will merge those changes in the master now that I have merged those changes into the master branch and I can push these changes to our remote master branch by doing a git push origin master just like we did before and then now those changes have been pushed to the master master branch on the remote repository so now that we have finished merging those changes from calc divide now that feature is done and we're ready to delete that branch so that same command that I ran earlier just to double-check that everything was successfully merged if we rerun get branch - - merged here now you can see that calc divided shows up here so that means that we have already merged in all the changes in that branch into master so now we're ready to delete that branch since it's no longer necessary so we can do yet branch and then - D which will delete it and then calc divide which is the name of our branch so now that has been deleted locally but remember that we also push that branch to the remote repository so that command that we did earlier get branch - a if we run that again then you can see that we deleted our local branch and all we have is master but we still have this calc divide branch on our remote repository so to delete a branch from the remote repository then it's this syntax here we can do a git push origin which is the name of our remote repository - - delete then the branch so if we run that and then we rerun get branch - a you can see that all we have is our local master branch and our remote master branch okay and I know that that was a lot to take in especially if you're new to something like git but it's really not that bad once you get the hang of it and this becomes part of your daily routine now really quick I'm going to run through a faster example here because I know that that seemed like a very long process but if I'm not talking through step by step then you'll see how fast it is to actually make these changes with this workflow so for example run the master branch here and say I wanted to work on the subtract function so I can do git branch subtract and then get check out subtract so now I'm working on the subtract branch I can come in here make the changes we want to return X minus y save that do it get status over here we've modified that we need want to add that to our staging directory now we want to commit those changes and you want the commit messages to be more detailed than that but this is just an example and then we can do a git push you origin subtract so now we've pushed that branch to our remote repository and let's say that all those tests ran well so now we're ready to merge this with master we can check out our master branch and now we can do a get pull because we want to pull in all the changes that have been made so now we pull it in all the changes now we can do a git merge of the subtract branch this will merge subtract with master and now we can push those changes and then you can go through the process of the leading that branch just like we looked at earlier but you can see how that process isn't actually as slow as it was with me going through and step-by-step explaining it along the way so that was a broad overview of using the git command line on local and remote repositories I know that was a lot to take in for one video but I really hope it was useful for you guys if you do have any questions just ask in the comment section below now there's a lot that we didn't get to in this video and I'm going to make additional videos soon on other topics like if you run into merge conflicts if you need to undo your mistakes some other common pitfalls tagging versions and get and also advanced topics like your get history rebasing cherry picking and things like that so be sure to subscribe to the channel to make sure that you catch those later videos and again I hope this video was useful for you all and thank you for watching
Info
Channel: Corey Schafer
Views: 1,772,419
Rating: 4.9557223 out of 5
Keywords: Git, Git tutorial, Git tutorials, Git Command-Line, Git Command Line, Git Terminal, Command-Line, Terminal, Command-line Interface (Computing Platform), Software (Industry), Git Basics, Git how-to, Video Tutorial, How to, How-to, Web Development, Web Dev, Software Engineering, Computer Science, Programming, Python, Python (Programming Language), Version Control
Id: HVsySz-h9r4
Channel Id: undefined
Length: 30min 33sec (1833 seconds)
Published: Mon Aug 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.