1

I can indirectly reference "the current commit" with HEAD.

I can indirectly reference "the commit before otherCommit" with otherCommit~ or otherCommit^ (subtleties relating to merge commits IIRC)

Is there a way that I can indirectly reference "the branch which the currently active branch is tracking".

Usages would include git reset --hard <magicReference> which discards local changes, and local commits to the current branch, without needing to type the name of the current branch.


Regarding any warnings about reset --hard or detached HEADs ... I do know exactly what I'm doing and none of those concerns apply. Please assume that what command I want to run that happens to references the remote branch that my current branch tracks is genuinely what I want to run.

Brondahl
  • 7,402
  • 5
  • 45
  • 74
  • Warning: [`git reset --hard`](https://git-scm.com/docs/git-reset#git-reset---hard) will discard your uncommitted changes. They will be lost forever. Don't use it unless you know what you're doing. – axiac Apr 13 '18 at 11:37
  • @axiac You write: "it's something that you should not do, except for exceptional situations". I don't think there is anything wrong with a detached HEAD; the only downsides is that newcomers to Git may find it to be a confusing state. – jub0bs Apr 13 '18 at 11:38
  • 1
    ``@{upstream}`` – user4003407 Apr 13 '18 at 11:40
  • I believe this is a dupe of https://stackoverflow.com/questions/171550/find-out-which-remote-branch-a-local-branch-is-tracking, but I'll wait for confirmation from the OP before exercising the [tag:git] superpowers granted to me by Stack Overflow. – jub0bs Apr 13 '18 at 11:42
  • @axiac _"you don't need to type the name of the current branch"_ cool. So ... I'm currently checkout out on branch `feat_1_3` how would you like to execute `git reset --hard origin/feat_1_3` without typing `feat_1_3` – Brondahl Apr 13 '18 at 12:13
  • @Jubobs yeah that looks very similar. and I infer from it "there isn't a built in shortcut for this, but you can probably abuse dynamically invoking stuff to make it work if you try hard enough". – Brondahl Apr 13 '18 at 12:17
  • *"which discards local changes"* -- by "changes" I usually mean *uncommitted* changes. Apparently you mean "local commits". – axiac Apr 13 '18 at 12:18
  • @axiac ah, yes, indeed I do, and I agree that my description was ambiguous at best. Have clarified now, thanks for the input. – Brondahl Apr 13 '18 at 12:22

1 Answers1

4

You're looking for HEAD@{upstream}, also known as @{upstream}, also known as @{u}.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • 1
    Ah hah! yeah, that's the one! – Brondahl Apr 13 '18 at 12:54
  • Can you add a link to some documentation, so that I can understand how the `HEAD@{upstream}` syntax works - it suggests that I can do `otherBranch@{upstream}` except that confuses me, as HEAD is a reference to a commit, rather than a branch, and how would it interpret `arbitraryCommit@{upstream}` where `arbitraryCommit` is in multiple remote branches? – Brondahl Apr 13 '18 at 12:57
  • 1
    @Brondahl `HEAD`, unless detached, is a reference to a branch. Otherwise the same problem would apply to `git commit`: which branch to advance if several point to your current commit? And indeed, requesting `@{u}` from a commit SHA-1 will only get you `fatal: no such branch: ''`. – Quentin Apr 13 '18 at 13:15
  • ooh! I didn't realise HEAD was technically the branch not the commit! – Brondahl Apr 13 '18 at 13:19
  • 1
    `HEAD` is neither the branch nor the commit, it's just the string `HEAD`. It's typically a *symbolic reference* (an indirect reference to a branch name) in which case it's possible to determine either the name or the commit, depending on how you do the lookup. If it's detached it's no longer symbolic which means it no longer contains a `refs/heads/` name and therefore the only item you can look up is a commit hash. The actual upstream of a `refs/heads/` name is set in your `.git/config` file, in two parts, but using `@{u}` is much easier. – torek Apr 13 '18 at 14:24
  • 2
    For much more on references, see https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html – torek Apr 13 '18 at 14:24
  • @Quentin _[...] the same problem would apply to git commit: which branch to advance if several point to your current commit?_ Note that nothing prevents you from committing while `HEAD` is detached (`HEAD` will then point to the new commit). – jub0bs Apr 13 '18 at 14:38
  • @Jubobs yes, I did mean that specifically about commiting with a current branch. – Quentin Apr 13 '18 at 14:42