Git Error-You have divergent branches and need to specify how to reconcile them | Git Merge Conflict

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so today I'll do a work around on one of the most common git error that you might have faced or may face so let's begin so I got this error when I was trying to pull changes from remote repository to the local I got this you have Divergent branches and need to specify how to reconcile them so today I will gonna demonstrate the same by replicating everything that I did then how I solved it and what was the root cause and what are the practices that we must follow so that we don't face such errors okay so let's begin so I am using Ubuntu ec2 instance as my local machine and I'm using GitHub as my remote okay so to work with this suppose uh we have one git error repository in local City Guitar okay if I to get status here it's not a git repository so to make any repository get we have to initialize it using git init so I am initializing this it got initialized now if I but we haven't set its remote if I had to get remote hyphen it won't show me anything so now it's get it is tracked by git now we have to set a remote repository for this local so uh go to the GitHub from here create a new Repository let's give it the same name you can give any I'm giving it the same name okay it's available and I'm adding a readme file okay so our repository got created and you can see the initial commit has happened okay now let's get back to here now add that repository as origin here using command get remote add origin then copy this SSH URL from GitHub thank you okay now if you do get remote hyphen we you can see our local repository is attached is connected to our remote repository okay so suppose you have started working on this local for the example I am just creating three empty file using touch command touch ABC if you do Alice you can see we have our three files if I do get status I can see I have all these files but these are untracked so let's track them using it so let's do git add a b C if I had to get status you can see they have they are ready to commit so after get add we have to commit with one message like added a b and c now let's push it to R main branch okay in remote when I am trying to push it to my remote repository it is getting failed even though I have already set up my remote and you can see it has been set up is it pushing so I realized like what is the best practice we should do while working in local we should first pull our changes pull any changes from the remote because the excess of remote can be with any developer so we should ensure that our local repository is in sync with remote Repository as we can see our inner GitHub we have one commit and we have this redmi file is it here if I do Alice no it's not here so what was the best practice the best practice was before pushing our changes before starting to work with local we should first pull our changes by doing it full origin mean and then we should push but even after pulling I am getting this error you have Divergent branches and need to specify how to reconcile them so why it happened like if I have pulled it then why this Divergent branches error I'm getting so the reason is like the you are the this message is appearing because your local branch and the remote branch have been diverged meaning they have separate commits that are not shared between them if you do if you check the log by doing git log hyphen hyphen one line you can see it has only this commit but the initial commit was this that is not there in local that means our commits have been diverged so get is prompting this warning to specify how you work how we want to reconcile this Divergent branches by specifying a poor strategies so there are three options that you can choose from depending on your situation the default strategy is through merge but we have already done this like it will merge the remote changes into our local branch we have done this get pull origin main but it didn't work so let's move to the second strategy the second one is rebase rebase will this will reapply your local commits on top of the remote branches commit and this can create a cleaner history but may require resolving conflicts you can use the following command to pull using the rebase strategy this might work for our scenario because we want to reapply our look but look it will reapply our local commits on top of the remote so yeah this will work now let's see the third one third one is fast forward this will only perform a pull if it can be done using a fast forward merge which implies that your local branch is behind the remote branch and has no additional commits but that's not true in our case like in we have already made some commits we have we made one commit now after adding those three empty files so this won't work in our case so in our case this command will perfectly work so let's give it a try you can see successfully rebased and updated it now you can see when I do when I check the logs you can see a log that was there in remote it's now here in our local as well that means it's in sync now if I do get uh get push origin main it will work uh wait wait git branch okay so currently we are in master Branch so we need to do git push origin master yeah see our changes have been successfully merged now create a pull request for Master on GitHub so now let's go to here you can see this message Master had recent pushes so we need to compare and create a pull request added ABC create pull request check it's checking this branches no conflicts we need not to resolve the conflicts now we can directly merge conflict merged let's try to comment as much then count it confirm merge pull request successfully merge and closed if I come to the code you can see our files have been successfully all the three files that we have created in our local have been successfully posted to a remote Repository so that is how we have resolved the error Divergent error so many of the times we might face many errors and it mostly happens when we don't follow the best practices so the one of the best practice that I'll suggest is before working on your local make sure that every time you start first pull the changes from the remote Repository do that first and then only uh do your work because it might lead to some other errors like merge conflicts and it might be frustrating at a time so it was all about this resolving that error I hope you get idea on this okay that's it I hope you enjoyed it thank you bye
Info
Channel: Sakshi Rathore
Views: 7,330
Rating: undefined out of 5
Keywords:
Id: HzBoMI7yqsU
Channel Id: undefined
Length: 10min 27sec (627 seconds)
Published: Tue Aug 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.