2

I've been using git in the console for the last couple of months and I feel like there's a little too much typing.

Here's my flow (assuming I'm on foo branch):

[ ... working ... ]
git add .
git commit -m "Commit message"
git pull origin foo
git push origin foo

I know it's one of first world problems but is there a way of cutting the typing here? Can I just use git pull and git push for example? What's the difference between git push, git push origin foo?

Wordpressor
  • 7,173
  • 23
  • 69
  • 108

5 Answers5

7

For git push, you can configure to automatically push to remote branch with the same name as local using configuration.

git config push.default current

https://git-scm.com/docs/git-config#git-config-pushdefault

For git pull, you will need to make local branch tracking branch to remote branch.

Refer to https://stackoverflow.com/a/19279975/1433665

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • The referenced so question is `the` reference, for me. – Pierre Cordier Feb 14 '20 at 09:27
  • I've been wondering how to do this for ages while also putting off the time to figure it out. I always just want to push the current local branch I am on when I run this command and I think "typing out the same branch I am on seems redundant and unnecessary". Thanks for this. – Mayron Aug 11 '22 at 10:23
2
git commit -am 'subject: research the commands, Luke!'
git push

is equivalent unless you've added new files yourself, so you need to tell Git to hunt them down and track them with git add ., or git push sees new history on the remote branch you're tracking that you didn't know about already, so you need to pull first.

jthill
  • 55,082
  • 5
  • 77
  • 137
2

You can also use the following shortcut to push your local branch to the remote repository and create a new branch with the same name on the server

git push -u origin HEAD

In this way, you avoid typing

 git push --set-upstream origin _your_possibly_very_long_branch_name
Cristian Rusanu
  • 452
  • 5
  • 15
1

Not a "shortcut" actually, but definetely reduce amount of typing:

Once:
git push --set-upstream origin foo

Then:
git push and git pull

Dmitry Demidovsky
  • 7,557
  • 4
  • 22
  • 22
  • I don't get this workflow, I've been doing this for a while but it's way too easy to forget to set upstream if you have a lot of branches. – Wordpressor Jan 26 '18 at 10:30
  • Ok, I understand you. Maybe you could use aliases in **~/.bashrc** like ```alias gplo="git pull origin"``` and ```alias gpho="git push origin"``` in order not to break existing workflow. – Dmitry Demidovsky Jan 26 '18 at 10:39
0

To push your local branch: git push origin head
To pull to your local branch: git pull origin $(git branch --show-current)

I realise that the last one is a bit too long to type, so you can wrap it in an alias like this:
alias gitpull='git pull origin $(git branch --show-current)'

Then just execute gitpull