10

Is it possible to have git pull and git push in one git command?

The syntax like git pull & git push doesn't suit me completely, since I need to provide my credentials to the server twice: on pull and on push.

So I wondering, is there any workaround for this? I believe it should be, since it's pretty common case when developer pulls remote origin before pushing local changes.

EDIT: I'm using Windows 7/x64, msysgit-utf8 1.7.9

shytikov
  • 9,155
  • 8
  • 56
  • 103
  • 1
    It can be dangerous if there're merge conflicts no ? – Sandro Munda Jun 22 '12 at 12:39
  • 1
    What the protocol? there are ways to provide credentials only once per session – CharlesB Jun 22 '12 at 12:45
  • 3
    Don't do it. Set up public key authentication or change the protocol so that you can. – wadesworld Jun 22 '12 at 12:49
  • @SandroMunda I haven't had situation you're talking about, but I believe if git will allow pushing when working folder in faulting state (merging state actually) this is serious bug. – shytikov Jun 22 '12 at 13:01
  • @CharlesB I've checked it using SSH and HTTPS, and it requires me to provide password twice. I'm using Windows 7, msysgit-utf8 1.7.9 – shytikov Jun 22 '12 at 13:04
  • If you haven't already got one, generate an ssh key pair. Then use an ssh agent (e.g. [Pageant](http://the.earth.li/~sgtatham/putty/0.62/htmldoc/Chapter9.html#pageant)) to store your ssh key. – Jonathan Wakely Jun 22 '12 at 14:14
  • See: http://stackoverflow.com/questions/3669001/getting-ssh-agent-to-work-with-git-run-from-windows-command-shell – Daenyth Jun 22 '12 at 14:42

3 Answers3

9

First of all, you can configure _netrc file that will be used by Git, here is the related issue: Git - How to use .netrc file on Windows to save user and password Then you can configure an alias for the command:

git config alias.publish '!git pull && git push'

And just type: git publish

Aymen
  • 1,476
  • 18
  • 28
lisachenko
  • 5,952
  • 3
  • 31
  • 51
  • I can add login to URL of repository using @ symbol as separator. And I can add my password to `.git/config`. But I don't want to store my passwords. I like to be asked with password. But only once! – shytikov Jun 22 '12 at 14:00
  • You do not need to add an login and password to the .git/config. It's a wrong way. If you use ssh connection, then is't better to use public keys for that. For https connection by credentials you can use _netrc file in windows, pay attention to the underline in the file name instead of dot. – lisachenko Jun 22 '12 at 14:14
  • If you want to implement a custom logic for asking password, then you can use `core.askpass` for that and make an alias for login: `git config url.https://user@host/.insteadof https://host/` – lisachenko Jun 22 '12 at 14:19
  • Nice! I added the --global flag to make it global. – André Luiz Müller Feb 25 '21 at 20:00
0

I recommend doing something just quick and dirty in this case. It may not be the most efficient way to do it, but I would just create a bash script in vim with the commands and run that considering how simple the commands are. Then you can just open that file every time you want to pull and push at the same time.

All you have to do is create the file, put in the two lines of commands you want to write, and type that filename into the command line whenever you want to execute them.

Again, not the most efficient way, but a quick fix.

SSEMember
  • 2,103
  • 4
  • 20
  • 28
  • 1
    It does not fix situation with double authentication... And that's the problem. It doesn't bother me to type `git pull & git push`. I don't want to type my password twice, since I believe should be optimization for this. But I haven't found it yet... – shytikov Jun 22 '12 at 14:15
0

You'd better use SSH, but if you can't use SSH, you can always use the windows credentials store to save your password so you don't need to fill it in twice:

git config --global credential.helper wincred

Source: Git documentation

Highmastdon
  • 6,960
  • 7
  • 40
  • 68