Git Tutorial - Git Crash Course using BitBucket

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back today we're going to cover version control using git version control also known as source control is basically the practice of tracking and managing changes to your software code a version control system is basically a tool or software that keeps track of every modification done to your code and it stores it in a special kind of database get is a version control system that lets you store all your files and track all changes done to each file every time you make a change to your files git creates a version for you this lets you revert to a specific version if you ever needed to get is a software that runs locally on your computer and can have an online host such as github or bitbucket so you could store all your files on a remote host there is a lot of different git remote hosts that allow you to store your code repositories when it comes to using git it makes no difference which host you use pulling and pushing code for example is exactly the same now two of the big hosts for git are github and bitbucket the biggest difference between github and bitbucket is in github by default your code repositories are public this means anyone can access your code now github does have a paid option to make your repositories private but when you use bitbucket by default your code repositories are private this means no one other than you or the people you grant access can see or download your files the nice thing about bitbucket is that it's completely free so if you don't want to make your code public then bitbucket is your best option on the other hand if you're working on an open source code and you want to make your code public then github would be your best option in this tutorial we're going to use bitbucket to get started with bitbucket you will need to create a bitbucket account if you don't have one you wanna go to bitbucket.org from here on the top right you wanna click on get it free and next now you want to go ahead and sign up now once you're signed up with bitbucket on the top you want to click login and let's go ahead and log into our new account now once you're logged in we want to create a new repository and get every project you work on is basically stored inside a repository so on the left here you could click on the create so the plus sign and click on repository now for the project we're gonna basically create a new project and let's call this tutorials so it's up to you what you want to call your project so the repository name let's call it demo and for the access level we want to make sure this is private now if if you uncheck this basically anyone can access your code now we want to go ahead and click create repository now in order for us to use this new repository let's go ahead and copy our url and let us for now store it inside our notepad to install get let us go to get dash scm.com from here we're gonna go to downloads now you wanna select your os if you're using macs windows or linux we're using windows for this tutorial so let's click here now let's go ahead and run our installation file and from here it's pretty simple we're gonna keep clicking next now the installation is done so let's go ahead and close this if you're on windows if you want to verify that you have get installed you want to right click and you should be able to see this git bash here so if you click here it will open this terminal and if you type get you should be able to see that git is installed when you set up git for the first time you will need to set your username and email address once you make any commits this information will show up on your git history it's important to use the same name and email as your bitbucket account to get started we're gonna first configure our user name so we simply write get config and we're gonna do two dashes and use the global flag so we could set the username globally and let's specify our name so you wanna write your first name then your last name in my case i already have my username configured but if you want to change your username you will have to basically use the replace all flag now to configure your user email we're gonna write get config and we're gonna use the global flag and writeuser.email and let's specify our email address now the last thing we want to configure is our default editor for git so if i type git config and we use a global flag core dot editor you could specify which editor you want to use for example you could say notepad for example another one is emacs if you have that installed i personally like nano so let's add that now let's say you want to add notepad plus plus for example then you could simply just do the same thing here and you want to basically add the installation location for your notepad executable so it would look something like this this is in my example this is where i have notepad installed but let's go back i'm using nano to view our current configuration we write get config and we use the list flag and you can see here we have our configuration set so the username the email and our editor now that we have get set up let's initialize our project directory so on the desktop i'm gonna create myself a new folder and i'm going to call it demo so this is for my demo project and let's go ahead and open this and we want to open our terminal here so the first thing you want to do when you want to initialize a git directory you write git and it's now once that's done you should be able to see a hidden file uh called get and this is basically where your get stores all its information locally on your computer now that we have initialized our directory let's go back to our terminal to be able to track our repository that we created on bitbucket we will need to specify our remote origin so remember we created this repository here it's called demo so we're gonna have to copy the url here and to specify our get remote origin we simply write get remote add origin and we paste our url now to check which remote origins our directory is configured for we write git remote then dash v and you can see here our origin is configured for our bitbucket url now one of the biggest advantages of using git is branches the best way to understand git branches is through an example so when you create a new repository your code will always be stored inside your master branch your master branch is a permanent branch that always reflects a production ready state for your code this means that your master branch contains the latest stable iteration for your source code now let's say you have two developers developer a and developer b you want them to make modifications to your source code without affecting your latest stable iteration this is where they would create a branch when you create a branch you must always start it from another branch now let's say developer a starts his work so he creates a new branch out of the master branch once developer a completes his work and his branch is ready to be pushed live then they would need to merge their branch with the master branch once they merge their branch with the latest master branch git creates for you a new version for your master branch the new version will contain developers a work now let's say developer b started working on a new feature developer b created his feature branch before developer a merged his work so both developer a and developer b started working on the same version of master however developer a has merged his branch before developer b so developer b branch is now outdated let's say developer b finished his work now they will merge their branch with master since developer a updated the master before developer b then developer b will face two scenarios scenario one would be developer a has modified the same exact files as developer b this will cause emerge conflict this is your worst case scenario a merge conflict happens when the same exact lines in a file has been modified in two different branches emerge conflict will need to be resolved we will talk more in depth about this in a bit now case 2 there is no merge conflict this is your best case scenario now before we make our first push let's quickly check what branch we're currently on right now so we're gonna write get branch dash vv now when you use two v's basically you'll get a list of all your local branches and beside them here will be your uh the remote branches that they're tracking in this case our local master branch is not tracking anything let's go back to our folder and right now we don't have any files here so let's go ahead and add some code files so i have my folder opened in my visual studio code editor and let's create a dummy file so let's call it index.html and i'm going to paste this very basic html code and let's go ahead and save this before we push our code you will need to understand the three get stages so the first stage you modify your files in your working directory so that's what we did we added a new file here the second stage would be is you stage them this basically adds a snapshot to them in your staging area so i'll show you guys let's go back to our terminal now let's uh write get status and this will show us the status of our branch so here we can see that we have a new file here it's untracked so this file right now is not staged now to stage our file we write git add in the file name index.html or you could use a period which will basically add all your untracked files instead of listing them one by one now let's write git status and here you can now see your changes need to be committed that's the third stage when you commit your changes you will basically store it in your remote origin so to do that we write uh git commits and we use the dash m flag so we could add a commit message and here i'm gonna write first push now this is ready for it to be pushed so when you're pushing the first time you will need to use the force flag so let's do get push dash u so we could track our new remote branch dash f so we could force our changes then we're gonna write origin master now let's go ahead and check what happened to our remote branch and let's refresh and see we have our file here let's go to uh commits and you should see that this is our commit message and let's click on it and see what we did here so as you can see we added a new file now to create a new branch you will write get checkout dash b now you want to specify a new branch name let's call ours test branch and you want to say which branch you want to base this off so we want to base it off our master branch and let's go ahead and see what happens all right get branch dash bv so see here we're currently inside our test branch and our test branch is only local so you can see it's not tracking anything on remote so we only created this locally now let's go back to our code and let's make a simple change here let's change our name so h1 tag so let's call this uh demo let's save this and let's go back to our terminal now let's write get status and we can see that we have modified our file so let's go ahead and add it let's add a commit message and let's say uh updated h1 tag now i'm not gonna push this so i'm gonna just switch to another branch so let's switch to our master branch so we do that by writing git check out master so if you write get branch dash bv you should see that we we switched inside our master branch so let's go back to our code editor and you can see that what we did on that branch is only reflected once we're inside of it so since we're on our master and the change that we did was in our test branch you wouldn't see it inside your master branch so let's go back to our test branch and let's go back here you should see this is our change here now let's go ahead and push our change so let's go back to our terminal and we're going to write get push now since this branch does not exist yet on the remote we will need to set the upstream so we write get push set up stream origin test branch so this will basically create a new branch for us on our remote so let's check our bitbucket account so let's go back to our project and in here let's go to branches so see we have our test branch and we have our master branch i'm going to create a branch inside the bitbucket interface because i want to show you guys how you could fetch and see changes so let's say uh feature and let's make it off our master branch and let's call it uh created online and let's go ahead and create this branch now inside our terminal if you write get fetch and dash all then basically what this does is it will uh fetch all the new changes on your remote and it will display it on your screen here so now if you do get branch dash r which will show you a list of your remote branches you should be able to see the one that we created online now if you create a branch online or make a changes so let's say let's create a dummy branch and you right here get branch dash r you don't see it so you have to always fetch let's not forget that to do get fetch you have to use the two dashes so now you could see our new branch here so if we write get branch dash r you should be able to see it to delete a remote branch on your terminal you will need to basically make a push so you write get push then you want to use the d flag for short for delete and you want to specify your remote so ours is origin and you want the branch name so ours is called feature created online let's go ahead and delete this now let's go ahead and do the same thing for this branch so let's copy this and uh remember we have to push our changes always to remote so when you delete you have to use get push bash the the remote name and your branch name now let's grill our terminal and let's uh see our local branches so so far we have our master link to our origin master here so let's delete our local branch this one test branch so locally you write get branch dash d and your branch name so this does not delete it on the remote just locally and obviously if you're checked out in a branch you cannot delete it so we have to exit this branch let's go ahead and go to our master so get checkout master let's uh see this so now we're switch to our master and we can delete this branch and let's go ahead and make sure this works so right get branch dash bvnc this branch is no longer here now let's check what happened to our bitbucket account so let's go back to our project and from here let's go to branches and see we deleted those two branches they're no longer here and our test branch is still here a merge conflict usually happens when two people modify the same file in the same line of code at the same time so let me show you guys so let's do get branch dash pv to list our current branches so here we have master now let's go ahead and create our new branch so get checkout dash b and let's call it branch one now let's go back to master and let's create a new branch out of master and let's call this one branch two so we have two new local branches branch one and branch two now let me go back to branch one and let's make a change in our code file inside branch one so here i'm going to change the title to branch one and let's go ahead and save this and let's add our work let's commit our changes and for the message let's say changed h1 tag and let me change the edge to lowercase and let's go ahead and push our work so we're gonna have to write the get push set upstream origin branch one now let's go back to our second branch so get checkout branch two now let's do the same thing and change our tag so let's open our code editor and let's say here we wanna write uh branch two so we're changing the same line of code as we did in our other branch so let's save this and let's do the same thing let's add our work let's commit our changes and let's push our changes so get push set up stream origin branch 2. now let's go ahead and merge branch 1 with our master branch so let's go to our master branch to merge branch one with our master we simply write git merge and our branch name so branch one now let's confirm what happened here and yes we we basically merged our work and let's push it now let's merge our other branch with our master so let's do uh get merge and branch two so see right now we have a merge conflict so let's go back to our code editor and in here so let's close this file so a good idea would be basically when you have merge conflicts is to do control shift f and you want to search for just add a few of these symbols and you should be able to get a list of all your merch conflicts so let's go to this one and here we could see that our current change says branch one because we merged our branch one first and here it's telling you the incoming change is branch two so how would you wanna handle this you could either accept the current change except the incoming change or basically you could just erase all this and add your own change so let's say h1 hello world now let's go back and let's say for example you want to accept this change then you press accept incoming change now let's save this and let's add our work let's commit our changes and let's go ahead and push our changes now let's go back to our bitbucket so let's uh go back to our project let's refresh from here let's uh go to commits and as you can see here that we have merged our branch and when a branch gets merged you should you should be able to see it connect here in the line so this branch for example here is not connected and that's our test branch so this is not merged but this branch which is the branch 2 is merged to revert a commit you will basically create a new commit with changes that roll back the commit that you want to revert so let's uh go back to our code editor and let us erase some parts of our code so i'm going to erase the body and let's save this and let's commit this change so let's first check the status so we are on our master branch so let's add our work let's commit our changes and let's say for this message that we want to do a bad commit and let's uh push our changes now let's go back to our bucket account and let's just refresh and from here let's go to our commits and you can see that we made a bad commit and we want to revert this commit now so to do that we will go back to our terminal let's clear everything in order for us to revert our commit we need to know the commit id so let's write get log and let's use the one line flag and in here you could see this is our bat commit and this would be the commit id so let's go ahead and copy this and write get revert and we paste in our id and we want to revert this so let's go ahead and exit and now let's check the status and you can see that we have reverted this commit so let's just push our work and let's check our code editor so right now see here we restored our work and let's go ahead and check our bitbucket account so let's go back to commits and you can see here the revert message this is one of my personal favorite things about git so the get stash basically allows you to store a copy of your work um without actually committing anything so let me show you let's say let's go back to our code editor and let me make a quick change here so i made a change i saved my work let's go back to our terminal and let's see the status so right now i am on master branch and i modified my index file and let's add my work now let's say for example i wanna go to another branch i wanna go to branch one i don't wanna commit my work yet i don't wanna save this i don't wanna push it so i could use the stash so if you write get stash basically right now we have stored our work in the stash so if we go back here you can see that the changes are no longer here so let's write get stash list you can see here i have two items on my stash now if you do stash apply so git stash apply you will basically apply the last element in your stash so now if we go back to our code editor and you could see this is our work here now let me stash this again so get stash save this is another way you could do it now let's uh list our stash so let's write git stash list and here we could see our stashed item so if you write get stash clear you will basically clear your stash and let's see get stash list now let's say sometimes you have multiple items on your stash so let me just quickly make a change here and let me add my work and let's stash our work now i'm going to add another change let's add our work and let's also stash this so get save now let's do get stash list to apply let's say for example this specific stash then you have to reference it by the number here so one so let's do get stash apply one when you initialize any get directory you should always have a get ignore file so let's go ahead and open this in notepad now i get ignore file what it basically does is it ignores any file here that you mention or basically any rule so here for example you could see that we are ignoring star which means all files that end with dot class and this one for example we are ignoring all files stored inside the folder called target now i don't have any of this so i'm going to erase all that and let's go back to our code and let's copy our index file and let's make a copy of it i'm gonna rename this and i'm gonna say secret file so we wanna ignore this file we don't want this file to be committed so i'm gonna copy the name and let's go back to our get ignore file and let's add it in here so let's save this and now let's go back to our terminal and let's do get status and you can see that we modified the get ignore but you don't see our file here so let me go ahead and erase this and let's see what happens now get status and you could see our files is listed so if you want to ignore a file you add it here if you want to ignore a folder you also add it now if you want to ignore all files inside the folder you do slash star if you want to include like let's say for example all your text files you do star dot txt or the file extension this just gives you some ideas on what you could do with it thank you for watching don't forget to like and subscribe to my channel and we'll see you on the next video
Info
Channel: AHT Cloud
Views: 1,683
Rating: 5 out of 5
Keywords:
Id: 1tC6Z57AOkY
Channel Id: undefined
Length: 36min 3sec (2163 seconds)
Published: Mon Sep 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.