Branches, Commit, Pull Request, Merge in IntelliJ IDEA and GitHub: Example with Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in a previous video i gave an overview of branches in git and github in this video i'm going to do a hands-on demonstration using java and intellij idea now it's helpful when we do this to have some changes that we want to commit and i want the focus to be on branches not so much on those changes so what i've done to help us prepare is i've written a bit of javadoc that we can use in this process so first of all let's take a look at our current repository and one thing you'll notice is that up here in the upper left you can see all of the branches that are created so far right now we only have one branch and it's the master branch and when you're starting a project that will be your first branch we can create a new branch here but we can also create a new branch in our ide so let's take a look at our id you see where it says master let's click on this and let's just say new branch and i'm going to call this one documentation and create now you notice that where it used to say master it now says documentation but the nice thing is that both branches are active in this pop-up menu so we can switch between them as we want one thing i want to point out though if we go back to our repository we still only have one branch because we haven't actually pushed our new branch yet now let's make some changes and go ahead and push so i'm going to run back to idea and i'm also going to grab a bit of javadoc this is the class level javadoc so we'll put this right above the controller statement and paste and now i'm going to save right click get and let's say commit directory and we notice there's one change here and if we double click on this we can see in green it will show us what's new anything that hasn't changed is in white and anything that has been removed is in red i've done a net ad here there's nothing i've removed so we'll say add our class level javadoc and then let's go ahead and do a commit and push commit will commit it to our local file system and then push pushes it to our remote no errors we'll go ahead and commit and push now you see on github our documentation branch exists because we've pushed it you can also see it in the drop down here on the main page if we click into it you can see that the branch is one commit ahead of master because we've made this commit on the feature branch and this feature branch is separate from our master branch as a matter of fact if you take a look at our feature branch our documentation branch you see at our class level javadoc rather is a commit and then under that you have add new crd endpoints let's go ahead and switch to our master branch and you see that that latest commit that was exclusively on that feature branch does not yet exist in master okay good so at this point we could continue to work on our feature branch or we could merge our feature branch back into master or we could do several other things one thing that's interesting though is what if we're down here in our feature branch and we realize that we need a change to the master branch can we do some work into that master branch and then merge it into our feature branch well indeed we can let's click on here from documentation let's click back to master and let's say checkout notice in master when i go to plant diary controller it does not have that java dock on top of the class let's watch something interesting here let me grab some of this javadoc here create a new specimen object let me copy this and i'm going to put this over our create endpoint which is our post mapping and paste and save and now i'm going to right click and get well let's do commit directory that'll be fine and once again commit and push and commit and push is fine now one thing i want to point out on this push screen is you see the word master here that way we can confirm that we are pushing to the master branch and so the master word there is the name of the branch you can name the branch whatever you want as a matter of fact when we push to our documentation branch that's going to say documentation now origin here that's referring to the location of the remote system we're pushing to so origin is my nickname for github and that's typically the first nickname that you'll have but we could certainly make others as well let me go ahead and choose push now if we take a look at the documentation branch we see add our class level javadoc and if we open that up we'll see that we have the javadoc above the class now let's switch to the master branch i realize i made a little mistake here not fatal but i didn't change the commit message so the commit messages are the same but we can look at the source and we can realize that in the master branch i put the javadoc over the post mapping so in this point we have the exact same file in two different branches with two different changes so how do we put them together well what we can do is we can come back to intellij idea we can switch back to our documentation branch and say check out so now our work is effectively back in the documentation branch now from here let's go to master and let's look at a neat feature that we have here merge into current that means take those changes from master merge them into our current branch which is our documentation branch so we say okay and then we get the confirmation message merged master to documentation now let's take a look at plant diary controller we have the javadoc that's above the class we go down the post mapping and take a look we have the javadoc above post mapping 2 so at this point we've merged the two together if we take a look at our repository i haven't pushed anything yet so it's still going to remain as it was before where the master branch and the documentation branch have the same file with some differences we'll sync those up in just a minute but i'll tell you what let's add one more piece of javadoc which is our fetch javadoc we will put this above our git mapping that takes a specific id and then we'll save now i'm going to right click and choose get and let's remember we're still in the documentation branch we see that i'm going to commit and push this up to github so we'll say add javadoc to get mapping and commit and push now take a look here remember this is a little bit different than what we saw before where we had master and then an arrow and then origin master this time we're saying we're pushing from our local documentation branch to the documentation branch that's on our origin repository where origin is github.com now take a look there's several things that we're pushing here several commits were pushing including the merge of master so let's go ahead and choose push now if we take a look at the master branch we see that we haven't committed anything for a little while basically since we added that post javadoc so we have a total of five commits but now let's take a look in our documentation branch this feature branch and we see number one this branch is three commits ahead of master and it's kind of giving us a hint here it's saying do you want to create a pull request pull request will pull these two branches together but first of all let's see what we're going to be pulling into master so we know we had our add classlevel.javadoc we had the merge of master into our feature branch where now we're going to take feature and merge it back to master and then we have this brand new commit that we just made add javadoc to a git mapping now let me address something really quickly before we jump into this merge why did i merge from master into the feature branch and from the feature branch back into master well you don't always have to merge from master into the feature branch and you don't even have to always merge the feature branch back into master what will typically happen is you'll work in a feature branch when you're done with that work then you'll merge that feature branch into master on the other hand you might work work for a little bit and then realize that you're not getting what you want or maybe priorities have changed and you no longer need that feature so you can simply orphan the branch and never merge back into master but why would you ever want to merge master into a feature branch well many times one developer or a small group of developers will be working in an individual feature branch and other developers will be working in another feature branch and if they want kind of a checkpoint to see how they're synchronizing they can use this as an opportunity from to merge from one branch to another additionally by doing this merge from master into a feature branch it keeps that feature branch closer in sync with master and it makes the future merge a lot easier because the two are coming from a more common base as opposed to if you had written a feature branch and then the feature branch and master never synchronized for three months or some long period like that you might find that it's hard to merge because the two have completely changed so anyway let's go ahead and start this merge process and it's going to begin with a pull request now i want you to take a mental screen capture of the commit history of both of these different branches first which is why i showed them the five commits and master and the eight commits and documentation because the outcome of this merge is we're taking documentation and we're putting it into master first let's start the pull request the pull request doesn't actually merge merge any code it simply kicks off the process to merge the code so we'll start with this screen we'll leave a little bit of information like add javadoc to our controller and then we'll go ahead and create pull request and remember this is not actually doing the merge just setting us up to do the merge one really nice thing about a pull request is it allows us to have a discussion alongside of source code without actually polluting the source code with any comments now we can see the pull request has been created i can go ahead and click into the pull request and here's the neat part i click on files change and take a look you can see in green everything that's changed in white anything that's not changed in in red anything that has been removed sorry green is actually net ad but look at when i mouse over do you see the little plus here and i can click and drag and then i can place a comment that says something like really good javadoc but do you want to include a code sample something to that effect and then i can say add single common the nice thing about this is it allows us to have an interactive conversation without actually polluting the code with our comments comments that we have in code should really focus around javadoc like this which is describing the api documentation it should not be a conversation among developers conversation among developers safe here for the pull request speaking of which let's navigate back to the conversation tab and towards the bottom we see an option to merge the pull request we see one other thing that we really like here which is this branch branch has no conflicts with the base branch which means that the merge can happen automatically for us we don't have to do any other intervention we also see a little nudge here to set up continuous integration which is definitely a good idea but for now let's go ahead and choose merge and we can say and confirm merge great it's merged and we see with the purple it's been merged let's go back now and take a look at our master branch wow look at this remember our master branch used to have only five commits now it has nine commits and take a look at the sequence of events this was something that was on master this was on documentation this was on master then we took master merged into documentation we added one more thing to documentation we took documentation and we merged it back into master at this point you can see that master has all of these changes but we do still need to synchronize our local environment so let's go ahead and go back to the master branch again and say check out if we take a look at this oh gosh okay well we take a look at this and we see there's only javadoc over the post endpoint where we know we've added javadoc over the git endpoint and also over the controller well we have not yet pulled those changes down to our local that merge happened up on github remember we just did all that through the web interface on github it doesn't automatically come down to our own repository on our local computer until we tell it to so for that let's navigate to view and then tool windows and then we can say get and there's several ways to get here there's you can also just find the shortcut right down here you can see that master looks like it did before we did the pull request because we simply have our new change at our classlevel.javadoc and then the documentation branch shows that it started with master's history and then we made our own change in our documentation branch we also made a change in master and then we merged master into documentation and then we made yet one more change in documentation branch so this is kind of neat because it shows how we made changes in two different branches and then we merged them together however it doesn't reflect the current state on github so let's go back to our project right click choose get and then repository let's simply choose a poll which means update me locally from what's happening remotely now take a look here i like this command box it gives us it lets us choose our remote which we're going to stick with origin and it also lets our ch lets us choose our branch you notice documentation and master well remember we've merged documentation into master so master is currently the latest and greatest let's go ahead and pull master now look at the graph notice that it's updated since i did that pull and this is kind of cool now remember we're looking at master so we start with the commits we had when we only had master then we had the the new feature branch for documentation which was represented in purple here then we had the change that we made in master we had our merge of master into our feature branch to get those updates from master we had our latest change in our feature branch but now take a look at this this little line in this circle indicates that we took those changes from our feature branch and we merged them into master let's take a look at our plant diary controller and if everything worked right and sure enough it did we have javadoc above the class which was the first thing that we added we have javadoc above the post mapping which is what we added in master and then merge the documentation and then we have javadoc above the git mapping which we put in our documentation branch and then merged everything into master and just so we can confirm here we see all three of the changes that we made in this one file but take a look over here you see we have it all in the master branch so now what do we do with the documentation branch well we could just leave it or we could delete it since it's all been merged into master now we could delete it and clean it up for my purpose though i'm just going to leave it so i hope this hands-on view of branches and get and github was helpful and i look forward to reading your comments thank you
Info
Channel: Brandan Jones
Views: 50,090
Rating: undefined out of 5
Keywords: Git, GitHub, feature, branch, merge master, into, pull, commit, push, create, merge, request, branches
Id: jEN3D9EN7ss
Channel Id: undefined
Length: 14min 52sec (892 seconds)
Published: Mon Sep 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.