2

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
jww
  • 83,594
  • 69
  • 338
  • 732
SkyWalker
  • 11,704
  • 14
  • 65
  • 144
  • 3
    Configure your shell to show the current branch in its prompt. Example: https://qph.fs.quoracdn.net/main-qimg-0e0f78f0d478987c0443fcf9f7274cde.webp – jweyrich Jun 22 '19 at 07:10
  • 1
    `xargs -t` will print what it's doing, though perhaps that's too late for your use case. – tripleee Jun 22 '19 at 07:19

5 Answers5

2

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.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
1

You can set git aliases in ~/.gitconfig:

[alias]
    p=!git status && git pull origin $(git branch | sed 's/.* //')
    r=!git status && git push origin $(git branch | sed 's/.* //')

Usage:

$ git branch
  master
* other
$ git p
On branch other
Your branch is up to date with 'origin/other'.
MAGA
  • 6,370
  • 3
  • 14
  • 35
1

To quickly display only current branch name you can use :

git symbolic-ref --short HEAD
Rajni Kewlani
  • 543
  • 3
  • 18
1

If you set up a tracking branch and set push.default to 'upstream' or similar then git pull/git push without arguments will default to that branch.

To set 'push.default' run git config --global push.default upstream.

If you originally pulled the branch from a remote then a tracking branch should have been created automatically. If you created the branch locally then you can create a tracking branch by including the -u option when you push:

git push -u <remote> <branch>
Calum Halpin
  • 1,470
  • 1
  • 8
  • 15
  • True, see my edited answer. I do address the right branch name to use when pushing. – VonC Jun 22 '19 at 08:53
1

I like to use git-prompt to solve this problem. Besides my hostname and current path, my prompt always shows what branch I'm on, so it's immediately obvious what I'm about to push.

https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

joanis
  • 4,360
  • 4
  • 21
  • 30