user2124712
user2124712

Reputation: 154

git: reset to remote branch

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?

Upvotes: 5

Views: 7536

Answers (2)

Antonio P&#233;rez
Antonio P&#233;rez

Reputation: 7002

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.

Upvotes: 3

Claudio
Claudio

Reputation: 10947

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

Upvotes: 0

Related Questions