Learn Git, Sourcetree & BitBucket Tutorial: A Crash Course for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you know git isn't hard to learn and with a great tool like Source Tree by your side it's even easier to master hi I'm Cameron McKenzie I'm the editor-in-chief over at the serverside.com and you may know me from some of my short form tutorials on YouTube helping people solve really difficult git problems and I've put together what I think is a pretty awesome crash course on sourcetree git and bitbucket that's going to make you very dangerous when it comes to these Version Control tools if you just stick around and give me a few minutes of your time in the next little while I'm going to teach you how to connect to a remote repository using sourcetree I'm going to show you how to clone repositories how to pull from the server how to push back to the server and I'm going to show you the difference between a poll and a fetch I'm going to show you how branching works with sourcetree and git that means creating branches that means merging branches that might even mean resolving merge conflicts beyond that I want to show you how to manipulate your commit history that means amending commit but also doing resets and doing reverts maybe even doing a cherry pick and then I want to show you some of the advanced features of git that sometimes scare people but you know really aren't that difficult that's things like doing a rebase maybe squashing some commits who knows maybe we'll even talk about stashes and smudges anyways stick around and I promise you if you just stick around for a little while you'll learn an incredible amount about get and Source tree but before we do that we've got to install Source tree and that's what's next you know the thing I love about tools like git and Source trees there's no long list of prerequisites that you have to satisfy in order to install them all you really need is a fairly modern operating system I've just typed into Google download Source tree in the first link that comes up gives me the option to download sourcetree for Windows and hey I'm all about that so I'm going to quickly download sourcetree it only takes a second and when it starts up you'll notice that well I have to register Source tree was purchased by atlassian atlasian atlassian I don't know how to say it um it was bought by that company in 2011 and they owned bitbucket they own jira they own source tree as well in order to get started you need a bit bucket account from atlassian so you need to you're going to need to go in and register and I'm going to go in and register I'm going to do bit bucket tutorial at mcnz.com and I'm going to go through this registration process quickly so that you don't have to stick around and wait for it to finish okay and once I've registered and verified my account it's time to pick a username and I'll call it learn Source tree fast let's see if that's been taken and I'll call it learn Source tree quickly and as soon as I've registered it then asked me just to install either git or Mercurial I'm going to have the tool install git for me that's a common question do you need to install git in order for Source tree to work sourcetree will install git for you and there we go git has been installed I'll click the next button It'll ask me to put in my name and email address this is required on every Commit This usually goes into the configuration file there's a git config config command I'm going to leave those defaults there and click next I'm not going to configure an SSH key and when I'm done boom all of a sudden Source Tree starts up now the first thing I want to do once I've got Source tree up and running is I want to actually pull code from bitbucket right one of the key things you do with a source code management tool is share code collaboratively with others and you do that by well writing code on your local computer and then pushing it to bitbucket or GitHub or some cloud-based git repository and when others push to that repository in order for you to get it you pull their changes down so what I'd like to do is I'd like to First go into bitbucket first go into the atlassian tool and actually create a repository so I'm going to go into sourcetree I'm going to verify my email that's all done I'm going to log back in I'm verified I'm logged back in now the first thing I'm going to have to do once I'm inside of bitbucket here is I'm going to have to create a workspace so I click create a workspace but the button and now this workspace is going to become part of your Global URL so you have to pick a unique name here I'm going to use learn sourcetree quickly and hopefully that's not taken learn Source tree very quickly create the repository create the workspace and now that a workspace has been created I can now create a git repository and this git repository I'm going to do some collaborative work so I'll call it collaborative git and then the actual name of the repository I'm going to call it push and pull examples I'm going to make it a public repository I'm going to put a basic readme into the repository I would like to put one file into the repository to start I can accept main as the default Branch name we're moving away from master and we're using the name main now and I'm not going to click create add a git ignore I'm just going to click create repository and the repository will get created on the server now the cool thing about the repository and the servers you can go in here and you can edit code you can manipulate code you notice that one of the options here is to actually add a file and I'm going to add a file to my repository on the server I'm going to click add file I'm going to call that file Rose dot txt that's my mother's my mother's middle name and I'm starting the the file with the letter R just indicate that I created this on a rem on the remote server so I created this file on the remote server it's just a text file you can put anything into a git repository I mean we're always used to putting code in there and Java in there and python in there but text files are fine you can Version Control them as well and working with text files is a great way to learn about git and Source tree and bitbucket so I'm going to commit that file I'm going to accept the default commit message there and once I've created that repository added a file to the repository performed a commit let me go back up and take a look at this repository you can see there's rows.txt in there well I'm going to Mosey on over to the source tree tool and I'm going to click on learn Source tree quickly and you'll notice that there's a repository listed in there push and pull examples and it says do you want to clone this repository and of course I want to clone this repository now the first time you take a repository on a server and make it local so take all those files on the repository in the server and bring them down to your local computer that's called a clone in the future after you've cloned you do pulls and fetches so the Clone only ever happens once so watch keep your eyes on the prize here as we do this clone so I'm going to click on the Clone button it's going to say where do you want this repository well I've actually got a folder called underscore repos over here and I'm going to put it into underscore repos I don't want to put it under I don't want to put it under users account owner I just want to be able to see these files make it easy put it anywhere you want anywhere that you have permissions to on your file system and once I've specified everything that I need I'm going to click that beautiful blue button say clone you can see some movement over there on the right hand side where it has clone the repository I'm going to double click over here and you can see look at that I've got a folder named push and pull examples that's the name of the repository and it's got the readme it's got a git ignore file and it's got rows.txt in there it's got the file that I created on the server so everything here seems to be going swimmingly now what do I want to do next well when you're working with with Git the idea is well you can grab files from the server files that other people have created you can go in you can edit them if you like I could always go in here and double click on rose.txt and say great file click control s maybe close that there and I could go in here and I could create a new text document of my own right why not and so I'm going to go in here and I'm going to create a file called Lisa dot txt so now I've got some files on my file system over here so there's Lisa dot txt now I'd like to get these files up on the server right I've taken files from the server and brought them down well now we've created some files locally on our local hard drive let's push them back up to the server but there's a few things we need to do in order to make that happen and sourcetry is going to make it easier for us notice this link for file status it's actually telling us that there's been two files that have been manipulated here it says uh rose.txt has been edited so this is kind of that orange icon and the purple icon with a question mark says and there's a file named lisa.txt that has just magically appeared do you want and it's asking me do you want to kind of make these files part of a commit do you want to kind of group all of the changes that happened with these files together into one common save point and of course I do so I click the plus button there I click the plus button there and both of those files become staged when you're working with Git You you create commit commits are like save points in a video game right never play a video game and it's like you know you're going along and then you get to a save point and and then you move on from that save point if you die you can always start again from that save point you don't have to start at the beginning like we had to do with Pac-Man um you know that's kind of what a commit is like and get it's all these save points yeah you do some work you're happy with your work you do a save point you do a little bit more work you do a save point you do a little bit more work you do a save point and those save points are commits and that commit history is maintained by git so if any point in time you get killed I mean any point in time you uh write some code that you don't like you can always reset back to a previous point in time in history and just ignore all of the bad changes that you may have made it's an incredibly powerful tool but the first thing you have to do before you do a commit is tell git which files you want to have as part of that commit like sometimes you might create a file in your file system that's just like a note to yourself that you don't want to have as part of the repository part of the commits that go to the server right um so you have to tell git hey this is a file I want to be part of the commit and then maybe these are other files that I don't want to have part of the commit telling git about the files you want as part of the commit is called staging those files so before you do a commit you first stage files that's what I just did there those files were unstaged I clicked the plus button and that staged them notice there's a minus button here I could unstage them I could now click the plus button and Stage them again and once you've done staging all of your files what you do is you do a commit and so I'm going to do commit here it said hey you left the commit message blank that's probably a bad thing and I'm going to call this my first commit that's probably a polite thing to do I'm going to click commit and now I've done a local commit I've now committed created a commit in my local repository and you can see that it's keeping track of the commit history you can see the initial commit you can see the commit that was created on the server Rhodes txt created online and now you can see my first commit which was done locally but what do you think would happen if we actually went online right now what happens if we go to bitbucket and do a refresh notice that we've got the readme notice that we've got the git ignore file but we don't have lisa.txt we've got Rose but we don't have Lisa so how do you take your files and push them to the server well you use the push command it's that easy so let's bring up Source tree let's click on that push button here it says where do you want to push this looks like it created the master Branch by default I feel bad I should have created a main branch but I didn't type anything into that field initially so I'm going to click the push button it's going to say okay do you want a credential helper to manage your credentials I'll say sure and then git is going to take those changes take that commit and push those changes up to the server and now if I go back to my repository and click refresh boom all of a sudden we have lisa.txt and rose.txt up on the server now we did a clone initially when we wanted to get files from the server you only do the Clone right at the start every subsequent time that you want files from the server you do a pull or a fetch so I'm going to create another file on the server here and I'm going to call it rita.txt right it's a file created on the remote server so start with the letter R we're going to create that file now that file is created on the server so on the server if we go back and take a look at our push and pull examples repository you'll see Lisa Rita and Rose but read is not on my local file system so if I come back down and take a look at my Repository I see Lisa I see Rose I don't see Rita so how do you get them well you do a poll to take your files and get them on the server you do a push to bring files down one of the options that you have is to do a pull so let's go back to Source tree let's click on the pull button so you're sure you want to do a poll I'm like yeah I'm completely fine with doing a poll the pull comes down and boom all of a sudden we've got both Rita and Rose on our local file system so we've now taken files and we've pulled them down from the server and we could do this all day we've got Lisa let's create another file we'll call it lynda.txt check out the file status and we can see that Lindo was created we will do a commit another local file created click the commit and now we've got a a local commit but these files aren't on the server so how do you Rectify that situation well again you do a push you push to the server and now if we were to go back to the server and take a look at their status the server would end up having that Linda file so let's take a look at the server click refresh and boom linda.txt is there now I'm actually going to create another file on the server they could have file named Randy Dot txt so create a file called randy.txt on the server do the commit and now I'm actually going to do a fetch so people are familiar with the push people are familiar with the pull people aren't familiar with the fetch um the fetch is interesting so notice I've got the option here to fetch a file from the server I'm going to click that fetch button it says you want to fetch from all remotes I'm going to say yes just fetch everything bring it all down now you'll notice that the commit has come down to my server but the file Randy is not on my file system see with a fetch what happens is the changes on the server are brought down in your your remote tracking system inside of your local git repository registers the fact that oh there's there's changes here so those changes come down to your local repository but they're not put onto your workspace right so the fact that there's changes comes down into your local repository your local repository knows that there's another file named randy.txt on that same Branch but it hasn't brought it onto your file system yet so that's what a fetch does it it brings the changes down but it doesn't actually put them on your file system so that's good that gives you the updates but you know maybe if you actually brought the code down onto your file system it might cause some problems you might just want to get the the changes onto your local machine and then merge them in a little bit later and in fact that's what you can do so when you're happy you can right click on that Branch you can right click on that commit that's not in there click merge are you sure you want to merge and I'm definitely sure that I want to merge and as soon as we do that merge you now see that Randy dot txt file coming down on your local file system and so that's the the difference between a poll and a Fetch with Git a poll will take changes on the server bring them down into your local git repository and push those changes onto your local file system if you're pulling from the same branch on the server that you're currently working on whereas a fetch will bring the changes down to your local repository but it won't actually merge them into your local workspace or local working tree is the technical name that we use in git so there you go now those are all of the collaborative functions that I can think of that pertain to get right we did a clone we did a push we did a poll we did a fetch we did a git add that was adding files to the stage underneath the covers that's a git add command we did the git commit the commit is what created those save points for our files and that really is a whirlwind tour of the collaborative tools that are built into git and that are easily accessed if you're using an awesome tool like sourcetree now when you're working with Git Co doing collaborative work is one of the key features that git provides another key feature that gets Pro git provides is the ability to manage and manipulate your local commit history and then I guess the next big thing is working with branches then there's some Advanced topics as well what I'd like to do next is show you the ins and outs of working with your local commit history so we're going to manipulate our local commit history next as you can see git and Source free are awesome when it comes to helping you out with collaborative development that is working with the push pull Fetch and clone commands but you know one of the other places that git and Source tree really shines is with managing your local commit history that's what I'd like to do now I'd like to show you how to manage your local commit history and do it with a local repository this exercise is going to be completely independent of bitbucket or GitHub or git lab we're actually just going to create a local repository where we're going to do all of our work in order to do that just say file new from file new you've got the option to create a new repository I'm going to browse here and I'm going to create a new folder and I'm going to call it collaborative git and I'm going to put that right into my repos folder I'll double click on that and select that as the folder click create and boom all of a sudden a new git repository is created for me now how do I know it's a new git repository well if I dig into it I'll see that dot get folder now by the way it's a hidden folder if your environment is not configured to show hidden folders you're not going to see it I can dig in there if I really want to that's just the bowels of the git git repository there it's got your config file it's got references objects which that's where all of your branches all your commit history all of the references to remote repositories that you might be connected to are stored we don't generally need to go in there but it's good to know it's there and that is proof that a git repository was created now seeing that there's a git repository there just waiting to to track our files let's do the polite thing and add a new file I'm going to add a new text document here I'm going to call it Alpha Dot txt and if I Mosey on over here to file status you'll see that it tells me hey somebody created a new file so I'm going to add that as a stage file and I'm going to call that my first commit on this repository click that commit button and boom I've got a new file committed to my local git history now just so you know what I'm going to do here I'm going to create a couple more files I'm going to create Alpha I'm going to create beta I'm going to create Charlie I'm going to create Devo and I'm going to create Echo so follow along if you can I'm going to create a new file called beta.txt and I think I spelled that wrong so it should be beta that should probably be Bravo but I'm going to come over here take a look at my file status it's going to recognize that there's an unstaged file I need to add I'm going to type that as second commit commit that file now I'm creating a an interesting commit history here let me see I'm going to create a new text document I'm going to call it Charlie dot txt Charlie don't surf and I'm going to come over here take a look at my file status see if we've figured out that a new file was added sometimes just moving from link to link wakes the tool up Charlie is added there that's my third commit I'm going to click the commit button there and you know what I'm actually going to do a little Razzle Dazzle here before I create Devo and before I create Echo see that second commit there that second commit is where we created that beta file I'm gonna tell git to get rid of that file right because sometimes you're doing work and you think ah you know what that stuff I did not commit wasn't really good I want to get rid of it here's what you can do you can right click on any commit in your commit history and say reverse commit and what will happen is the changes that were made in that commit will be removed but the changes that were made in subsequent commits will be left alone so watch this I'm going to say reverse commit keep your eye on the prize over there look at beta.txt boom it's gone I have alpha.txt and I have charlie.txt but beta.txt has been removed so there you go that's one of the tools that you have at your disposal when you start working with Git and you start manipulating your commit history now let's go and create Devo Dot txt and I'm going to commit that file that'll be the fourth commit although you know one reason why that's not the fourth commit make sure you've added it to the index one reason it's not the fourth commit is the revert actually counts as a commit so when you revert a file when you undo the changes in a particular commit a new commit is actually registered for that so you can actually see that we've got the first commit second commit third commit and then there's like this revert commit and then there's the fourth commit which is really the fifth commit but it's the fourth commit that we've done so I think we're honest in terms of that now I'm going to come over here create that new file called Echo Dot txt okay Echo has now been created we're going to do this dance one more time I'm going to come in here I'm going to add Echo I'm going to Stage it then I'm going to call this the fifth commit and now look at this Rich commit history that we've got here right we've got a whole history of commits and we've even demonstrated the ability to even reverse a commit now here's what I'm going to do I'm going to say you know what I want to reset all the way back to the first commit right like I want to say okay I've done all this work but you know what the only good thing that I've got here in my life is alpha.txt I just want to go back to the point in time where that was the only thing that I had and you can do that what you can do is you can right click on any commit and say reset the current Branch to this commit you can do a hard or soft commit now a hard commit will take you back in time to that previous commit and it will wipe out anything in your index and it will reset all the files on your file system to that point in time as well which is what I want to do when I've done this reset all of the files are going to disappear except a select few with a soft it'll actually take you back in time but it won't remove any of your files and it won't touch the index mixed will touch the index and but it will reset your files something like that keep your working copy but reset the index so I'm going to do the hard so I'm going to click ok now keep your eye on the prize okay looks what happens when we set back to the first commit boom all of a sudden we are back to Alpha dot txt we have now gone back in time we've gone to a previous save point we've said hey you know I didn't like the the the things that I was adding to my repository I'm going to reset back to a previous point in time and that's exactly what we were able to do there so there you go that's just some of the interesting things you can do with your commit history and git you can create commit you can track a long history of commits you can revert individual commits which will surgically remove the changes that were made in a particular commit but leave changes on commits that were subsequent to that or you can just throw your hands in the air and say I give up I'm just going to reset back to a previous commit completely and there you go those are some of the key tools that git and sourcetree provides for manipulating your local commit history now what's next what's next is working with branches foreign tools like git and sorcery really provide three amazing features for teams one is the collaborative aspect the push the pull the fetch the Clone the other is the ability to manage that git commit history with the revert reset git add git commit but the biggest gift that git gives to the development Community is the ability for developers to write code in an isolated environment through the use of git branches when a developer creates a branch they're given an area where they can develop code they can commit code they can write code they can break things they can take chances and if what they do stinks well they just delete the branch and nobody is the wiser they do something brilliant they just merge that Branch into the main trunk and then everybody can take part in their Brilliance branches is what I want to talk about right now and I've got a little exercise I want to do with you where I kind of create a little bit of a world travel site where I take notes on all of the different countries I visit in Africa and Asia and North America and I want to create branches for each of the different continents now to do this I first need a repository so I'm going to go up to bitbucket I'm going to create a brand new repository I'm going to put this repository in the same project that I created earlier which was collaborative git and I'm going to call it World travels it'll be public I'll put a little readme template in it I'll click create repository you've done this before so this is all old had stuff for you with this created I'm then going to come down to sourcetree I'm going to go file clone new look at the various remotes and there we go World travels magically appears it knows about this new repository I'm going to click clone it's going to say where do you want to clone it and I'm going to say why do you even ask I want to put into that repos folder where I put everything else I'll click the Clone button keep your eye on the right hand side there you see the new folder World travels magically appears and now I've got a brand new repository I can start working with now as I told you I'm going to be traveling around the whole world and I want to do development in separate branches so I'm going to create a new Branch right away I'm going to call it Africa I've always wanted to go to Nigeria I don't know I'll create another branch called Asia I've always wanted to go to Asia I'll create another branch called Australia I don't know and I'll create another branch and I'll call this ant Arc is it Antarctica well it is now we got a bunch of branches here um and I want to do development so you can create branches and branches are places where you can organize the development that you are doing um I mentioned that I wanted to go to Africa so I'm going to head over to Africa I'm going to check out the Africa branch I'm going to go into that world travels folder there now by the way if you don't know where that world travels folder is you can always go into actions and say open in Explorer show and explore that will open up the the repository in Windows Explorer but I'm on the Africa branch and I want to go to Legos so I'm going to create a new document called Nigeria dot txt I'm going to then go in and add that to the index and also commit it hello Legos the realest developers in the world are from Lego so shout out to all of them there um I don't know where I'm gonna go next I guess I can go to Uganda txt I'll head over here file status it's found it add it hello Uganda click commit I hope I'm spelling all of these right otherwise I'm going to get some people very very mad at me and so we've got a a couple of files here on that Uganda Branch now what do you think happens if I go back to the master Branch watch this I'm going to right click on the master Branch say check out master all the files go away they're not there they're not on the master branch whenever you do developments on a separate Branch the files on that Branch are isolated to that Branch they're not shared with other branches so for example if I come over here to Asia and I check out Asia by the way we don't use git checkout anymore we use git switch the word checkout is a the old command that we had to move from Branch to Branch but it sounded like old version control tools where they would check out files and lock them so nobody else could access them and git doesn't work like that so they've moved away from that name it's now git switch but git checkout is still inside the tool but now we're on that Asia Branch and I've always wanted to go to Japan and so I'll add Japan in come over to file status do an ad hello Tokyo do a commit no no why don't we add another one create a new file and we'll call it korea.txt I could do this all day right you know go in take a look add the file we're getting good at these commits let's do Gangnam Style there we go hello Korea shout out to all the K-pop stars out there now look we've got Korea and Japan on the Asia Branch we could head over to Africa and check out Africa we've got Uganda Nigeria on the Africa branch and we can go on to the master branch and there's nothing on the master branch so we've now got into this scenario where we've got isolated development happening on multiple branches at the same time now the idea is eventually you take the changes on those isolated branches and either you delete the branch and get rid of them or you merge them into your code and so that's what I'm going to do now I'd like what's on that Africa Branch so I'm on the master Branch I'm going to right click on that Africa branch and I'm going to say let's merge Africa into the current branch so I click on that it says are you sure you want to do that and I'll click ok and boom you can actually now see the get graph there showing how we've actually merged the Africa Branch onto the main branch and if I actually take a look at that main branch now you can see that Uganda and Nigeria now on the main branch and the main branch and the Africa Branch look like they're pointing at the same thing right now right they're pointing at the same commit they're at the same state but Asia Asia is a little bit different right now it's standing on its own now I'm going to switch over onto that Asia Branch so I'll check out Aja and here's a cool thing you can do you can actually Branch off branches so let's go over to here and say let's create a new Branch off of Asia called the Middle East and we'll create a branch called the Middle East and as we go in here I am going to add in a new file called Iraq dot txt right because it's part of Asia go file status add that in hello Iraq click commit what else am I going to add here I'm going to add a new file we've got a rack we will add Iran I'd like to visit Iran there we go we add that in add a commit message so we've now got that committed oh I got to make sure I got it to the index seemed like it didn't go in there now we've got the commit and watch this I'm actually going to add a new file and I'm going to call this file Egypt right because Egypt's part of the Middle East right it is I think and you know what I'm going to unstage that because it should be Egypt I spelled it wrong I'm going to insult some people there if I spell it wrong so let's see if we can get a Refresh on the file status here we go Egypt added in I want to see the pyramids there is no commitment to spelling here there may be spelling errors as we go along now here's the problem uh Egypt is arguably part of the Middle East I think they would say that they are I think they are um but it's actually part of African continent right so I'm going to go over here to my Africa Branch I'm going to check out my Africa branch and the Africa bench has Uganda and Nigeria on it and what I'm going to do is I'm going to go to this commit here and a cherry picket and cherry pick allows you to take a commit from one branch and Pull It in to another Branch so I'm going to go to that commit now remember that commit there over on that Asia Branch that has Korea and it has Japan has Iran it doesn't have Iran oh yeah this one has a Rand Iraq because it is the the Middle East Branch which is off the Asia Branch if I tried to do a merge I would get all of the files but I only want Egypt I only want Egypt I want Egypt to come over here and join Africa get out of the Middle East and this let me take some negotiations but let's see what happens so to do that to just take one commit surgically out that's called a cherry pick so I'm going to do this cherry pick it's going to say are you sure you want to do a cherry pick and I'm like yeah and boom all of a sudden the Africa Branch now has Egypt on it now if I actually go over to this Middle East Branch and check out the Middle East Branch you'll see that Egypt's still there it didn't delete it right because that stuff in that branch is over there it wasn't like a revert command it left it alone but it did pull that file out and pulled it onto another Branch so now we've got it all in that Branch there and so that is the idea of a cherry pick to cherry pick across branches now um what have we got here now we didn't actually go to Antarctica so you can delete a branch if you want sorry Antarctica um but just too expensive to get down there um I will visit Australia one day um but right now I want to go back to the master branch and you know we've got some changes that we need to bring in I want to bring in all the changes from the Middle East Branch so I will say the merge the Middle East into the current branch so now I get all the changes from that Middle East Branch is the Africa Branch any different I'm going to say let's see if we can merge the Africa branch in but you know what the Africa branch is uh well it's going to be merged merge in so we now merge in the Africa branch and now we've got this Rich commit history and do we have everybody here Korea Nigeria Uganda Iran Iraq Japan and Egypt I think we've done some amazing travels here now with everything there in that main branch you could potentially go in and then delete those other branches right you might not need them anymore I'm not going to delete them but what I will do is I will do a push right because if I actually go and take a look at bitbucket up here it's got no idea about all this tomfoolery that I've been up to the only thing that bit bucket knows about is that Master Branch so let's do the update let's do a push right you got a bunch of changes let's push them up now it says what do you want to push you want to put Africa Asia Australia Asia Middle East we'll push that one up we'll click push it's going to do the Upstream fetch by the way if you do this on the command line with Git it's like a git push Upstream Branch name so it's actually fairly complicated but as you can see with Source tree it's incredibly easy and now I end up going over to bitbucket do a little refresh here boom all of my files are available and you can see all of the branches that I pushed up to bitbucket as well and there you go that's how you work with branches and that's how you cherry pick and that's how you push branches back up to bitbucket through sourcetree it's all pretty cool now one thing that we didn't talk about was we didn't talk about merge conflicts talk about that next the bane of all software developers is the merge conflict nobody likes dealing with merge conflicts emerge conflicts occur when two developers are working on the same file in separate branches at the same time and they try and merge them together the best way to resolve emerge conflict is to not have merge conflicts in the first place make sure your team lead or that your self-managed team everybody knows who's working on what and you don't have two developers working on the same files at the same time but if it happens you gotta merge those files together and there's no magic way to do it you just gotta go in and merge them now I'm going to create a new local repository here I'm going to call it conflict so I'll click on this create button here I'm going to browse to my repos folder and create a new folder for this called conflicts we're going to do a little merge conflict demo here so I'll create this new folder click create click yes and in this folder I'm going to create a new file called Canada kind of continue on our world travels here I'll call it canada.txt there we go canada.txt is created and right there in our main Master Branch we're going to have our first commit first commit okay life is good now I'm actually going to go and create another Branch I'm going to call this Branch federalism now we've got a branch called federalism and I'm going to go into this branch and I'm going to say I love Montreal because I do so there we go in that federalism Branch we've got a commit where we say I love Montreal that'll be the commit message as well click commit and now let's head back to that Master branch and check out the master Branch here I'm going to go into that canada.txt file notice there's nothing in it right because that federalism Branch hasn't been merged into the main branch so the file the main branch is different so I'm going to say I love Toronto I'll click save on that then I will come over to my file status add my file say I love Ontario has the commit message my commit messages could really use Improvement but you get the idea and here we are in the master Branch now let's merge the federalism branch in keeping in mind that one file says I love Montreal the other file says I love Toronto two people have been working on those at the same time how do we resolve that do we love Toronto or do we love Montreal well let's right click over here can we say merge file now I have to do it over here right click on federalism say merge federalism into the current Branch it says are you sure you want to do this I'll click ok and boom it now says you now have a merge conflict in your working copy that needs to be resolved before you can continue you can do this by selecting the conflict files but what do you do you just have to go over to this file and when you open this file it's going to say look your student commits here one says I love Toronto the other says I love Montreal that it's got to be fixed and there's no AI tool in the world that can fix this like people are like oh what's the tool that can magically figure out what should have been in here like I don't know what could go in here right like you're done you got to go in and manually fix this so I don't know I'll get this I love Toronto and Montreal I don't know right who would have thought that would there would be conflicts in Canada over federalism between Toronto and Montreal geez that could have predicted that okay so now I've made changes to that file I've got the file status over here I'm going to add that I'm going to now say here is the commit message we're going to merge the branch with federalism and we're going to fix the the conflicts I'll save conflict fixed I'll click commit and now we've merged those branches with the conflict we've overcome the conflict but we just had to go through the file if you've got developers working on 20 files and you've got 20 conflicts you got to go through each file to resolve these conflicts you can do like a uh theirs or ours kind of force where you say oh I want to just take all their changes and force it in I mean you can do things like that but I mean that's not sustainable that's not the way that you're supposed to do development the way you're supposed to do development in scrum in agile by the way scrum certification guide right back there by Darcy declut go purchase it on Amazon you know when you're working with scrum you're only supposed to be working on teams with 10 developers or less and that includes the product owner and the scrum master so really eight developers and that's a Max your developers should be meeting every day in a daily scrum to talk about what they're doing there's no excuse for developers to be working day after day after day on the same files doing a lot of work and then coming back at the end of the week saying hey I've got 20 30 conflicts and I don't know what to do it's a waste of everybody's time it's not agile it's bad development don't do it the best way to resolve a merge conflict is to not have a merge conflict think smart develop smart work well on your teams make sure everybody knows what they're doing participate and talk in those daily scrums avoid the merge conflicts they're just a big waste of time anyways there you go that's probably one of the you know the the toughest parts of doing software development with Git and Source tree but there you go we're able to resolve that conflict right inside the tool that will help from notepad plus plus I don't know what's next on the agenda maybe we should talk about rebasing you know a lot of developers are afraid of the get rebase command but they shouldn't be and more to the point source tree makes doing a rebase exceptionally easy now I'd like to demonstrate a rebase to you but here's the problem I'm gonna have to create a really rich history in a repository so I'm going to create a brand new repository here I'm going to click the create button I'm going to create it in a folder called rebase right here that I've gotten that underscore repos directory you know how to create a new repository this is nothing new but I need to create a rich commit history to make this look interesting to you and I need to do it at the command line just because I'm faster that way notice there's an option open in terminal you can do that on any repository and when you do it it opens a git bash shell and you can see I do commands here touch Alpha Dot txt boom that creates a file called alpha.txt I can then do the get add Dot and get commit Dash M first and boom all of a sudden I've got a commit I can even do a get Branch other which creates a new branch called other and I can do get switch other I'm going fast here because I just I want to create a commit history that looks interesting to you and I've got a bunch of commands I've got to go through as I said you can fast forward through this until you get the commit history but I just don't want to just show you a repository out of the blue and say here you go like I just want to show you how I'm doing it and the fact that I can do this in the terminal window it's kind of cool so what have I got um get um touch beta Dot txt [Music] [Music] thank you [Music] foreign [Music] [Music] okay I've created a fairly interesting commit history and if I do a little Shake here and force Source tree to look at my commit history you can actually see that I've got looks like I got five commits there um the first second third fourth and fifth in my master Branch I've got Alpha Charlie and Fred and the other Branch I've got beta Devo and Echo and what I want to do is I want to bring those files from that other Branch onto the master Branch now normally we do a merge but I want to keep a nice linear commit history I don't want convoluted merge points I want to take those commits from that other branch and just rebase them on and get a nice pretty commit history and here's how you do it it's pretty straightforward all you have to do is right click on that branch and it says are you sure you want to rebase your current changes onto that other branch and I like living dangerously so I'm gonna say Okay notice I'm on the master Branch I've only got Alpha Charlie and Fred keep your eye on the prize keep your eye on the files in that folder over there I'm going to click OK and boom all of a sudden I now have all of the files Alpha Beta Charlie Devo Echo and Fred and keep your eye on that get history there because it's going to change in a second as I do a little refresh here and look at what the git commit history looks like now now after doing the rebase we've now got the first second fourth and fifth commits on the other branch and then these third and sixth commits from that Master Branch re-based onto that other branch and that's where the master Branch sits that's the idea of doing a rebase now you can create a linear commit history instead of having that disjointed Branch history that you get when you do constant merges it's largely preference in the end you have all the same files in your folder and that's really what you're after okay I think we're our time together is coming close to an end um the last thing I think I committed to showing you was uh interacting with GitHub I think we'll do that next now I think our time is coming close to an end right now I think one of the last things I just wanted to show you was cloning a GitHub repository and using sourcetree with GitHub and there's not really too much magic to it I've got a GitHub repository up there it's called macro maven I've copied the GitHub URL onto the clipboard now I wonder if I can actually find my GitHub URL there's Cameron mcnz there's a listing of my repositories oh boy what have we got here macro Maven is the one that I always like I'm a big fan of Maven I've got a nice tutorial on Maven up on YouTube you should check out if you get a chance so I'm going to copy my GitHub repository there and then go over to sourcetree and just say file clone and then under clone it's going to ask for the name of the repository it's going to ask for a destination path once again I'm going to come over here and create a new folder called Maven macro okay so everything is nice and clear and separate from my other repositories and I'm going to click clone and that's just going to go out to GitHub and it's going to pull that repository down now you can actually see all the different branches in that repository and I've got a pretty a pretty rich commit history there so you're actually seeing uh some interesting branches there kind of looks like the French subway system and of course you've got you know the option to to do all of the things that you would do in any other repository so you can open this up in terminal you can open it up in Explorer you can go in here you can create a new file index.txt you can come over here into the file status and then you've got the option to add that in and do a commit and there you go now all of those features of sourcetree are available to you or at your fingertips except now you're working with a GitHub repository and of course uh you can fetch you can pull resources down at any point in time if you were to do a push you would need to provide your credentials of course and that would probably include using a GitHub token that you can access through your account but otherwise it's exactly the same it's just completely different because you're using GitHub instead of bitbucket but as long as you can get that URL that points to your repository you can use code commit from AWS you can use git lab you can use bitbucket you can use GitHub all of those dvcs tools all of those Version Control tools are there are available to you they're at your fingertips and use Source tree to interact with them because sourcetree is an e is an awesome Version Control tool so there you go that's a whirlwind tour of source tree I hope you learned a thing or two there um now I wouldn't mind you do me a favor too so first of all do the like And subscribe thing and if you enjoyed this tutorial tell your friends about it right just tell them there's a good tutorial where you can learn sourcetree uh for free on YouTube I'd really appreciate that also notice I pulled a couple of books off the back shelf if you're interested in anything I've written I did write that book I mentioned earlier Pickering at Springfield all about how the fictional town of Springfield and The Simpsons is actually based on a small Ontario Town that I grew up in named Pickering also I'm a Java developer I wrote a jpa and hibernate book if anybody's interested in that but I also know a lot of people using git are also doing Agile development and scrum and I've been working with a young freelancer named Darcy de kloot she's making a Big Splash in the social media world and she just released the scrum Masters certification guide so anybody that's interested in agile pursuing scrum Master certification just wants to learn a little bit more about scrum I highly recommend that and you can pick them all up on Amazon um anyways if you're interested in my personal antics you know you can always follow me on Twitter at cameronmcnz is my handle also I'm the editor-in-chief over at the serversite.com we've got lots of tutorials on get GitHub get Kraken a bit bucket devops Java python scrum agile you name it so we've always got interesting content over there so check out the site and finally yeah well I guess I said I would not ask you to subscribe but I could use another subscriber so why don't you subscribe on YouTube
Info
Channel: Cameron McKenzie
Views: 17,619
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops, sourcetree, jira, gitgut
Id: x_3NA-_Rjwo
Channel Id: undefined
Length: 63min 10sec (3790 seconds)
Published: Tue Aug 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.