4

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.

3
  • Is cp .git/refs/heads/master .git/refs/remotes/origin/master (after resetting master to commit b) a valid solution?
    – mbdevpl
    Commented Jun 25, 2015 at 9:53
  • Why do you want to do it? Why not creating new branch locally form the desired SHA-1?
    – CodeWizard
    Commented Jun 25, 2015 at 10:25
  • I need to change a remote ref specifically, because I am fetching and then I do some diffs between some branch and its tracking branch. I could simulate this using only local branches, and assume that branch1 tracks branch2 but I want to use all git's mechanisms before I code something (to use something was already coded, and is well-tested, instead of adding new code). And also I prefer to operate in exact same conditions as in real case, to avoid some unexpected differences in behaviour.
    – mbdevpl
    Commented Jun 27, 2015 at 10:57

2 Answers 2

3

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.

1
  • Thanks. Unlike cp file1 file2, this command works regardless if I am using it from a repo, or from a submodule (that might not even have a .git directory). It also works with branch name, like git update-ref refs/remotes/origin/master master. Both of these features are very useful in my case.
    – mbdevpl
    Commented Jun 27, 2015 at 11:00
1

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

2
  • 1
    Note that these files may not exist, if the refs are packed. (.git/packed-refs) Commented Jun 25, 2015 at 15:17
  • And these files might not exist in the expected path when the repo you are doing this on is a submodule of some other repo. Then, finding the correct paths for copying is more troublesome. Therefore raw copying of the files is a solution, but only partial. But your answer is informative anyway, so thanks :)
    – mbdevpl
    Commented Jun 27, 2015 at 10:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.