Part II of my git/Github for poet series. In this video I want to talk about a concept called "branching" or "branches". What is a branch?
Why would you use a branch? How's it work? What's all this stuff called branching? So, in my first video we talked about what is a repository and what is a commit. So now, I want to talk about a branch The first thing that I did in my first video is I had this file called "rainbow.txt" So I have this repository ... And really, actually...! I shouldn't refer to the file because this idea of a tree, or a branch, has to do with the entire repository,
not a specific file So I had a repository called "Rainbow-Poem" And I could think of the history of this project as a linear list of times that I hit "save" A commit; a commit; and a commit. So remember there were this moments that I hit commit where a particular change happened and each one of those commits has that unique identifier And you could see how it's just this linear list. But what if I had this idea one day that I had this experimental thing that I wanted to add to my poem and I wasn't really sure if I wanted to keep it? So I'm going to think of this as an experimental try and I want to branch off So I could have a branch... I want to have an experiment where my poem is also going to be about a unicorn So this, by the way, this particular branch we can think of, by default, as "master". So when you create a Github repository, this main linear list of commits, this history, is on the master branch. Almost like a root of your tree, so to speak. By default, it's named "master", although you really can change the names and do whatever you want. So, you might continue to work on that poem and make further commits along master while making some commits along another branch. And you know what, there could be a branch also called "unicorns" and... What's something else that goes with rainbows and unicorns? Fairies? I don't know. I dont know where I'm going with this. But that could have its own branches and that could branch... So you can have this crazy set of experimental... all sorts of... You could have lots of people creating different branches and working on different aspects of the same document. But I'm going to keep things simple for right now and just show you I have a master branch and I want to branch off to try an experiment called "unicorns". But let's see what happens when I do that and how we can start to do things like merge to bring changes back and forth between the branches Okay. So let me come over here and I'm back again in my github repository called "Rainbow-Poem", if you recall. There's just one text file, called "rainbow", and there's my poem. Pretty pathetic, but it's the best I can do right now. So. What I want to do is right here -- -- you can see that I'm currently looking at the state of my poem, on the master branch. So what I want to do is find -- now you can see here that I don't have any other branches so what I want to do is I want to type in "unicorn", and you can see by default it's giving me an option to create the branch unicorn. If the branch unicorn already existed, it would be giving me the option to switch into that branch. So I'm going to create that branch, and you could see now, look at this, I'm on branch unicorn. So what I'm going to do now is hit "edit" And I'm going to call it "The Rainbow and the Unicorn". Purple, pink, orange, blue, green... Unicorn. Yellow, red, these are the colors of the rainbow... And the unicorn. My poetry skills leave a lot to be desired. So now I'm going to hit "commit". Now notice -- look at this -- commit directly to the unicorn branch. I'm on the unicorn branch right now. By the way, this other option might make sense for you now Create a new branch for this commit. So any time you're making a "save", github actually gives you the option of, just, making it as a new branch which can be something that you want to do um, but I'll get to that -- why you might want to do that -- I'll get to that later. But right now, I want to commit to the unicorn branch. So I'm going to hit "commit changes" And now you could see, I'm on the unicorn branch and, I've got the version of the poem. Now, if I were to switch back to master, you could see that the version of the poem does not have the unicorn involved. And now if I go back and switch to unicorn... it now does. Now this is kind of similar -- it's a little bit trickier -- it's kind of similar to just the history of commits, right, and it is really the same thing, right, just like switching back and forth between commits oh I'm seeing the previous state or the next state, but branching is really making a more substantive, a substantial commitment to the idea of a separate path. We have the history kind of just in case but now if I really know I want to have two possibilities going on at the same time I really want to have a separate branch. So, the question becomes now... Let's now diagram what we've actually done. We had commit, commit, commit, commit... And now branch... So, master... And unicorn... What happens if I decide this experiment was a good idea, and I want to bring it back into master? Let's take a look back at what I mean by that. So over here, one thing we can actually see, by the way, is that I can go to, back to the main page of the repository, I'm looking for, ah, up here, I'm looking for a button that says "graphs". One of the graph options is to visualize your tree and branches. So if I go and click on "graphs", the one I want to look at is "network". So I want to click on "network" right now and we can see here... Look at this... You can see here, these are the changes I made and then I branched off, and I have this unicorn so master has not gone forward since unicorn. By the way, notice this one, "patch-1", green, somebody else watching this video has decided a username on github named "b8horpet" has decided to make their own branch. And potentially, they could contribute the changes back to my particular branch. But we're going to get to that in a future video but you can see this full diagram. So, let's say, what I want to do is have unicorn go back into master. This now brings up a new term -- I'm adding to our list, something called a "pull request"... and a "merge". Oh boy. Suddenly we added so much, just in this one video. So... "pushing" and "pulling" are important terms in github; "pushing" kind of being like sending something to somebody, "pulling" being, pulling something in from somebody. In some ways they are almost interchangeable. Actually I'm going to save that for a later topic a bit more, but what I want to do is, essentially... My unicorn branch says, "hey, will you please --", the request is the key term here, "-- will you please take my changes, and pull them into master?" So what I want to do, I'm the same person, so I'm really just making the request to myself, later you will see how we could be two different people, but what I'm doing here, right now, if I go back to the main page... Look at this, actually github is saying, look at this, -- oh I'm in the wrong place -- github is saying, look at this unicorn branch. I can press this button "compare and pull request" meaning I want what I just did in unicorn to go back into the main branch. So I'm going to click that button, and I'm going to say, "Hey! I added some unicorns." So now I'm making a pull request, meaning a request to merge those changes I made in the unicorn branch, back to the master branch. And I'm going to say, "hey, I made some unicorns." I could write information about it. I'm going to do "create pull request"... and there it is. Now, what did it do, it actually created a, a thing called a pull request, and what is a pull request? A pull request, again, is saying, take some changes from a particular branch, and bring them into another branch. And, one thing that it's doing github -- git automatically behind the scenes -- and github for you -- is looking at it saying, "oh there's no conflicts I can see what was changed here, I can see what was changed here, and I can bring those changes together, no problem. And git is actually quite smart about this. You really only ever run into an issue if you're editing the same exact line in the file. But two different branches could edit different lines and merge those together very easily. So, all I have to do now is complete this by clicking the "merge pull request" button... and I can say confirm that merge and then, suddenly, if I go back to "graphs", under "network" you can see, oh, you can see that happened now after I hit refresh. You can see that "unicorn" was merged back into "master". So your unicorn branch is now somewhat irrelevant in that its changes were merged back in. Let's try to do something a little bit kooky and let's make some changes in both master and in another branch and see if those could be merged. And that's really kind of going to wrap up the end of this video tutorial. Okay, so I'm going to go back to my poem... And I'm in master right now... and, oh, I'm going to make a branch I'm going to call it... um... I'm going to call it... ah, I can't think of anything all of a sudden. Um, I'm going to call it, um, "ponies"... it's kind of like unicorns... Oops. Branch ponies -- I don't if that's how you spell ponies -- and I'm going to say, "and the unicorn and the pony", and I'm going to say, "pony", and then I'm going to commit that, and "adding ponies". I'm going to commit that. And now what I'm going to do is I'm going back to master and I'm going to edit it And I'm just going to add another color. Yellow, purple, green, pink... orange, blue... indigo... I'm going to add that, "adding indigo". I did it in a completely different part of the poem. Now I'm going to go to the graphs and I'm going to look at the network. And you can see here... Look, unicorn had some changes, went back in, ponies had some changes, but then master went ahead. Can I merge those two, they're in completely different states. Let's give it a try. So I go back to here, I'm going to "compare and pull request"... No problem, able to merge. Create pull request... Merge pull request... Confirm merge... I'm just doing this quickly, and let's go back, and look at that network again... And we can see, here's what it looks like now. So you can see how git and github is very powerful for working on multiple instances; these branches can actually be different people doing different things, trying out different experiments... This is an exciting set of possibilities that as I continue to show you more and more, hopefully will set the foundation for doing interesting things and collaborating with other people on github. So. If you're looking for something to try now you have your poem, try making lots of different branches, trying editing, making commits in one branch, making commits in another branch, making commits in master, see if you can do some pull requests, see if you can do some merging, see what happens, see where you go wrong, ask your questions in the comments. And, uh, in the next video -- I forgot what I'm going to do, but I have a plan -- I think I'm probably going to start looking at how you could actually start to be a different person, and two people can collaborate on the same repository. Maybe. Or something else. You'll see.