Collaborating using Git and GitHub | Branches, Pull Requests, Merging vs Rebasing

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi i'm luna hi i'm jen and in this video we're going to help you understand how to collaborate using git and github so this is going to be a very practical workflow where we show you how to create new branches switch between branches merge your branch using pull requests get merge and get rebase as well as how to deal with nasty merge conflicts if you haven't already make sure to watch this video on the basics of git and github this covers the basic git workflow which we're going to be building on in this video and last thing before we jump in if you like this video please consider subscribing you can always change your mind later [Music] so first let's talk about branching and get so branching is a way to create a unique set of code changes in a separate workspace in your repository so generally when you are working on a project with many features you should create a branch for each contained feature so these feature branches are usually merged into the main branch once your changes are finalized so this makes collaboration much easier because everyone's working on their own edits in their own future branch and only pushes changes to the main branch when they're finished first i'll show you guys what this looks like on github so this is what you'll see when you pull up the repository on the github website on the top left corner you'll see a drop down menu showing the current branch that you're currently working on and once we have other branches that are active you'll also be able to switch between branches using this drop down menu and then right next to it you'll see the number of branches that are active in your repository so right now there's only one active branch which is the main branch this is also sometimes referred to as the master branch so those two terms are used interchangeably but this is going to be your default branch for this repository and basically anytime you have another branch you'll be merging into the main branch okay cool so now that we've seen what branches look like on github let's switch to looking at it in the terminal so first we want to use the command git branch to see what branch we're currently on so you can see here you'll see that we only have main kind of like jen mentioned and that's highlighted in green since that's the branch we're on if we want to create a new branch in this same repository we can use the command get checkout dash b and then whatever your branch name is going to be so i can just name my branch luna for example and now we've created that new branch and switched to it so if we use the command get branch again you'll see that we have two branches this time um and it's called our current one's called luna that's highlighted and the other one is main and jen's doing the same thing right now with her on branch also um so now let's make a file in our branch so if we do uh sorry touch um let's just call it luna.text and then we can add something to it say hi and now kind of like our previous git workflow we can just add that and commit it okay so now that we've added committed we want to push right but if we do get pushed you'll see that we get an error here that says our current branch has no upstream branch and that just basically means git doesn't know yet where to push kind of our changes from this local branch like what remote brakes it's pushing to so all we need to do is do that command it gives us so it's get push and then set the upstream and origin basically means it's referring to the remote branch and we say the remote of luna and you basically only have to do this once any time you create a new branch once you do that and you do get push after that it won't ask you for it it's already set that upstream um so yeah now that you've done that we can see that if we do ls or new files there if we were to switch back to main um and look at the files there you'll see that that file is not there so the branches the changes on the two branches are completely separate and now we'll look back at how that looks on github so on the main branch you'll see that it still only has what we originally had which is just the hello.txt file but now when you click on the drop down button you'll see that the gen branch also exists and also the lunar branch so here you can switch into the gen branch and kind of see the specific gen.txt file that i created as well as the luna branch which shows you the file that luna created and again if you click on this icon it'll tell you all the different branches that exist and also the ones that you are currently pushing towards so this will be under your branches now you'll also see that if you are working on a branch and you made recent pushes it'll prompt you to do a pull request so i'll hand that off to luna to talk about how to create a pull request on github yeah so now we're going to talk about what a pull request is and how you can make pull requests um and use them so you can see here like jen mentioned there's a new button that pops up that says compare and pull request so if we click that um you can start making a pull request and basically what a pull request is is it's just a way of like letting other people know what changes you're gonna be pushing to the main branch um it's really good for allowing like more discussion about these changes and just keeping a log of all the like features you're pushing um and changes you're making so you don't push them directly to main but you go through more of a review process through making pull requests so you can see here this is kind of what making a general pull request looks like it's best practice to write a really detailed title and description for the pull request to make sure to explain like what changes you've made and what you're merging into maine so that other people know exactly what you're doing and they don't have to take extra time to figure out what changes you're making so we can say here adding a new text file um setting a file called that text that we need in the main branch example to try to be as detailed as possible even though this is pretty simple um pull request and then another good thing about pull requests is you can add specific reviewers so for example i can add jen here and ask her to review my pull request so that'll actually tell her that she's requested as the code or viewer so now i can create that pull request so you can see here this is once the pull request is created you'll actually see that if you keep adding any commits to your branch those will show up here so it's not necessarily fixed you can keep adding to a pull request even once you make it and you can see a log of your commits there an important part of this is also looking at the files changed um this is basically going to show you what the difference is between what's on name now and what changes you're pushing yeah and now jen's going to talk a little bit about what it looks like to be a code reviewer so once that pull request has been created i'll see that there's a notification in the poll requests tab so if i click onto that i'll see the pull request that luna just created with the title that she wrote so if i click into it i'll be able to add a review so if i click on the add review button it'll show me the files that are different and also the different commits and i can click review changes to add a comment approve the changes or request that luna makes more changes onto the branch so for right now let's do great file and approved the pull request so if i click approve and submit that review we'll see that in the timeline here you'll see the commits that luna made as well as requesting a review for me and then with my comment um for the review so now that that's all finished um since this branch has no conflicts with the base branch we can complete the pull request by doing a merge pull request here you can also add a more descriptive title if you want to so we could do merging luna.txt to main branch and then confirm that merge to finish off that pull quest so pull requests pretty straightforward that way another great thing about them is that you can actually customize what your repo looks like for doing pull requests and that kind of thing so you can add things like branch protections so that would mean not allowing people to push domain unless they're making a pull request or kind of requiring a certain number of code reviewers or even requiring automated tests to run that pull request so i'll have to pass in order to be able to merge them so there are a lot of powerful tools here and i think most large teams that use github will use all these features because it helps keep your repo clean keep a good log of like all the changes you're making that kind of thing so definitely check those out if you're looking to do any big projects or work with a big team on github so pull requests are integrating your branch's changes into the main branch but let's say that the commits in maine are relevant to the feature that you're working on to incorporate new changes in the main branch into your branch you can use git merge or git rebase so right now i am in my personal gen branch but just now we had completed that pull request in main so i know that main has a bit more commits that my local main has so let's go to main branch and pull those new commits it's always good practice to pull from the remote branch as often as you can to make sure that everything is up to date so the first option um we'll be talking about is going to be rebasing so rebasing essentially moves your entire branch to begin at the most recent version of main this means that instead of using a merge commit rebasing actually rewrites the project history by creating new commits for each commit in your personal branch the benefits of rebasing is that you have a clean linear project history this means you're not going to see extra commits like in your commit history when you rebase but the trade-off there is that it loses kind of traceability so you won't be able to see exactly when you rebase another thing with rebasing is it can be kind of confusing or tricky to get right i think we're doing a pretty simple example of rebasing here but we'll probably do a more in-depth video in the future um because there is a lot to go over when it comes to re-basting so let's check out our personal branch again and to do the rebasing command it's going to be get rebased so once we do that it says we are rewinding head to play your work on top of it this just means that we are going back so that our start of the gen branch is starting at the last commit of the main branch so after that applies the one commit that i have which is adding the gen file so if we do ls then you'll see that the luna.txt file now also exists in my local branch so this means that the rebase went through correctly and everything is up to date so the next thing we'll be looking at is merging and with this we will also try to show you guys how to resolve a merge conflict so luna is going to add a line to the hello.txt file and i am also going to add a line to the hello.txt file let's do hello and luna so i've updated that and i will add that and commit so now we have a change in hello.txt and i'm going to go back to the main to pull the change that luna created so luna also updated hello.txt so first we need to switch back into our gen branch and once you do that you'll see that an error pops up saying that our remote branch and the local branch have diverged this is because when we were editing our hello.txt file before we did not push our changes and so the remote gen branch and the local gen branch have different commits so this is going to be a version of merging where you use git pull to merge the remote branch into yours so when we use git pull a merge commit will pop up saying that's merging the remote branch into our local branch you can add a commit message in here or you can just leave it as is and once you exit that it will say that the merge was made using a recursive strategy this just means that the remote branch and the local branch are now merged together let's do git push to make sure that our edits of the hello.txt file is pushed onto the remote branch so merging creates a new merge commit that ties together the histories of the two branches using the most recent versions of each branch so the benefit of merging over rebasing is that the branches aren't individually changed at all but the trade-off is that your branch's history can get really clogged so you'll see um merging ads these commit messages that are like merge this branch into this branch okay so let's try the merge command so git merge main so here you'll see that there is a merge conflict in the hello.txt file so there are a couple of ways to resolve merge conflicts the way that we'll be showing you guys is using a text editor so here i am using adam and when i click into the hello.txt file what you see highlighted in purple is r changes which is on our branch and then what's highlighted in blue is the changes in main so here you get to choose basically what version you want to use so let's say we want to use the version for main which says hello this should create a merge conflict so we're going to save that and then we're going to go back to our terminal and add that file and then commit resolving merge conflict so this commit essentially merges the two branches together and so if we do get merge main again you'll see that everything is up to date now we can use git push to make sure that our local branch is synced up with our remote branch and so when we look at the hello.txt file the version that we selected will be the version that exists now in our branch so most code editors will have a way to resolve merge conflicts that's pretty similar to this i personally usually use vs code there's also lots of tools that will let you do this in the command line if you prefer we'll make sure to put some links in the description so you can check a few different ones out thank you guys so much for watching this video on gift branching we really hoped it helped you guys um give us a big thumbs up if you enjoyed this video and don't forget to subscribe if you enjoy our content so we post videos every week make sure to comment if you have any questions at all for us or if you have a topic you want us to cover in the future see you guys soon [Music] bye you
Info
Channel: Decrypted By Us
Views: 591
Rating: undefined out of 5
Keywords: git and github tutorial, git and github tutorial for beginners, git branching and merging strategies, git branch tutorial, git branch and merge tutorial, git branches explained, git branching tutorial for beginners, github for beginners, github pull request tutorial, github pull request branching merging & team workflow, github pull request review, git merge conflict, git merge vs rebase, git merge branch to master, git merge conflict tutorial, resolving merge conflicts in git
Id: _wQdY_5Tb5Q
Channel Id: undefined
Length: 15min 33sec (933 seconds)
Published: Mon Jan 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.