3

I really love the ability to use - as shorthand to refer to the last branch I was on.

I was wondering if there is shorthand to refer to the current branch I am on? For example,

Before

$(dev/my_branch)    git push -f origin dev/my_branch
$(dev/my_branch)    git branch --set-upstream-to=origin/dev/my_branch

After

$(dev/my_branch)    git push -f origin .
$(dev/my_branch)    git branch --set-upstream-to=origin/.
  • 1
    As both answers noted, `HEAD` means *the current branch*. However, it also means *the current commit*. It depends on how you ask the question. Use `git rev-parse --symbolic-full-name HEAD` or `git symbolic-ref HEAD` to ask the question *what is the **name*** and use `git rev-parse HEAD` to ask the question *what is the **hash ID***. – torek May 14 '20 at 22:49
  • The `git push` command is interesting in that it actually needs to ask *both* questions, internally, and will do so. – torek May 14 '20 at 22:50
  • 1
    Last, note that `@` by itself is a short way to spell `HEAD`. Given that you like `-` for "previous branch name" you might like this one-letter spelling. :-) None of this works for your `--set-upstream-to`, though, which demands that you spell out `origin/dev/my_branch`. I like to use `git push -u origin HEAD` to set it up initially, and then after that, just `git push -f` with no argument to update the current name after a rebase, provided I'm on a branch everyone agrees gets rebased like this. – torek May 14 '20 at 22:51

3 Answers3

4

The shorthand to refer to the current branch is --:

Suppose, you are on a branch called 'feature/old-branch-name`, wishing to rename it.

git branch -m -- feature/new-branch-name

The current branch was renamed!

Maksym Dudyk
  • 1,082
  • 14
  • 16
  • This does not work in other situations. For example, `git pull origin --` You asked to pull from the remote 'origin', but did not specify a branch. Because this is not the default configured remote for your current branch, you must specify a branch on the command line. – Ian Aug 23 '23 at 02:54
3
git push origin HEAD

HEAD will always refer to the latest Commit of your current branch.

Alternative:

You can configure git to push to the current branch using the following command

git config --global push.default current

then just do

git push 

this will push the code to your current branch.

Rakmo
  • 1,926
  • 3
  • 19
  • 37
-1

HEAD will refer to the current branch

user2199860
  • 788
  • 4
  • 14