Check out a free preview of the full Everything You'll Need to Know About Git course
The "Ours & Theirs" 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 discusses the differences between "ours" and "theirs" when utilizing rebase and merge. He also demonstrates how to resolve a conflict by selecting either "ours" or "theirs" changes.
Transcript from the "Ours & Theirs" Lesson
[00:00:00]
>> So we're gonna to talk about Ours and there's you've heard me say this multiple times. Now Ours is Theirs and Theirs is Ours with rebase. Whereas it's the opposite for merge, so long as you have one of those correct, you'll get the other one correct, or you can just remember how rebase works.
[00:00:11]
So sometimes you just wanna be able to pick just one side. You don't wanna have to go through and manually resolve. Each one of the things you just wanna say, hey, file or change A is better than change B, I just want change A, right? By the way, there's a very well-known Netflix person named some coding guy, and he even says that Ours and Theirs is like the worst thing to get.
[00:00:35]
So it confuses everybody. So if you feel kinda confused about it, don't worry, you're not the only one that thinks completely ridiculous and also makes perfect sense, but ridiculous. All right. When I say ours, I do mean our current branch. When I say Theirs I do mean the incoming branch.
[00:00:52]
So if you wanna select a change, you can just simply do Git checkout-- hours path to the file and it will just select the entire files change set. And if you do Theirs, it'll select the entire file change set for just Theirs and it's only for that file.
[00:01:10]
So, let's do it again, let's make another conflict. We'll do A plus 7 and Hello, A plus 8 and remote and let's do a merge from upstream to resolve it and let's choose hours with a merge. So I'll go to Hello, and then the ReadMe, A plus 7 A plus 7.
[00:01:33]
Do it again here. There we go. I've done both to Hello and to remote. Now all I need to do is just simply merge those two together and then we'll get the conflict so Git pull. Notice I'm using a merge, I'm not rebasing awesome. I have the conflict.
[00:01:53]
Now I'm just gonna simply go Git check out Ours ReadMe. So if I do that, and I go get status. You'll see it still says, both modified cuz we haven't added it yet. If I cap the ReaMe, you'll see it's just our change. It did it for us.
[00:02:09]
That also means if the remote had other changes that weren't conflicted, what is it gonna do?
>> Still pull them in.
>> It's gonna pull in everything on Ours. So, just think things to remember, choosing Ours, you're choosing this experience versus that experience. So just remember that. It's the full deal here.
[00:02:31]
So sometimes that's why you wanna go through hand by hand, select what you want. There we go, pretty straightforward. We checked out hours, now all we have to do is just simply merge it. So I'll just simply go Git, add this, Git Commit, let's see, merged, why not?
[00:02:46]
These commit messages are just getting worse as the day goes on, which really represents true using Git. This might be the most true version of using Git is where your commit messages disproportionately get worse as time goes on. It's just the worst, right? All right. So we can do a quick little log.
[00:03:04]
I can go get log one line graph and we'll just do like the last 10 commits. And you can see right here, you can see the A plus 7 over here, but then we just merged in A8 with A plus 7, we get the merge. So there we go.
[00:03:18]
I mean, I always go back and forth in some sense of whether I did. Do rebase or whether I do merge. In some sense I do this piece of history right here, which is I get to see that these two things happened, and we did make a proper change to the code in such a way and this is the result of the conflict.
[00:03:36]
I do that experience. I think it is a little bit more sensible for me to understand how a bug could form. Cuz if you have a conflict and you use rebase, it kinda erases the idea that you even had a conflict and it just looks like linear history.
[00:03:50]
It's kinda like a you choose your own adventure. I always go back and forth. I can convince myself either way to love either one, right? As for whatever reason, I don't seem to have a solid like, this is the only way to do it here. It's just like, you can visit me.
[00:04:04]
Tomorrow, I'll be all about rebase, today I might be all about merge for conflicts. It's the life I live as an engineer, it depends just continuously and ad infinitum for all of the rest of my life. Anyways, good reminder. If you don't wanna mix merge and rebase, start getting these Commit merges and it's very hard to keep your tree kinda straight so you really do wanna try to stick with one strategy and at least make sure they're in sync and then you can kinda diverge once they're in sync, but I tend to try to never mix them both.
[00:04:36]
Cuz then you just start getting merge commits constantly and just nothing feels good it's just trouble after that. So, let's merge everything up, and make sure that our Hello Git and remote Git are in the exact same position. And then we'll start doing the next part. Okay. So Git, push origin, I don't know why I'm saying the whole thing here.
[00:05:01]
Again, exactly got owned again. Get checkout bar, go back, push it up, go back, get checkout. Trunks I don't goof that up. All right. We're in. By the way, if you guys are wondering how in the world am I switching between projects so dang fast? This is called tmux.
[00:05:18]
I have an entire course on front end Master is all about how to use your system well, includes Vim and all the good stuff. If you don't use Vim, that's okay. Vim's awesome. It makes me entertained. I know most people don't like it, but hey, I think it's great.
[00:05:32]
All right, so we're gonna do the exact same thing again. We're gonna create another conflict with this A plus 9, A plus n add, add, by the way, forgot to say that, there we go, add SEC rules now, wait, FEC, which one is it? Anyways, one of the governmental agencies, you have to say add after things, I don't know.
[00:05:50]
Anyways, all right, so let's do A plus 9 in Hello, let's do A plus 10 in remote, don't laugh at me, [LAUGH]. And do those two, and then let's do a rebase, and then let's try to select Ours. Let's just really drive home the point that Ours is Theirs and Theirs is Ours.
[00:06:06]
Even though we should already be there, but let's just do it anyways. So I'll go back to Hello, I'll open up ReadMe, set it to 9, Git add this, there's no file name there, Git commit, A plus 9, switch over here, then ReadMe, 10 Awesome. Okay. So now if I get a whole, Rebase, we have the conflict, get check out Ours.
[00:06:38]
Remember, we're doing Ours here on purpose. If we do that, and then we can Cat out ReadMe you'll see right here. We selected Hello Git, we did not select Ours cuz we selected Ours, which is not Ours. Okay, good. I just wanna really make sure we, we really are all fully on the same page here, so it's just such a goofy part.
[00:06:58]
So there we go. So that means if you're gonna do this, we need to select Theirs. So I believe I do a little change right here. We do this whole Ours as Theirs business. Okay, so let's just check out and use Theirs instead of Ours. I'll go Git checkout Theirs ReadMe.
[00:07:14]
There we go. So now when I cat that one out, you'll see it's our change. Awesome, Git add it, Git rebase continue. Fantastic. We've now done it, we've selected Ours, and we have our changing.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops