Git rebase: how to resolve conflicts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey how's it going why would you do that i always pull before i push to avoid conflicts today we're going to learn how to resolve conflicts during rebase without smashing anyone's head into a doorframe check out my previous video where i explain how to use automatic rebase to bring latest changes from the main branch into your feature branch rebase basically replace your commits in your feature branch on top of latest commits pushed by your team into the main branch sometimes when you're based like that you can run into conflict which isn't a big deal a conflict is when two team members change the same file or the same lines in the same file in well-organized code bases conflicts are quite rare but they can occur frequently when you shamelessly put all of your code into user class don't panic when that happens when git detects conflicts with one of the commits during rebase it pauses the rebase on that commit puts all files of that commit into working directory and add special markup to those files that have conflicts each of them will have both your changes and the upstream changes made by your team to resolve those conflicts edit those files with your favorite editor keep the text that's supposed to be kept and remove the markup then run git add to stage the edited files for commit when all conflicts are resolved run git rebase dash dash continue now let's try this for real to test the conflict resolution during rebase i created this sample repository called git conflicts and with one file in it then i cloned it twice into two separate directories i've got two terminal windows open one on the left is the directory git alex and one on the right the directory git bob then i used git config to set the user name bob on the right and to set the username alex on the left so that i can emulate different users committing into the same repository from my local computer let's create a feature branch on the right by running git check out minus b add middle initial and let's go ahead and add that middle initial so i'm going to do i'm going to add a middle to my initializer and i'm going to add first middle initial and then last separated by spaces simple enough change stage that file for commit and then make a commit add middle initial and then push it up by running git push origin add middle initial since i pushed it to github it generates the response with the link which i can click and that opens the new pull request window add middle initial so i can immediately create a pull request and in that pull request i see my changes this is what i've done looks great the author is bob as expected perfect then on the left side as user alex i'm working directly on the master branch because i'm an administrator and i'm arrogant so i'm going to modify this user file again but i'm going to make a different change in my full name formatting i'm going to reformat it to be last comma first instead to so that i can order users by full name by last name first for example if i wanted to i'm going to save that stage that for comed change full name to last first i'm going to push that up get push origin master i'm going to push that directly into the master branch and let's let's see what happens so i pushed it into the master branch and my code on the master branch now has my new formatting but if i open my pull request the bob's pull request it has the conflict now there is a conflicting file user rb i can resolve this conflict in here but this would create the merge commit and the better way to do it is to use the git rebase so as a user bob i'm going to use the rebase process to resolve this conflict as we learned in my previous video first thing you want to do is you want to fetch origin master fetch the latest changes from the master branch to your local copy of the repository then you want to run git rebase origin master to run the automatic rebase of your branch on top of the latest master branch and as expected conflict merge conflict in user.rb error could not apply etcetera etcetera so git even tells you if you're paying attention it even tells you that you can resolve conflicts manually then mark them as resolved with git add conflicting files and then around git rebase dash dash continue so let's see what git status says it says that both modified user rb if i open that file for editing you'd see this is the special markup that get adds when resolving conflicts and it basically tells me that this is at middle initial so the change at the bottom is the change that i made and the change at the top is the change that came from the head from the latest commit in the master branch and at this point we need to decide what to keep so we definitely want to keep middle initial and first and last name but we also want to keep the ordering last name first and then first name so what we're going to do is we're going to modify this we're going to add middle right here to make it last name comma first name space middle initial and then we're going to remove this special markup and you can do this in edit editor just open this file in in your favorite editor be it vs code ruby mine whatever and then save the file once that file is saved you want to stage that file for commit by running git add user rb and you can run git rebase dash dash continue that shows you your commit message that was just rebased you save that commit message and you're good so now you've got both on your local system you've got both the changes that came from from the origin and the change you made so at this point since you rebased your branch you you want to force push get push dash f origin add middle initial so now if you go to your pull request and refresh it it no longer has the conflicts that's it in rare cases when your team made the exact same changes you made you can end up with no stage changes during conflict resolution so you'll have to skip that commit by running git rebase-skip we could explore that in a separate video let me know if you are interested in the comments below it is also possible to run into conflicts multiple times during rebase this happens when you have several commits in your feature branch that you need to rebase if your feature branch has a lot of commits it might make sense to squash your commits into one before rebasing on top of main to squash commits you can use an interactive rebase which i'm going to talk about in my next video i hope this was helpful to you if so press the like button and consider subscribing have a wonderful day and i'll see you in the next one
Info
Channel: Team Programmer
Views: 31,058
Rating: undefined out of 5
Keywords: git, rebase, conflicts, merge, version control, github, gitlab, programming, team, programmer, beginner, collaboration, commits
Id: OXtdxHTh2oY
Channel Id: undefined
Length: 7min 31sec (451 seconds)
Published: Fri Apr 23 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.