mbdevpl
mbdevpl

Reputation: 4870

How to reset the origin/master ref locally

I have the following scenario:


  1. I have 3 commits in my local repo, like this:

    initial -- a -- b

    where both master and origin/master point at b.

  2. After that, I fetch the origin, and the repo looks like this:

    initial -- a -- b -- c -- d

    where master points at b and origin/master point at d.

  3. After that, I merge and now master also points at d.


I can reset my master branch by doing git reset --hard b.

What I would like to do is to also reset the origin/master ref to commit b. And, I do not mean that I want to reset remote repository to some state - I want to do this locally only. No pushing. I want the remote to remain unchanged. What I want is to change the knowledge of my local repo with regard to the state of the remote repo.

How can I do that?

I can probably change some files in .git directory, but which files? Or is there maybe some git command for it?

Why do I need that? Because I want to be able to replay the scenario, to easily test a git-based project management I'm working on. I would like to replay the same situation, in order to see how it behaves if I change the conditions only slightly.

Upvotes: 4

Views: 3764

Answers (2)

David Deutsch
David Deutsch

Reputation: 19015

The safest way is with git update-ref refs/remotes/origin/master <SHA>. This will cause your change to be recorded in the reflog, should you ever need to refer to it. Though as codeWizard said, your next fetch will update the ref anyway, so there is not much risk in changing it directly either.

Upvotes: 3

CodeWizard
CodeWizard

Reputation: 141946

In git branch is simply a "text" file with the sha-1 of the commit.
As you mentioned in your comments there are the paths to your branches.

.git/refs/heads/master 
.git/refs/remotes/origin/master

You can manually edit any of those files and paste into them any SHA-1 you want.
I still cant see any reason to do it but its possible of course.

Edit .git/refs/remotes/origin/master and change the current SHA-1 to the one you want to point on, but keep in mind that your next git fetch will update it again to the latest commit on the server

Upvotes: 1

Related Questions