zale
zale

Reputation: 1279

Reset local view of remote branch

Assume I have a Git repo that looks like this

* abcdefa (master) tip
* 1234567 (origin/master) base

If someone else fast-forwards origin/master from 1234567 to abcdefa, my local repo will not change until I git fetch. Once I git fetch, my local git will look like

* abcdefa (master, origin/master) tip
* 1234567 base

Is there a way to undo this git fetch? That is, can I make git believe again that origin/master is still at 1234567 rather than abcdefa, without actually changing the remote? Clearly this state is not completely inconsistent, since this is the state I was in before running git fetch, so I assume there is some way to return to it.

I realize this sounds like a huge case of an XY problem because what I want is very unreasonable, but (a) I think I need to do this for an company-internal CR tool that I unfortunately can't share many details about, and (b) outside of practical applications, I'm curious to know how to do this, or if it's not possible.

Upvotes: 0

Views: 38

Answers (1)

torek
torek

Reputation: 487745

You can forcibly move a remote-tracking name with git update-ref (which takes the full name). If reflogs are enabled—which they normally are—you can undo the last git fetch that updated origin/master with:

git update-ref refs/remotes/origin/master origin/master@{1}

It's probably better to fix whatever tool you are using to accept commit hash IDs or things that parse to such hash IDs; then you can just run:

tool origin/master@{1}

or:

tool $(git rev-parse origin/master@{1})

directly.

Upvotes: 2

Related Questions