Check out a free preview of the full Everything You'll Need to Know About Git course

The "Rebasing Practice" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to create a conflict using the rebase command instead of the merge command. He makes changes to the "Hello Git" and "" files in both the local and remote branches, and then performs a rebase on the remote branch. He also discusses the potential danger of using rebase, as it can result in the complete removal of a commit if not handled carefully.


Transcript from the "Rebasing Practice" Lesson

>> All right, so let's create another conflict via rebase instead of merge, so I added some bolts here, just so I don't goof it up, cuz I kept goofing it up when I do it by myself. So first we need to create a change in hello-git, we're gonna go from A+2, to A+3.

All right, so we're gonna start in hello-git I'm gonna go to the README, and I'm gonna go A + 3, fantastic. Then, we're gonna create another change in, the last line, okay? We're gonna go to the last line, so, I'm gonna go all the way down and go last line from hello.

This is probably not what I put in the course, but you can follow along. Now I'm going to go git status, you can see those two changes, I could go git diff to look at the two changes if I wanted to, okay good. A 3, add it to the last line, fantastic, git add this, git commit, let's go I don't know what to call this thing, a change from hello git for rebase conflict enjoy your day.

Very nice message, no it's sort of making me happy when I read it on the other side. All right, so now we're gonna do the exact same thing in remote, I'm gonna change A +2 to A + 4. And I'm gonna make a change in bar, but this time to the first line, not the last line, the first line, this is important.

All right, so we'll do it again, we'll increment that twice, go to bar, and we'll go first line change bar, we can see it right there. We can diff it if you want to, A +4, first line awesome, add in here, git commit, and then we'll go change further from remote for a wonderful rebase conflict, awesome.

Just makes me feel so happy doing it that way. All right, so now that we've done that, we're gonna rebase remote git's trunk with hello git and create the conflict. Now, remember, that means you gotta go git pull rebase, long as you don't have the option, auto-magically pre-enabled, this should just work now.

There we go, we have this wonderful error message, but look at it kinda closely, what do you see here? I see, okay getting some information from hello-git, we're gonna auto-merge README, there's a conflict in README, we're gonna auto-merge bar, no problems. Bar was It's nicely merged, the README could not be merged, therefore we have to manually do this.

All right, so let's check out the conflict, so if I go get status, you'll see right here that bar is in the green because It's ready to be committed. It was able to merge it, nothing you need to worry about, you're good to go. But this one has the both modified, this is what I was talking about with the whole both modified thing where you need to resolve it.

We just simply have to go through and do the exact same thing, let's fix the README all right, so I'm gonna to go vim README. I want you to tell me something that's different than what it was with merge, what do you see that's different, how about this one?

I'll go back here and I'd like this, cat README, A + 3 right, why does it say HEAD, is a plus three? That's upstreams, that's not our change, that's upstreams change, why is it HEAD?
>> That's where it currently is.
>> In rebase, when you rebase, you check out the branch are rebasing on, then you play your commits one at a time.

Then you check out the branch from whence you came and then it's updated and nice, right? So you gotta remember that, so it's inverted when you're looking at rebase. Ours is theirs and theirs is ours, it's that easy, by the way, this is the one part of git that's just like it's if you know how rebase works, it makes perfect sense.

And even when it makes perfect sense it's still stupid, it's how I feel about it, why you got to be like that, you know what I mean? You got to reverse it, and the worst part is that once you remember this and you go like this. Okay, rebase is the opposite one, you're gonna be like dang it, is merge theirs and ours, or is it ours, then which one's rebase?

Then all of a sudden you reverse uno yourself and you're just like, dang it, I can't remember, so just remember how rebase works. Instead of trying to remember some cute way to remember that it's ours and theirs. If you just remember that rebase first checks out the branch you're rebasing and then Plays on top, it's very easy to always remember it.

So just kind of a little, that's four years of me being confused about theirs versus ours and then finally just reading it, and then you don't reverse yourself anymore. Let's see, let's choose our conflict in this case, let's choose hello gits change. So remember, because we're currently checked out with hello git, so I'm just saying it from gits perspective.

Let's commit with a hello git, let's not commit with remote, so we'll go like this, we'll erase that, that and that, there we go, A +3, I want the A+3 one. Now don't goof this one up on accident okay, you'll notice right here that it says, to be able to fix these conflicts, you do git rebase-continue.

If you actually do commit, you've seen me do it a couple of times, which I haven't explained it yet. You can git reset -soft HEAD~1 and you can undo your commit, and then rebase-continue I'll show you that in a little bit, but there we go. So now I can go git add, there we go, now notice that it went away, why did it go away?

Silence, is that the reason, just a strong silent type? No.
>> It's up to this change.
>> Yes, it's because remember right now we're in perspective of hello git, we're in perspective of our upstream. Therefore, we accepted upstream's change, therefore, there's no change right now. I know it's very frustrating to think about these things, but it's good kind of exercise to go through, now you kind of know okay, I know, it's very easy to goof up.

So now I just simply go git rebase continue, now we get this opportunity to edit the message to say hey a wonderful nice rebase conflict I'm gonna save it. And there we go it's beautiful where our branch is currently ahead of origin trunk by one commit awesome, if we do a little log one line.

You can see right here, we had this wonderful change from hello git, then we resolve the conflict, but there's no conflict commit, right? We just simply resolve it and continue to our rebase, which means if I were to add in a dash P, you could see we have the first line change to bar.

And that's it, we don't have the A+4 anymore, we literally erased it out of existence. I know it's a little emotionally painful when you think about it, so this is some of the dangers of rebase is that you're fully removing something right now. Because you moved forward your commit, then you edited your commit in such a way that you removed your change.

Change did not exist anymore, and now it's just this change you had left.
>> It's somewhere in the reflog still, right?
>> That change? We could probably find it in the ref log for sure, so if you did a git ref log wherever it is, this one right here is my guess, this looks about right.

A change from remote and wonderful rebuzzing, so if I go like this, git log p this, yeah there it is, so you can see it right there, so we can get back to it. It's just since there was no conflict commit, you lose that information. So this would be a good argument for why you shouldn't use rebase, this is probably the best argument honestly, I don't think you'd get any better than this one.

Just because I don't like the idea of losing information but at the exact same time it makes perfect sense if you erase your code.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now