Check out a free preview of the full Everything You'll Need to Know About Git course
The "Resolving Conflicts with Merge" 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 use Git stash to save and retrieve changes when pulling in new changes from upstream. He also covers merging and squashing, which allows you to combine multiple commits into a single commit with a custom message. The lesson also touches on conflicts and the importance of manually editing the diffs to understand why conflicts can occur and how to resolve them.
Transcript from the "Resolving Conflicts with Merge" Lesson
[00:00:00]
>> Beautiful. Okay, so I have my remote Git set up, but now we got this call productions down, there is a change you need to make, or there is a change you need to pull in, from our upstream. So, we're going to attempt to pull it in with an active change.
[00:00:18]
So let's start there. So do a git pull with the, right here. Get pulled Wait a second by miss one thing that it should say, hey, you don't do that. I think it's probably because I have rebase automatically on, so I think I accidentally had my little change in where I had rebased true, so I was causing a bit of a, you cannot pull with rebase.
[00:00:38]
So, oops daisies, right? And so since I have an active change, it's not letting me do those changes, so whenever you try to pull and you have active changes, it's gonna tell you, you can't do this. So we need to be able to stash. So we'll just go, we'll just go like this.
[00:00:50]
We'll do another one of those echo upstream change into here, git add this, git commit upstream change. There we go. We have the upstream change in hello. Now we go to the remote and we can go echo downstream change into the read me. So now we have something in the read me, and we have a change we need to get from hello.
[00:01:11]
What are we going to do now? We are gonna use stash, right? So this is what you do at this point. You'd wanna stash any of your changes to prevent any errors from happening. And then you're gonna want to be able to get the changes and you're gonna wanna be able to on stash, right?
[00:01:27]
So this is typically what you're gonna want to do. So, right now before we do anything, let's stash our changes. So you can experiment by going get stash And then go get sets. Your changes are gone, right? Don't pull yet don't pull. You can view your stash list.
[00:01:51]
This is a good kind of activity to get into viewing your stash list because it's really nice to know that you can even do that. So again, Git stash lists So if you ever get into this, you will know. So we have this work in progress that we've stored right here at this point in time.
[00:02:05]
You can see, okay, it was at this commit. Not very informative though, right? When you look at this, you don't really know what your stash changes are or anything. So we can go git stash show, little bit nicer to see okay, this is the one where I made a change to README, so a little bit nicer.
[00:02:22]
Let's pop our stashes this time. And when we pop it, I wanna see that it's now made that change. Git stash pop, there we go. Our change is back, it's right here. All right. Now let's stash our changes again, but this time let's use a custom message. So go.
[00:02:39]
Git stash with a message. Read me changes. There we go. We did a stash again, but this time with a custom message. Now do git stash list. Now look at that. Isn't that a lot nicer to do when you have a nice message. The reality is sometimes you will have a couple stashes and you're not going to remember what's in these stashes.
[00:03:00]
And so just having a nice little friendly message to yourself can just- it'll just save a lot of headaches rather than trying to go through, get all the diffs and looking at each different one and then realizing, gosh, there's the one I'm looking for. So now let's create another change and we'll stash that so we have two in the list.
[00:03:18]
Let's go another one. I'll just go. Go bam readme and I'll just add another line. There we go. DeVore echo by the way, and then I'll go git stash. Another change. So now I want to go git stash list. I'll actually see two changes. I have another change on top, and the readme changes on bottom.
[00:03:36]
So the readme changes is the first set of changes we did. Another change is Is the second set of changes we did. But notice it's on top. It's the first one. So let's pull in our upstream changes now. So I'm gonna go get pull. There we go. I have this beautiful upstream change.
[00:03:52]
Now let's get our original change back, the first stash we did. Does anyone remember how you're gonna get the first stash out? Not the second not the one we just added. But the first one is the index. Path index. There you go. So what you should be doing is going git stash well first you can go get stash list make sure okay, I need to pull out the first one right here.
[00:04:12]
I can go get stash pop index one. And now we have that proper change. We We don't have the Dvorak one, we have the downstream change. So working with stash, pretty easy to use. Now we have this beautiful change on top. Awesome. And if we want to, we can even commit it if we want to, but stage is not changed.
[00:04:33]
There we go. It looks like even in here, I commit it so we will commit it with the exact same. Beautiful message that I have right here, let's see if that's coming from terminal git add greatest change. Fantastic. There we go. So commit that into your code. That means your readme should approximately look like that.
[00:04:55]
I have a little space in here. It's not there's no space In my example one, it's because my first commit contained that, it's that one right there. See, look at that. I added a space, but in all my examples, I didn't have the space. Very painful for me to see this, that I can't believe I did that.
[00:05:17]
By the way, if you don't know I don't cover it in this course but dash s will search like your commit message or search to commit change so you can look for changes that way. Very, very fantastic. So I knew where to find it. So I found it right away.
[00:05:32]
Pretty neat stuff, just in case you're wondering. Alright, so we're trees. We're going to talk about. This a little bit I believe, I'm not sure, yeah, we're not talking about right now but we're gonna talk about a little bit the thing that makes worktree so cool is that a worktree is a another copy of your repo without all the weight of a repo.
[00:05:51]
So if it takes you five minutes to download your, to clone out a repo, you can build a worktree effectively just as fast as you can build up The branch. They're very, very lightweight and they're fantastic. So you can see why that could be a very nice alternative to stash.
[00:06:08]
All right, so we're gonna do more rebasing. This will be the longest section here and it's gonna have all the conflicts and everything and we're gonna go over it. It's going to take a little bit. So anyways, rebasing, you can do things like this, where we start off here, right?
[00:06:25]
E is branching off of A. This picture makes it looks like it's branching off of B. I'm not sure which one I chose to have it actually branch off of. It doesn't really matter. It branches off in the days of history, way back then. And if you do a rebase you can make it so that it goes all the way to the front, right?
[00:06:40]
We all remember this. What are the steps that happen? Step one, we check out the branch we're rebasing on step two. We play our commits one at a time at the tip of the branch. We are rebasing on three we update the branch we were on with the new position and the new commit.
[00:06:55]
That's why it all. Alters histories because remember, it's actually creating a new set of commits. 'cause remember a commit, it also contains time. So that's why you get new commits when you rebase. All everyone's good. Everyone's good on that one. Awesome. All right. So first let's do a little bit of interactive rebasing and squashing.
[00:07:16]
So this part. Is where you'll do this pretty much on your own branches, never on the public one. Interactive rebasing and squashing allows you to take, say, this branch right here and turn it into this, where it's a singular commit as opposed to three commits. So that means while you're working, you can make all the commits you want every single checkpoint.
[00:07:36]
And then when you're done, you Can take all those commits and turn it into one commit yourself. Now a lot of people do it to where they have the remote like say GitHub will do an automatic squash merge for you. It will first take all of your commits squashed into one and then merge it onto GitHub.
[00:07:51]
That's a pretty common strategy that I've seen, I know we do it with stash and stuff like that at Netflix. So I think most People probably have some very similar experience. When you do interactive rebasing, it also allows you to edit the message. So that way what I typically do is I'll do each one of my commits and I'll start with squash me.
[00:08:10]
So I can just look in the history and go okay, where did I start? Okay, I started right here. rebase from right there, and then it's very easy that I interacted Given that I can just erase all the messages and give it a nice proper message, as opposed to a bunch of squash teammates, right?
[00:08:24]
And so we're going to create a, we're gonna create some situations where we can interactively squash our commits, and get some good messaging. There's a lot of ground to cover here. We're gonna go over conflicts as conflicts are a very important part of this. If for whatever reason I've done steps out of order and we're unable to create the conflict as easily we might have to add some extra commits because that's part of the fun is that we didn't do it right the first try so be very careful where we make changes because from here on out we're going to be trying to make conflicts and we all want to be on the same page so the first it is by the way conflicts are The most ready topic.
[00:08:58]
I hate them. I don't know why. I have an unreasonable amount of anxiety every time I pull a gloomy, and there's going to be a conflict and even though I know there's not, I still just feel like I'm going to have a freak out. I don't know why it is.
[00:09:11]
It just is that experience. Yeah, I hate them. You hate them. But it's good to know how to resolve them. Please, by the way. Don't use VS code don't use fugitive it with Git or with Vim. Don't use any of those for this. Let's just manually edit the diffs.
[00:09:25]
You want to see why they're doing this, to me very important, specially with your knowledge of rebase. Why conflicts can be goofy with rebase versus merge? We're gonna go through it all but if you're not using the hand at it, you're just Using a program, you may miss some of these subtle changes.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops