简体   繁体   中英

How to fix "diverged branch" in git?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM