Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

weird behavior when running git reset --hard on my local copy of a repo

Tags:

git

TLDR: doing a git reset --hard origin/mybranch resets the local repo to a much earlier version instead of the latest one.

Dev branch looks like this:

enter image description here

I think this is best explain with an example

[myusername@myhost myapp]$ git branch
* dev

[myusername@myhost myapp]$ git pull origin dev
From bitbucket.org:
 * branch            dev        -> FETCH_HEAD
Already up-to-date.

[myusername@myhost myapp]$ git reset --hard origin/dev
HEAD is now at 2be5b0e Merged in mybranch (pull request #57)

[myusername@myhost myapp]$ git pull origin dev
From github.com:myusername/myapp
 * branch            dev        -> FETCH_HEAD
Updating 2be5b0e..2cdc555
Fast-forward
--file list here--
 19 files changed, 324 insertions(+), 196 deletions(-)

[myusername@myhost myapp]$ git log
commit 2cdc555dde1ef7ef187756036bb2d19dacae0c26
Merge: 6c9399d fc7d29f
Author: First Last <[email protected]>
Date:   Thu Jun 4 00:53:16 2020 +0000

    Merged in mybranch (pull request #81)

[myusername@myhost myappc]$ git reset --hard origin/dev
HEAD is now at 2be5b0e Merged in mybranch (pull request #57)

I would have thought that doing a git reset --hard origin/dev would reset the repo to the last commit (which is pull request #81)?

I also have deleted all the files and folder (including .git*) from the filesystem and recloned the repo last week. It was behaving properly for awhile and this happened again.

like image 525
mrjayviper Avatar asked Jun 05 '20 01:06

mrjayviper


1 Answers

[myusername@myhost myapp]$ git pull origin dev
From bitbucket.org:
 * branch            dev        -> FETCH_HEAD
Already up-to-date.

Here origin/dev is not updated. It seems remote.origin.fetch is not set properly. Try git -c remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* pull origin dev to see if origin/dev gets updated. If yes, run git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/* so that origin/foo will be updated after git pull origin foo from then on.

Side note: in git-bash for Windows, I have experienced that origin/foo does get updated although foo -> origin/foo is not printed in the log of git pull origin foo, so the log is not completely accountable.

like image 150
ElpieKay Avatar answered Nov 15 '22 05:11

ElpieKay