Wrong git commit to trunk in local, how to pull clean from remote?

85 Views Asked by At

After doing some changes I committed them locally to the trunk before branching. I then branched and pushed my changes to the remote repository. Now the remote repository has the changes it needs to have. However, because of my local commit to trunk I get the following message when trying to pull recent changes from the remote trunk: hint: You have divergent branches and need to specify how to reconcile them.

I have seen this question How to git pull while ignoring local changes? and I performed the following commands suggested in the answer:

git reset --hard
git pull

However, I'm still getting the same message.

What did I try and what was I expecting?
I would like to have my local trunk up to date with remote, and I would like the remote to overwrite all the changes I might have locally.

3

There are 3 best solutions below

3
André On BEST ANSWER

This is the specific command that resets the local trunk and let the local pull an up-to-date version of the remote trunk, without considering the local changes:

git reset --hard origin/{trunkName}
3
eftshift0 On

You can set the local branch in the same position as the remote:

git branch -f some-branch some-remote/some-branch

As a side note, you should consider avoid using a local branch that is just a tracking mirror of the remote one. You can do operations against the remote branch just as easily.

0
Romain Valeri On

From you local branch, with a clean working tree, you could do a simple (branch-agnostic)

git reset --hard @{u}

@{u} is a specific construct in git, which means "the ref set for this branch as the default pull source" (see the part of the git revisions page where it's described)

Note: Before you reset, be sure to git fetch to get fresh remote references or you could still be behind the remote (because of what your local repo "knows" so far).