in this episode I'll show you the top git commands you'll be using every day and how they can help you use GitHub like an expert today we're going to dig into some of the most used git commands so you can be successful using get I'm cadesa and I'm so happy you're here with me today let's start at the very beginning after you install get on your machine what's the first thing that you do for me the first thing I like to do is to configure git so it understands Who I Am the git config command allows you to set git configuration values that can help you create a customized git workflow for example to set my email and username I can type gonig D- Global user. name lady Cur get config d-g Global user. email lady cmail.
com and these two settings allow get to associate my work with my username and email so I can get credit for the work that I do I can run get config d-list to see all the configuration settings I've created so far to go a little further I can also set aliases for certain git commands for example if I wanted to create a shorthand or Alias for git init I can type git config -- Global Alias DOI init and this will create an alias for the command get init which is used to create or initialize a new git repository so let's say for example I create a new folder with the terminal command mkdir project 1 I can go into project one by running CD project 1 in its current state project one is not a git repository if I wanted this folder to be a git repository so I can track all changes I make I would type giit I with the Alias I created earlier for the git init command when we run the git ined command in an ordinary folder it allows us to transform that folder into a trackable git repository where we're able to use git commands so think of get in it as turning on a git switch if I wanted to see what I've done so far in my new git repository I can type git status which allows me to see what files have been added deleted or modified in my working directory this returns nothing to commit because we have haven't added any files yet let's create a new file by running touch hello. MD then run get status again now we see that we have an unchecked file hello. MD an unchecked file is one that has not been added to the staging area to add files to the staging area we can use the git ad command so there are a few ways to use this command we can use get ad period to add all files to the stage area from the working directory at the same time or we can use git add file name to add a specific file to the staging area let's create a new file called learning.
piy and another file called waiting. Pi now let's run get add learning. piy to add this file to the staging area if we run get status again this will show us that learning.
py is in the staging area and wai . p is untracked in the working directory when files are added to the staging area that means they're in safekeeping before you commit them think of using the git ad command as telling git okay please keep track of this file in its current state if we make additional changes to the files that are being tracked we will need to use the git ad command again for get to keep track of those newer changes let's add some code to the learning. P file so you can see what I mean current ly learning.
py and hello. MD files are being tracked in the staging area but waiting. py is currently on track if I add the code print I'm learning git to learning.
py then run git status in the terminal you'll see that the learning. py file has been modified and has on tracked changes to keep track of these newer changes we will need to run get ad learning. py in the real world you would typically complete the work you're doing in the learning.
Pi file and then add those changes to the staging area once you're satisfied with your work you can then commit your changes to commit A Change Is to store a version of your project in the git history currently learning. and hello. MD are tracked while waiting.
py is untracked let's run get commit DM initial commit to see what happens run get status a and this will show us that we have two files changed one insertion in git because we just added two files and one of the files has one line of code we also see that we have an unchecked file waiting. piy because that file was never added to the staging area if I add new changes to the learning. piy file we can add both learning.
piy and waiting. piy to the staging area along with any other changes we make by running git add period git commit M add waiting file and new function and that's how you would use the get ad and the get commit commands together these two commands help you track changes to your work and store them okay so now what if you were given a link to a folder from a remote location that you needed to get on your laptop how would you do that in the world of git this is referred to as cloning or making a copy of a remote repository to your local machine you can do that with the command git clone when you work on a team that uses git for collaboration you may be asked to use git to make a copy of a project folder that you need to make changes in say for example we have this remote repository here on GitHub to make a copy or clone this repo to your machine click on the green code button then select the option that says https click on the copy icon to copy the link to the Repository and then open up your terminal and type get clone paste the URL Link in your terminal and hit enter or the return key once you see done you just successfully cloned a repository to your machine now you have your own local copy of that remote repository that you can work in and make changes to Let's CD into the folder and create a new branch that we can work on when working with a get repository with your team it's important to add your changes to a new branch a branch in a get repository is like creating a copy of a document so you don't miss up the original branches allow us to work more collaboratively and effectively with her teammates in the same project so let's create a new branch in the repository that we just cloned type get checkout DB update name to create a new branch called update name this command allows us to create a new branch and switch to the branch at the same time to see a list of the branches that we have have we can type get branch and it will show us that we have three branches the main branch a branch named innit and the newly created update name Branch hit the q key on your keyboard to exit now what if we wanted to go back to the main branch we can use the get switch command to do just that in your terminal type get switch main to return to the main branch great now we're back on the main bran Branch let's return to the branch that we created and add a few changes to a file type G switch update name and then open the project inv vs code navigate to the index. html file right click on the file and select open preview and this will open up a side browser where we can see the changes live in our code let's say you were assigned with changing the title of this app to getg going let's update the name of the HTML file and run get status to see that the file has been modified putting it all together let's add these changes to the staging area then commit the changes to our local repository get add period get commit DM update app name get status awesome nothing to commit working tree clean great now let's get our local changes into the remote repository that we cloned earlier to update the application to get the changes we just made into the remote repository we must push our changes to the remote repository let me explain the command git push tells git thanks for tracking these file changes now I want to upload these changes into the main project file when we push our changes we're essentially updating the remote repository with commits that have been made in our local repository to do this we use the command get push where remote refers to the remote repository and Branch refers to the branch we're working on in our case we can type get push origin update name where origin refers to the remote repository and update name refers to the branch that we want to upload did you get that if we go back to the repository on GitHub we will see that there's a new Branch available it's been pushed up to the repository which is why we call it git push we can now open up a pull request on GitHub to merge our changes into the main branch a pull request is a proposal to merge a set of changes from one branch into another in our case we want to merge the update name Branch into the main branch let's open a PR here click on this green button at a description then click the merge button typically you would have a coworker take a look at your work before you merge your pull requests but for this demo we'll go ahead and merge the changes into the main branch and there you go okay so now that we've updated the main branch in the remote repository with our changes let's go back to our local repository in the terminal type get switch main then open the file in vs code navigate to the index.