Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference current branch and origin/<current> in git alias

Tags:

git

alias

As an example assume you want to write a git alias, which shows the difference between the current branch and its origin partner.

In the specific case of master it would look like the following:

[alias]
    top = log --oneline --graph --decorate master ^origin/master

How to replace master?

like image 633
erikbstack Avatar asked Sep 23 '13 13:09

erikbstack


People also ask

How to Alias the remote branch name in Git?

However, if you’re working on the same branch name as the one you wish to push changes to, then we can alias the remote branch name as HEAD . Push your commits from the local git repository to the origin or upstream remotes with a shortcut as simple as git done using this alias:

How do I list all of my local Git branches?

And to list all of your local branches, you use the git branch command. To collaborate with other developers on the same project and for them to view any changes you make, you need to push changes from your local branch to a remote repository. This leads us to remote branches. A remote branch refers to a branch that exists in a remote repository.

How do I Find my Git aliases?

Your git aliases are often stored per your user’s configuration at ~/.gitconfig. You can also manually set aliases using, for example, the command git config alias.s ‘status -s’.

How do I move back and forth between Git branches?

Use the following git alias to have a shortcut for it: Moving back and forth between git branches is also something we often do. Let’s shorten that! In fact, a git branch checkout can be done using a shortcut, such as git checkout - which checks out the previous branch you were on.


2 Answers

If your git version is not ridiculously old, the string @{u} means "upstream", i.e., whatever origin/foo the current branch is tracking. (And: HEAD means "the current branch, if on a branch", and omitting something in the .. syntax means HEAD.) Thus, @{u}.. means "everything in HEAD that is not in its upstream":

[alias]
    top = log --oneline --graph --decorate @{u}..
like image 187
torek Avatar answered Oct 24 '22 19:10

torek


One way to do it:

[alias]
    top = "!git log --oneline --graph --decorate `git rev-parse --abbrev-ref HEAD` ^origin/`git rev-parse --abbrev-ref HEAD`"

Which turns alias into a shell command, which gives you an ability to nest commands.

like image 20
Ruslan Osipov Avatar answered Oct 24 '22 21:10

Ruslan Osipov