Merging - Git for Android Developers - Part 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to a new video so as i said in the last video in git we often create separate branches to implement single features but in a real app we of course usually at some point need all of those changes we made in those single branches and have them on a single branch so if we have five branches that all implemented a single feature then at some point we need all of those features on our master branch for example when we want to release our app we need all of those changes from our separate branches on a single branch so how can we actually take the changes from one branch and get those changes on another branch the answer is called merging so we merge one branch into another and let's actually do that because if we take a look in our version control tab here in the log tab then you can see we have our experimental commit here that we are currently in so make sure you're currently in that experimental commit and check that out but what you can also see is that this experimental commit is not contained in our master branch which you can easily see at that green thing here that tells us master this is on another commit then this yellow thing and those other two green things because we still have our youtube branch we're currently in and that youtube branch is on the same level as our experimental branch which you can see that all of those things here those banners or what that is are on the same commit but not our master branch banner so how can we actually get those changes we made on that experimental branch or on that youtube branch we're currently in and merge those changes into our master branch so our master branch is basically up to date and this time i want to show you that from android studio first so not from the command line because this is much more intuitive in my opinion and really helps you to understand all that merging process so first of all take a look at our code here we have three print line statements and if we now check out our master branch by going to the bottom right corner here clicking on master and then checkout then we switch to the master branch and you can see here we only have a single print line statement so what we want here is we want to get the two other print line statements that are not contained in our master branch and merge them into our master branch so we also have them here and in android studio we can do that from the vcs tab up here click on that go to git and click on merge changes and really make sure that you're currently in the branch you want to merge the changes into so we are currently on the master branch click on merge changes and here you can see we have two branches to choose from so we can either merge the changes from our experimental branch or from our youtube range into our master branch and i'll choose youtube branch because that was the brand we were in before and the rest can be ignored for now and now we click on merge and then you can see the changes from our youtube branch are now inside of our master branch so now we are inside of our master branch and we have the changes from our youtube branch and you can also see that in the version control tab here now we have our experimental commit and it's basically up to date with our master branch but why are so many people actually so afraid of merging that changes because like you really often hear that that this is one of the most complicated features for some people in git but it's actually not that complicated but i understand that anxiety because you can really up your project with that so what we did right here was very easy that was a very simple merge but let's imagine we changed the same line on our master branch and the same line on our experimental branch on our youtube branch and we would then merge our youtube branch into our master branch how would git then know which line we want to keep because both lines were basically changed and what android studio did here when we used that default merging strategy it will just take the changes from the merge we are merging into and basically add them to our master branch so if we would have a line changed in our youtube branch but the same line wasn't changed in our master branch then it would just override them but it can't tell which line to keep if the line was changed in both branches so let's actually change a print line statement here for example that experimental we change that to change one we're currently in the master branch and we commit our changes and call that commit change one commit click on commit and then we switch to our youtube branch again so bottom right corner youtube branch checkout and here you can see we still have that experimental line of course because we committed that line in the master branch and here what we will do is we will change that to change two so a different line than from the master brand commit this again this time on the youtube branch call this change to commit click on commit and i can also see here in the version control tab that we have our change one commit which is on our master branch and our change to commit which is on our youtube branch so those are two different commits of course and now we want to merge that youtube branch into our master branch to do that we need to go into the branch we want to merge something into so our master branch click on checkout and now we go to the vcs tab again go to git and merge our changes now we can select the youtube branch here to merge the changes from our youtube branch into the master branch now click on merge and you will see oh we have a conflict and this is what people really mean when they talk about that they are afraid of merging their changes so what git will do now is it will take all of those lines that were changed on both branches so where it doesn't know how to merge them together show them to us and we need to decide now which changes we want to keep so which change we want to have on our master brand and to do that we click on merge here and then you can see a window opens up with three editor tabs basically and you can see the editor tab in the middle is our result tab so that is how our master branch currently looks and on the left side we have the changes for master so those lines we changed in the master branch our change one line here and we have on the right side our changes from our youtube branch so our change to line and now we need to decide which changes we keep so in the middle we currently only have that experimental line and let's say we want to keep the changes from our master branch what we can then do is we can click on that little arrow here so it will apply those changes to our result to our master branch and if we would now click on apply here then we would have resolved that merge conflict and it would take the changes from our master branch and that is basically how our master branch will look afterwards but if you don't want that change one here then you can simply press ctrl and that to undo that and also do the same for our change to here so if we want the changes from our youtube branch we click on that arrow instead apply those changes to that result here and yeah then we basically have the changes from our youtube brand but what is also possible is to just have both changes we can now click on this arrow here then you can see we have both changes in our master branch but i'll undo that again and let's say for now we only want the changes from our youtube branch because otherwise the merging wouldn't make any sense in this case but of course in real project you usually have much more of these merge conflicts this is just a single one here and of course in a real project this is also not that easy to decide every time because let's say you accept that change one here and that somehow it doesn't work with the other code in the master branch and you don't know that at that moment here then this can really break your project and that is the real reason why so many people are afraid of merging and i remember once we had a software project in university and we worked with nine people on that project and every person basically had its own brand there so for the feature the person was currently developing for example the upload file branch that was a little social network we did there and at some point you will get to the point where you have to merge your changes because we had a real customer there in that project that was a really cool project and of course the customer wanted to see the project state and for that we needed to merge all of our changes and you can imagine how that is in a software project with thousands of lines of code and with nine people when nobody wanted to do that nobody wanted to merge the changes because nobody wanted to be responsible if he basically up the project with those merge conflicts here so we ended up doing that somehow together and i remember we up so hard with our project that we basically deleted all of our changes from our single branches with those merge conflicts i don't even know now what we did there but basically nothing worked and we up our own project but still we could recover that with a good so even if that happens to you good is such a good software that you can even if you up with git you can still recover your changes so that's just a little story from my hand why people are so afraid of merge conflicts i can really understand that but it's still something you need to know if you work with git and if you get used to it it's not that hard anymore and all your changes are always saved with git but yeah let's get back to our merge conflict here that is not that hard to solve luckily and we now apply the change from our youtube branch so we take the change to line in our master branch and not the change one line if we don't want to change one line we need to click on that little x here to ignore that basically and then you can see all changes have been processed and that means we can now finish the merging process by clicking on apply let's do that and you can see if we are on our master branch we are we are now have that change to line here and here you can also see what git does behind the scenes it created a commit here where we merge our branch so it just basically included all of those lines from our one branch into this master branch and just committed that that is how it manages this and that is already the whole magic of merging let's actually see how we can do the same from the command line as promised let's switch to our youtube branch again click on checkout here and simply add a print line statement here this line will be merged then we go to our terminal here actually we commit our changes first of course um let's call this i don't know changed recommit doesn't matter click on commit then we go to our terminal here clear that up use git checkout master to switch to our master branch you can see that worked and now that we're inside of our master branch and we want to merge another branch into this master branch we just use git merge followed by the name of the branch we want to merge into this master branch so that is our youtube branch click on enter and you can see the merge was made and if we now click into our editor tab here you can see here's our line this line will be merged and there are no conflicts of course because we just added a single line and didn't change um the same line on both branches and you can also see that the merge was made with the recursive strategy i personally don't know that much about those strategies but that is just basically a rule for git to decide how to resolve merchant merge conflicts because they can appear much more often but for android studio the rule is basically if we set that to default that our line on the master branch was not changed and the line was changed on the other branch then we will just take the new changes only if both lines were changed then there will be a merge conflict basically but this is somehow another strategy here you can find so much about that in the internet but i also never really needed those strategies in detail just know that they somehow decide how merge conflicts will be automatically resolved by git but let's actually get to the scary part let's create a merge conflict inside of our master branch right now we change this change to line to change three commit those changes let's do this this time from the command line hit commit and attach a message here um change we already have that i think i don't know just final merge change one press enter and we forgot to add that file we first write here at and a dot after that and then we can commit them afterwards you can see now that worked now let's switch to our youtube branch check out youtube branch press enter oh of course git check out youtube branch now we switch to youtube branch and we can make another change here in that line so change four commit that line first add that file git add dot and write git commit minus m and call that final merge change to press enter commit those files and now if we merge that this youtube branch into our master branch we will have a merge conflict so let's actually clear that up and switch back to our master branch git checkout master and if we now write git merge and we want to merge our youtube branch into our master branch press enter you can see we have a conflict and we need to fix our conflicts and then commit the result so that is basically what we need to do here and if you now click into our editor tab you can see that git marked that merge conflict basically so the line it's not sure about which version to choose from so we can either choose this version that change three line or that change four line or just both of these lines let's choose the the change four line and we can simply remove the rest here delete that delete that and now simply commit the changes as get set so get commit and we have to add that first git add dot and then git commit m um merged youtube branch into master and press enter and then you can see we merged the youtube brand into youtube range into master so yes that was already the whole magic of merging if this was understandable for you please tell me below and also leave a like and of course subscribe to my channel if you haven't already because 53 of those who watch my videos haven't even subscribed to me it's just a single click and you will get regular android content every second day as usual have a nice day see you next video [Music] bye [Music] you
Info
Channel: Philipp Lackner
Views: 17,981
Rating: undefined out of 5
Keywords: git, merging, merge, branches, commit, push, pull, android, android studio, ide, jetbrains, easy, tutorial
Id: T-lh2YMXO6g
Channel Id: undefined
Length: 16min 32sec (992 seconds)
Published: Thu Aug 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.