How do I check what `origin` references in my remote repository?

819 Views Asked by At

For as long as I used git, if I want to reset my local branch to the remote master branch, I've done

git reset --hard origin

and this seems to work empirically, but I recently learned that this might be dependent on the settings of the repository and origin may not always be the head of the remote master branch.

How can I check what its pointing to?

5

There are 5 best solutions below

0
Jim Redmond On

origin, by itself, typically refers to a specific remote - the one from which you cloned the repository. If you see origin as part of a longer string, like origin/main or origin/develop, then it indicates that Git should check what it knows of a specified branch (in the examples, main or develop) as they are on the origin remote.

In the specific example you listed (git reset --hard origin), your local branch will be reset to what Git knows of the associated remote branch. This is only possible because your local Git already knows that your current branch should track a specific remote branch. (Note that git reset does not initiate a network session of any kind, so if you haven't run git fetch or git pull lately then your reset may not match what's actually on the remote.)

You can see what origin refers to with git remote -v (which will also list any other remotes) or with git remote get-url origin (which will only list origin).

3
UpAndAdam On

You can see what origin references in your repository with git remote -v

However a simple fix of your command would solve your problem separately. Just use git reset --hard origin/master and be explicit that you want to reset to origin/master. (Note that for highest accuracy you should probably do a git fetch origin first.)

In your case, it only works because your branch is tracking origin/master so git is selecting the remote tracking branch from origin for you as the implied refspec (fully expanded as origin/master). If you were not tracking origin/master that would not reset to origin/master but to origin/foo if you were tracking the remote repo's foo branch for example.

4
knittl On

Each remote has a default branch that can be set with git remote set-head. This setting is local to your clone and independent from the remote repository (but you can tell Git to assign the value from the remote).

When used in the place of a revision, origin is equivalent to origin/HEAD. Subsequently, git reset --hard origin is identical to git reset --hard origin/HEAD.

To query which branch is currently configured as default branch for the remote, run git symbolic-ref refs/remotes/origin/HEAD.

To find the configured default branch of the remote repository, run git remote show origin. The default branch is printed in the line HEAD branch: ....

0
summerisbetterthanwinter On

git remote -v will show all referenced remote branchs of a local repository. the name is a variable, you can set any whatever you want.

Beside, you can change the variable:

git remote add origin https://some_url/some_repo

or remove it:

git remote remove origin

And use other name, after removing:

git remote set-url blabla https://some_url/some_repo

Lastly, you can reset: variable is origin and current branch is master

git fetch origin
git reset --hard origin/master
0
William Pursell On

If you want to know what origin refers to, the best way (IMO) is probably:

git config remote.origin.url

Note that this says nothing at all about whether the current branch is tracking a branch on origin, and git reset --hard origin may not at all be what you want. But I suspect you don't actually care what origin is pointing to. If you want to reset your current branch to be at whatever it is tracking, you can use:

git reset --hard @{u}

which will reset the current branch to its remote tracking branch, and you don't care what the remote origin is. (Note that this actually sets the current branch to what your local repo thinks is current on the remote, so you may need to precede this with a git fetch.)