25

I need to retype my current branch name constantly.

Examples

Updating my current branch with the changes from remote

git pull origin mySuperLongComplicatedBranchName

Push my current branch to remote

git push origin mySuperLongComplicatedBranchName

I would to simplify this to a simple:

git pull origin *

or something similar. Unless maybe something like this already exists. I'm fairly used to git, but am just hoping I'm missing something simple.

EDIT:

Adding extra info in response to the one comment:

git show-ref --head

eaadd6401c4e179c105ac3565fe9bf53e2882f83 HEAD 
eaadd6401c4e179c105ac3565fe9bf53e2882f83 refs/heads/myReallyLongBranchName
d6af452b1ad0a309a55061f9eeb6ab7ae83b6aef refs/remotes/origin/HEAD
eaadd6401c4e179c105ac3565fe9bf53e2882f83 refs/remotes/origin/myReallyLongBranchName`

so it looks like my remote reallyLongBranch looks identical to current head. Oh I know. I'll

git fetch

then I ran

git show-ref --head again

bc1b96345913e911d03eb62763f8795cc20ecd8f refs/remotes/origin/myReallyLongBranchName

Oh great. Now I must be able to do git pull origin HEAD.

I run the command. and I get an Already up to date.

Then I run:

git pull origin mySuperLongComplicatedBranchName

I get changes pulled down. Face palm.

It seems as though it doesn't work. Git knows what my current working branch name is... I wish there was just a symbol for it.

EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

26

You can use HEAD as "shortcut" for the current commit tree HEAD:

git push origin HEAD

git pull origin HEAD

HEAD usually points to the head commit of currently checked out branch.

EDIT: jtill reminded that we have @ as shortcut for HEAD, so it can be even simpler:

git push origin @

git pull origin @
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
  • 3
    What do you mean "usually"? Just want to make sure I know the "gotchas". – EGHDK Jul 13 '17 at 20:57
  • 1
    The first (and accepted) answer of this question explains perfectly: https://stackoverflow.com/questions/2304087/what-is-head-in-git. In general, you are safe to use it :) – Alberto Trindade Tavares Jul 13 '17 at 21:00
  • 1
    Thanks. Can't believe I didn't know to use HEAD. Glad I asked! – EGHDK Jul 13 '17 at 21:07
  • 1
    Hmm... An issue I just ran into. `git push origin HEAD` worked. But `git pull origin HEAD` did not. I just got a `Already up-to-date.` message. – EGHDK Jul 13 '17 at 21:32
  • If you do git show-ref --head, the commit id that shows up is for the latest commit from current branch or from master? – Alberto Trindade Tavares Jul 13 '17 at 21:39
  • Well, it sounds that git pull origin @ should work fine (because HEAD is pointing to the tip of the current branch indeed). Are you sure that there are changes to be fetched from remote? – Alberto Trindade Tavares Jul 13 '17 at 23:20
  • Yep. I outlined my process in my edit. Let me know if you see anything wrong. – EGHDK Jul 13 '17 at 23:36
  • @EGHDK `HEAD` is a valid tag name, so it can refer to any git object. – ElpieKay Jul 14 '17 at 01:23
  • 5
    `HEAD` is a shorthand for "commit at the tip of the branch" and is absolutely not equivalent to the branch name. It's a commit, and a branch is a pointer to a commit. These are not the same. – liberforce Dec 16 '20 at 10:08
2

These are the shortcuts I use to push and pull, including getting the branch name (not just using HEAD, which points to a commit and will cause issues when pulling):

alias push='git push origin "$(git symbolic-ref --short HEAD)"'
alias pull='git pull origin "$(git symbolic-ref --short HEAD)"'
DerTarchin
  • 73
  • 5
-1

Short answer:

If it's a branch you created, tell git with remote branch you want to track as upstream.

git push -u mySuperLongComplicatedBranchName

Then just running the command without a remote nor branch name will by default use the upstream branch you're tracking:

git pull

Long answer:

When you publish your branch using git push, if it's a branch you created yourself, git asks you to set an upstream branch to be able to track changes between your local and remote branches. This is done by using the --set-upstream or the shorter -u option. So git push origin -u <name_of_your_branch> publishes (and creates, if it didn't exist) your branch on the origin remote.

From man git-push:

-u, --set-upstream
           For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. For more information, see branch.<name>.merge
           in git-config(1).

The key info here is: argument-less git-pull.

By not specifying either the name of you remote (usually origin) nor branch name in your git pull command means: "pull from the tracked branch", meaning the one you defined as upstream.

So just running git pull to merge changes on the upstream branch or git pull -r to rebase your branch on the remote changes is what you're looking for.

liberforce
  • 11,189
  • 37
  • 48