Home > Software design >  How to fix "diverged branch" in git?
How to fix "diverged branch" in git?

Time:06-03

I cloned a repository and did a checkout of a branch, and now when I do a git pull I see the error

hint: You have divergent branches and need to specify how to reconcile them.

Is there a way to fix this issue without rebasing or merging something? Is there some git command that will just get the local checkout to the same state as the remote repository?

I do not want to keep any local changes, I want the local branch to be EXACTLY like the remote branch which I am trying to pull. The local changes do not interest me.

There is a workaround that works fine:

cd ..
rm -rf repo
git clone <repo-url.git> 
cd repo
git checkout <branch> 

but maybe there is an easier way? It took very long to remove the complete repo.

Again: I want a branch EXACTLY how it is on the remote. I do not want to merge or rebase anything. I want to have locally the exact same state of the branch as it is on the remote.

CodePudding user response:

Maybe the following process works:

  1. You checkout main

     git checkout main
    
  2. Then you remove your branch

    git branch -D <branch>
    
  3. You checkout your branch again

    git checkout <branch> 
    
  4. You do git pull, git fetch, git st etc. to check if all is up-to-date

CodePudding user response:

TL;DR

git reset --hard origin/<branchname>

Details

If you really want to discard any local changes, and just set your local branch to state of the remote, a hard reset is the thing to do.

Assuming you already have branch <branchname> checked out, and you want to reset it to origin's version:

git fetch
git reset --hard origin/<branchname>

This will discard any local commits on that branch, as well as any edits you have done locally in your sandbox. Since you're considering deleting your local sandbox altogether, I believe this is exactly what you want.

  •  Tags:  
  • git
  • Related