Extremely lazy here, don’t like to type too much and specially don’t like to remember on which branch I’m in to pull from and push to. Far too often I make the mistake of doing git pull
from a non-master branch, or worse do a push and the target branch is incorrect and I’m left with un-pushed or uncommitted changes.
I created in my .bashrc
the following aliases to help:
alias gitr='git status | grep -Po "On branch K[^ ]+" | xargs git push origin'
So doing gitr
on any git repo will push the committed changes to the correct branch.
Likewise doing:
alias gitp='git status | grep -Po "On branch K[^ ]+" | xargs git pull origin'
will pull the latest changes from the correct branch I’m currently in.
Finally this one will commit all local changes as “Cosmetics” and push them to the correct branch:
alias gitcr='git commit -a -m "Cosmetics" && git status | grep -Po "On branch K[^ ]+" | xargs git push origin'
Is there a way to print all the intermediate commands without doing print in each? and is there a way to make these commands more robust?
UPDATE by print the intermediate commands I mean to show the following to be sure it is running the correct commands:
git status git pull origin somebranch
Advertisement
Answer
At least, with Git 2.22, you can use:
git branch --show-current
That would avoid a few grep/awk/sed
Plus, if you want to push, you need the name of the remote tracking branch associated to the current branch (you could have associated the remote ‘bar’ branch to your local ‘foo’ branch’
That is [<branchname>]@{push}
, e.g. master@{push}, @{push}
git rev-parse --abbrev-ref --symbolic-full-name @{push}|cut -d / -f 2
Again The suffix @{push}
reports the branch “where we would push to” if git push
were run while branchname was checked out.
Note: git rev-parse --abbrev-ref --symbolic-full-name @{push}|cut -d / -f 1
will give you the name of the remote, which is not always origin
.
It is not the same as [<branchname>]@{upstream}
, e.g. master@{upstream}
, @{u}
.
The suffix @{upstream}
to a branchname
(short form <branchname>@{u}
) refers to the branch that the branch specified by branchname
is set to build on top of (configured with branch.<name>.remote
and branch.<name>.merge
).
Ie the branch you are pulling from.
See “Viewing Unpushed Git Commits” for the Git 2.5+ @{push}
shortcut notation.
Git 2.38 (Q3 2022) clarifies the @{upstream}
shortcut.
See commit 8cdab69 (23 Jun 2022) by Tao Klerks (TaoK
).
(Merged by Junio C Hamano — gitster
— in commit 9a13943, 13 Jul 2022)
rev-parse
: documentation adjustment – mention remote tracking with@{u}
Signed-off-by: Tao Klerks
The documentation explained the conversion from remote branch path to local tracking ref path for
@{push}
, but not for@{upstream}
.Add the explanation to
@{upstream}
, and reference it in@{push}
to avoid undue repetition.
revisions
now includes in its man page:
A branch
B
may be set up to build on top of a branchX
(configured withbranch.<name>.merge
) at a remoteR
(configured withbranch.<name>.remote
).
B@{u}
refers to the remote-tracking branch for the branchX
taken from remoteR
, typically found atrefs/remotes/R/X
.