0

Through a reset to an earlier commit, I cannot get my local branch back in order. I keep getting merge conflicts locally, while the version on remote does not have any conflicts and what I want, is to get an exact copy of the fremote in my local branch.

What I can do is git checkout <last commit> and I will essentially get a copy of the remote in my local directory but then I also get:

$ git checkout bd8b876 
Note: checking out 'bd8b876'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

Which I don't want, as I still need to work on this branch.

I prefer not to start a new branch as the branch name is tied in with a script that is linked to the JIRA ticket too. If there's no other way though, I will do that.

What I've tried to resolve the issue:

git reset --hard HEAD

is now at 8dc6510 Update main.py

git status
    On branch OS-4055-header_bar_Drive_parameters
    Your branch and 'origin/branch' have diverged,
    and have 19 and 3 different commits each, respectively.
      (use "git pull" to merge the remote branch into yours)
    nothing to commit, working directory clean

but this would always leave my local files in a merged state. I figured that pull in a combination of fetch and merge.

I also tried (after reset): $ git fetch origin branch:branch but even that would not give me the expected result. I checked the branch out on another syste and the files came in as expected (w/o merge conflicts). How do I get my local branch working copy into that state without the need to entirely delete my source directory? `

mike
  • 1,233
  • 1
  • 15
  • 36
stdcerr
  • 13,725
  • 25
  • 71
  • 128
  • 1
    Your working directory is clean. Why would you need to entirely delete your source directory? Why did you checkout a commit and not a branch? Why not pull and merge the changes? – evolutionxbox Jun 03 '20 at 15:21
  • @evolutionxbox because it keeps giving me merge conflicts I don't want even thout `git status` doesn't show any local changes – stdcerr Jun 03 '20 at 15:43
  • 1
    Nobody wants conflicts. Is it not possible to resolve them? are other people making commits to the same branch as you? Or are you force pushing after updating the history (like rebasing or resetting)? – evolutionxbox Jun 03 '20 at 15:45
  • I did force a push after the reset, yes. No other people working on this branch, no – stdcerr Jun 03 '20 at 15:46
  • 1
    Did someone else commit to the same branch? – evolutionxbox Jun 03 '20 at 15:47
  • no, only myself. But I have rebased the branch in the past, i.e. git log shows commit from other people in the past – stdcerr Jun 03 '20 at 15:48
  • 1
    If you don’t want the commits on the remote branch, _and_ no one else is using that branch, you can force push again. – evolutionxbox Jun 03 '20 at 15:51
  • There's nothing to push, I basically want to force a pull – stdcerr Jun 03 '20 at 16:16
  • 1
    May you clarify whether you want to keep the commits on the remote branch? If you don’t _a force push will be needed_. If you don’t want your local commits, _a reset hard to the remote branch (origin/branch) will be needed_. AFAIK there isn’t such a thing as a force pull. – evolutionxbox Jun 03 '20 at 16:18
  • Yes, I want to keep the commits on the remote, i don't need the local commits. I have tried `git reset --hard` butafter that, `git status` would still tell me `Your branch and 'origin/branch' have diverged,` and `and have 19 and 3 different commits each, respectively. (use "git pull" to merge the remote branch into yours)` which doesn't sdound right – stdcerr Jun 03 '20 at 16:30
  • 1
    Make sure you checkout the local branch. Then reset again the remote. `git reset --hard origin/branch` – evolutionxbox Jun 03 '20 at 16:31
  • Boom! `git checkout branch && git reset --hard origin/branch` did the trick! Thanks, you can move this to an answer and I will accept it! Thanks again @evolutionxbox – stdcerr Jun 03 '20 at 16:54
  • 1
    That’s good news. I haven’t written an answer because I convinced a similar question like this has already been asked. (Again, glad it’s been sorted) – evolutionxbox Jun 03 '20 at 16:55

1 Answers1

1

I keep getting merge conflicts locally, while the version on remote does not have any conflicts and what I want, is to get an exact copy of the fremote in my local branch.

So you want to keep whatever is in the remote as the "good" stuff. Just perform a new clone and delete your current working directory.

How do I get my local branch working copy into that state without the need to entirely delete my source directory?

So you don't want to do that, but either way you'll have to remove your local commits that diverged, because they diverged and you want to keep whatever is in the remote.

git commit --amend all local commits that diverged, or even git reset --hard until you effectively deleted all local commits. You can achieve deletion of "commits" in a number of way. Do this until you get to the last common commit your local and remote share.

Once you are here, if any file is modified, then either git stash it or git checkout -- it to revert it to the HEAD original state.

Now, git pull will fetch and merge all that is in remote that you so desperately wanted.

Re-apply any wanted change you stashed, create a new commit and push it to remote. Repeat until Jira issue is closed ;)

BTW: I'm still unconvinced about how your local and remote histories diverged. I hope you put yourself in that situation and nobody else is pushing to your same branch without a merging strategy...

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
  • I have not tried what you're suggesting as what @evolutionxbox recommended in the comments above: `git checkout branch && git reset --hard origin/branch` did the trick for me. Thanks anywats! – stdcerr Jun 04 '20 at 15:32
  • 1
    well, in the end it is what I'm saying here: destroy any local divergence, then start over. – Daemon Painter Jun 04 '20 at 19:20