When working with collaborators they might push a change to your remote branch while you are working on the same file locally, resulting in a conflict. You will learn to create, and then fix, a merge conflict.
Keywords for Conflict Resolution:
Close, Fix, and Resolve
You can use the Issue Title to close issues. Using the above keywords along with the issue number can be used to close issues via commit messages. More info can be found in the GitHub help documents.
Let's say we want to add additional changes to our add-readme branch 0:00 before merging our pull request into the master branch. 0:03 This is really simple. 0:07 All we need to do is make the changes we want in our text editor, 0:08 commit the changes, and push the branch. 0:11 In this video, however, 0:14 we're going to intentionally create a conflict by making our edits and 0:15 committing the changes on the GitHub website as well as on our local copy. 0:18 This will give you an idea of the editing abilities on GitHub. 0:23 When you're just getting started using Git and 0:25 GitHub, my suggestion is to not make any edits on the website. 0:28 Doing all of your work locally will help you avoid conflicts 0:32 like the one we're about to create. 0:34 If you're only pushing up changes from your local computer and 0:36 working by yourself, you'll always be in sync with the remote repository. 0:39 However, if you do start working with collaborators, 0:43 they might just push up a change to the remote branch while you're working on 0:45 the same file locally, resulting in a conflict. 0:48 This is a fairly common issue, and it's not the end of the world, but 0:51 it can be scary, so let's practice creating and fixing a merge conflict. 0:54 Okay, so I'm here in my code view and I want to change to the add-readme branch. 1:00 Let's say I want to add an image to my README. 1:04 I'll click into the README.md file and click the Edit button here. 1:07 So here's a little hack or a pro tip. 1:14 If I wanna add an image to a readme file, 1:17 the markdown syntax in the markdown cheat sheet will tell us how to do that. 1:20 But a little shortcut is I can go to Issues and 1:24 New Issue. 1:29 And if I have a file like I do here, then I can just drag and 1:33 drop it into the issue body and it creates this automatic URL. 1:40 Or it creates the Markdown syntax that we need. 1:45 So I'll do Preview. 1:48 Cool. 1:51 So I can copy and paste it back here 1:54 and I can preview the changes. 1:58 Awesome. 2:05 So the green shows what I've added and that looks good. 2:06 So I'll go back and edit my file. 2:11 I'll also want to adjust this typo here. 2:13 Files completed and then let's do a, 2:18 And cool, looks good. 2:49 So here I will add a commit messages and 2:51 I can just say update README.md 2:58 to add image and fix typo. 3:04 I can add an optional extended description, and then I can go ahead and 3:09 directly commit the changes to the add-readme branch by 3:13 clicking Commit changes here. 3:17 Back in the console, normally if we have any changes to a remote repository, 3:22 we would use the command git pull to pull in the remote changes. 3:27 As they say, always pull before you push. 3:31 But let's say I forgot that I had made the change on the remote repository, and 3:34 I'm gonna go ahead and add some information to my README here. 3:37 And I'll save. 3:49 So in the console, I'll run the commands. 3:50 Let's make sure I'm in the right branch. 3:55 I'm still in the add-readme branch, great. 4:01 And so I will add and commit my changes. 4:03 Cool. 4:16 And I'll go git push origin, add-readme. 4:17 Okay. 4:34 What's going on? 4:34 It said failed to push. 4:35 Updates were rejected because the remote contains work that you do 4:38 not have locally. 4:42 This is usually caused by another repository pushing to the same ref. 4:44 Okay, so that's right, I forgot to pull, right? 4:47 So now I type git pull origin add-readme. 4:51 And I get a new error message. 5:04 CONFLICT: merge conflict in README.md. 5:05 Automatic merge failed; fix conflicts and then commit the result. 5:09 Okay, so here we can refresh the text editor. 5:13 And we can see there's a bunch of stuff here. 5:22 It looks kind of scary but we'll get through it, I promise. 5:28 Okay, we can see there are a lot of errors or 5:31 conflict markers which show us where the conflict occurred. 5:33 There's an equals sign that separates the two versions. 5:36 On the top, the head is the most recent commit, and 5:40 the one I was trying to push up from my local computer. 5:43 And below the equals signs was the change that I made on GitHub. 5:46 I'll need to choose which to keep. 5:50 I wanna keep the one that has the image, 5:52 so I'll just have to delete all the rest of this here. 5:55 And all these conflict markers, and save. 6:07 And then back in our console, 6:11 we'll do a git status. 6:17 Cool, I'll do a git add. 6:21 git commit -m. We'll go, fixed 6:24 merge conflict, 6:30 and I'll go git push 6:35 origin add-readme. 6:40 Cool. 6:52 Now we'll go back to the Treehouse repository. 6:54 We'll go to the Pull Request. 7:00 You can see my commit history right here, updated README.md file to add 7:05 the image and fix the typo, added my profile locally. 7:10 And then another commit message where I fixed the merge conflict. 7:14 Also, if we go here, we can see this commit history. 7:17 Now that I've added our additional comments and fixed the commit, 7:21 let's go back to our pull request. 7:24 We can see the additional commits to the branch are shown here. 7:26 And let's take another look at the files changed view, too. 7:30 Commits, and the file's changed. 7:38 That's fine. 7:41 Okay, this looks good. 7:44 Now let's click Merge pull request and confirm the merge. 7:46 Confirm merge. 7:53 Pull request successfully merged and closed. 7:55 Woo hoo! 7:58 Congratulations. 7:59 You've completed your first pull request. 8:00 Since we won't be using the add-readme branch any longer, 8:02 we can delete the branch here. 8:05 If we revisit the issue page, we see that the add-readme issue is now closed. 8:08 Because we included fixes number one in our pull request body, 8:13 GitHub took care of closing that issue when the pull request was merged. 8:17 If we go to our profile, we'll see a green square on our contributions graph. 8:21 Great job. 8:25 Let's quickly review what we've done. 8:27 After we opened a pull request, 8:29 we made a change to the repository on the GitHub website. 8:31 Since we forgot to do a git-pull, 8:34 we made a change to the same file on our local computer and we created a conflict. 8:36 In the text editor, we deleted the lines we didn't want to keep and 8:41 then committed and pushed up to the remote repository on GitHub. 8:44 Problem solved. 8:47 In the next stage, 8:49 Kyle will show how to use these features to collaborate on a team. 8:50
You need to sign up for Treehouse in order to download course files.Sign up