5

After cloning a repository I cannot reset that repository's state to a remote branch.

$ git clone <repo>
$ git reset --hard <upstream branch>
fatal: ambiguous argument '<upstream branch>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]

What works fine is either prefixing with origin

$ git reset --hard origin/<upstream branch>

or do a checkout before

$ git checkout <upstream branch>
$ git reset --hard <upstream branch>

Questions:

  1. What extra information or state change does the checkout command provide to the local git repository so that it eventually can reset its state?
  2. Is there a command (like checkout) I can run before the reset command that is not branch specific?
Zoe
  • 27,060
  • 21
  • 118
  • 148
user2124712
  • 161
  • 2
  • 13
  • Just to make sure: did you write a real branch's name and not `''` verbatim? – kostix Nov 24 '15 at 16:36
  • Also note that you can't "reset the state of a repository" to a certain branch: the repository contans all the branches of the repository which was cloned. `git reset` can be used to reset the state of *a branch* (and also of the index and the work tree, if asked, or only the index and the work tree or only the index) but not the repository as a whole. Consider reading [this](https://git-scm.com/blog/2011/07/11/reset.html). – kostix Nov 24 '15 at 16:39
  • It looks like you answered your own question. Unless, you are asking about git internals, then that would be a different question, and in that case you should re-word your post. – ryanwebjackson Sep 27 '18 at 15:49
  • The following question has a lot of good info in answers, for those who arrive here. https://stackoverflow.com/questions/1628088/reset-local-repository-branch-to-be-just-like-remote-repository-head?rq=1 – Kay V Jul 11 '22 at 14:49

2 Answers2

3

Git cannot find a reference to a branch named upstream_branch in your fresh local repo, because it doesn't exist. But the reference for origin/upstream_branch does exist. Run git branch --all and you'll see the full list of branches in your repository, both local and remote.

In the second working scenario, when you run git checkout upstream_branch you created a local branch named upstream_branch set up to track remote branch origin/upstream_branch. That's why the subsequent git reset command works.

Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
0

What do you mean by "reset that repository's state to a remote branch" ?

If you want a local branch equal to the remote branch just use **git checkout*:

git checkout -b local_branch_name origin/remote_branch_name

If you have a dirty workspace, and you want to get rid of any added/modified file you can type:

git clean -f
git checkout -f -b local_branch_name origin/remote_branch_name
Claudio
  • 10,614
  • 4
  • 31
  • 71