Your branch and ‘origin/main’ have diverged, how to fix this ?
I have been working in my local repository and on running command git status, I noticed that my local branch and main branch have diverged. The git status command shows “Your branch and ‘origin/main’ have diverged“. In this sneppet you will learn what you need to do when you face this issue.
Your branch and ‘origin/main’ have diverged
After running git status command I see the following output
$ git status On branch main Your branch and 'origin/main' have diverged, and have 1 and 5 different commits each, respectively. (use "git pull" to merge the remote branch into yours) nothing to commit, working tree clean
For example, the file1.txt in my local branch has the following changes.
first line second line last line
And let’s say the file1.txt in the main Git branch has the following changes.
first line second line third line
What to do when your branch is diverged with master or main branch ?
I tried to follow suggestion in the git status output. Hence I tried to run git pull command as shown below. It is evident that the automatic merge failed and the conflicts had to be fixed manually and then commit the result.
$ git pull Auto-merging file1.txt CONFLICT (content): Merge conflict in file1.txt Automatic merge failed; fix conflicts and then commit the result. first line <<<<<<< HEAD second line last line ======= second line third line >>>>>>> 774e727606ee3fd5bff09fc4548a453410041c70
Therefore, I had fixed the conflicts manually so that modified file1.txt content looks like below.
first line second line third line last line
Then, add the modified file using git add command to mark the resolution.
$ git add .
After this, use git commit command conclude the merge since all the conflicts were resolved and changes are added to the staging area.
$ git commit
Finally, run git push command to push all the changes to remote repository or main branch.
$ git push Enumerating objects: 10, done. Counting objects: 100% (10/10), done. Delta compression using up to 8 threads Compressing objects: 100% (5/5), done. Writing objects: 100% (6/6), 997 bytes | 249.00 KiB/s, done. Total 6 (delta 0), reused 0 (delta 0), pack-reused 0 ----- ----
That’s all. Now when you run git status command you should not see the message “Your branch and ‘origin/main’ have diverged” in the output. Hope you find this sneppet helpful 🙂
You’ll Also Like
- Throw away the local commits from your branch in Git ?
- Create a Branch from a Tag in Git ?
- How to connect to Github using SSH ?
- How do you do undo git reset –hard HEAD^ and revert to previous commit ?
- Git – How to compare files in two different branches
- Git not working with a proxy server – gnutls_handshake() failed
- npm ERR! enoent spawn git ENOENT
- GitHub unable to access HTTPS and SSL routines error
- Remove http and https proxy settings of npm and git ?
- Fix Nodejs Error: ENOENT: no such file or directory