6

I have two git repositories on different networks. I had been communicating between the two of them without problem but for some reason today when I am doing a "git push", I am getting the following error:

----------------------------------------------
bash: git-receive-pack: command not found
fatal: The remote end hung up unexpectedly
----------------------------------------------

I googled and made sure that the "/usr/local/bin" was in my "$PATH". Here is the output of the bin directory of my git:

[pradeep@laptop ]$ls -l /usr/local/git/bin/
total 16760
-rwxr-xr-x  1 root  wheel  4329416 Mar 26 20:06 git*
-rwxr-xr-x  1 root  wheel    14852 Mar 26 20:06 git-credential-osxkeychain*
-rwxr-xr-x  2 root  wheel   162402 Mar 26 20:06 git-cvsserver*
lrwxr-xr-x  1 root  wheel        3 Apr  3 11:02 git-receive-pack@ -> git
-rwxr-xr-x  2 root  wheel  1830248 Mar 26 20:06 git-shell*
lrwxr-xr-x  1 root  wheel        3 Apr  3 11:02 git-upload-archive@ -> git
-rwxr-xr-x  2 root  wheel  1893064 Mar 26 20:06 git-upload-pack*
-rwxr-xr-x  1 root  wheel   333121 Mar 26 20:06 gitk*

Any ideas what might be going wrong?

Thanks

-------------- Edit ------------------- I have the following line in the ".bashrc" of my remote system:

export PATH=$LOCAL/git-1.8/bin/:$PATH

This is the output on the remote machine:

$ls $LOCAL/git-1.8/bin
git  git-cvsserver  gitk  git-receive-pack  git-shell  git-upload-archive  git-upload-pack

And this is the output when I do "ssh user@remote env" from my machine to the remote machine:

[pradeep@laptop ~]$ssh k00603@k.rics.riken.jp env
SHELL=/bin/bash
SSH_CLIENT=153.133.52.171 52379 22
USER=k00603
MAIL=/var/mail/k00603
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/openssh/bin
PWD=/volume2/home/hp120242/k00603
SHLVL=1
HOME=/home/hp120242/k00603
LOGNAME=k00603
SSH_CONNECTION=153.133.52.171 52379 10.7.160.4 22
LC_CTYPE=en_US.UTF-8
_=/bin/env

The git path is missing in the bash.

Edit: The following is the ".bash_profile" in the home directory of my remote system:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:$LOCAL/git-1.8/bin

export PATH
Pradeep Kumar Jha
  • 877
  • 4
  • 15
  • 30
  • 1
    Is this `/usr/local/git/bin/` in your $PATH? – number5 Apr 03 '13 at 02:58
  • Yes. [pradeep@laptop]$echo $PATH /Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/Users/pradeep/softwares/hdf-java/bin/:/Users/pradeep/softwares/ParaView/paraview-bin/bin/:/Users/pradeep/softwares/ParaView/paraview-bin/bin/paraview.app/Contents/MacOS/:/Users/pradeep/code/python_utilities/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:/usr/texbin:/Users/pradeep/bin – Pradeep Kumar Jha Apr 03 '13 at 03:09
  • You should be checking the `PATH` on the remote, not from the machine you're trying to `push` from. I'm saying this because `pradeep@laptop` does not look like a server for the remote. If it is indeed then, ignore this comment. – Tuxdude Apr 03 '13 at 03:18
  • On the remote machine I dont have root permissions so I have installed git in my local account. And the bin directory of the locally installed git is in my PATH declaration. – Pradeep Kumar Jha Apr 03 '13 at 04:24
  • @jhaprade - Does your remote url use the local account's username or does it have a separate account ? – Tuxdude Apr 03 '13 at 05:35
  • it has a different username. it is completely unrelated to my local account. – Pradeep Kumar Jha Apr 03 '13 at 09:05

1 Answers1

7

When you ssh into a box and get a prompt, you're actually going through a slight different process than when running a command. Shells have rules about what configuration gets sourced based on whether it's a login shell and/or if it's an interactive session. Try running:

ssh user@host env

This will print your environment as seen by a non-interactive login session. My guess is that /usr/local/git/bin is not on the path in this scenario. You can work around this by using the --receive-pack option for git push:

git push --receive-pack=/path/to/git-receive-pack

/path/to/git-receive-pack is the path on the remote machine. It tells git where to find the other end when ssh'ing into the remote.

The best answer is to fix your shell configuration, but that can be tricky depending on the shell. I mainly use ZSH, and I fix this by setting up a ~/.zshenv, which gets sourced by ZSH in a non-interactive shell. I think things tend to be less clear with bash. You may have to edit your ~/.bashrc and/or your ~/.bash_profile.

Assuming you have a vanilla Linux installation on the remote end (which is probably the case), and that it's running the bash shell, the easiest fix is to change your .bashrc. Edit ~/.bashrc, and add:

export PATH=/path/to/your/installed/git/bin:$PATH

Where /path/to/your/installed/git/bin is where you installed git in your home directory. I tend to put local installs in ~/.local/bin, so mine would look like:

export PATH=$HOME/.local/bin:$PATH

Your's may simply be:

export PATH=$HOME/bin:$PATH

You don't say the location, so I can't give you the correct formulation.

On most systems, .bashrc is sourced even when a command is provided to ssh. After you make this change, try running ssh user@host env again. You should see your the location of the bin folder holding the git binary in the path variable. You should then be able to do something like ssh user@host git --version and see something like:

git version 1.8.2

If you see an error, then you likely didn't type the path correctly, or .bashrc isn't being picked up when you ssh into the machine.

Sample .profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Automatically using --receive-pack

Assuming your remote is named origin, you can do this:

git config --local remote.origin.receivepack \
    /path/to/git-receive-pack/on/the/remote

You may need to fix upload pack as well:

git config --local remote.origin.uploadpack \
    /path/to/git-upload-pack/on/the/remote
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79
  • I have already tried the --receive-pack option for git push that you suggested but it is not working. When I do the first ssh command that you recommended, I am getting the bin folder in which my git executables are stored in the list of the PATH. I am surprised because the git was working perfectly fine between my laptop and the remote system a couple of weeks before. I dont understand why should it stop working all of a sudden. – Pradeep Kumar Jha Apr 04 '13 at 11:16
  • Hmmm... if `--receive-pack` isn't working, then either you mistyped the path, it's a broken link, or it's just plain broken. Does running `git-receive-pack` on the far end work? It should just print out a help message. And what is the output of `ssh user@host 'type -f git-receive-pack'`? I'm not sure why it would have stopped working either. It smells like something in the environment changed. Does `user@host` have permission to run the `git-receive-pack` executable? – John Szakmeister Apr 04 '13 at 11:36
  • hi, sorry for the late response. I was looking again and I indeed don't have the bin directory of the git folder in the output of "ssh user@host env". But how do I fix it? When you say to use "git push --receive-pack=/path/to/git-receive-pack", should be the path of "git-receive-pack" on my local machine or the remote machine? Can you show me by an example, please, if possible. – Pradeep Kumar Jha May 15 '13 at 05:18
  • The easiest fix is to edit your .bashrc to include the folder with git in it. I updated my response showing more about how to do that. If you can get the `PATH` fixed, then you won't need to worry about `--receive-pack`. For the record though, the argument to `--receive-pack` should be the path on the *remote*. So, `git push --receive-pack=/home/username/bin/git-receive-pack`, or similar. But the best choice is just fixing the path on the remote end. – John Szakmeister May 15 '13 at 08:45
  • I have edited my original post to give you the idea of my present settings. I think I have done what you recommended but the ssh is still not seeing the git path on the remote machine. – Pradeep Kumar Jha May 15 '13 at 14:58
  • If you just ssh into the box and run `git --version`, it works, correct? What OS/Distro is the server running? It smells like `~/.bashrc` isn't being sourced for some reason, or it's bailing out early (you may see it checking something like `[ -z "$PS1" ]` to determine this, since there is no `PS1` variable on a non-interactive shell). On my Ubuntu box, I see it try to source `/etc/bash.bashrc`, but stops because it checks for an interactive shell at the beginning, then it sources the .bashrc in my home folder. Is your export at the beginning of your .bashrc? – John Szakmeister May 16 '13 at 01:24
  • Actually when I run the "git --version" it is telling me "bash: git: command not found". This is bizzare. When I actually log in to the system and work there, everything that I have in my bashrc works completely fine. Even I can use git without any problem inside the remote system. I dont know the distro. It is a Fujitsu machine running Linux. – Pradeep Kumar Jha May 16 '13 at 03:30
  • That really sounds to me like your .bashrc isn't getting sourced. Now, when you ssh into the box, that might be because your .profile, or .bash_profile isn't sourcing .bashrc. I've updated my answer with a copy of it, just for reference. When using `ssh user@host` (no command provided), it should source your system profile, the main /etc/bash.bashrc, and if there's a `~/.profile`, `~/.bash_profile`, or `~/.bash_login`, it'd source that to. From there, one of those should hopefully source .bashrc (but it's not). When you have a command though, bash should only source the two .bashrc's. – John Szakmeister May 16 '13 at 08:40
  • There is no ".profile" file in the home folder of my remote machine. There is a ".bash_profile" and I have but the contents of that in my original post. I even added a ".profile" myself and added your lines but that also doesn't work. I don't understand why ssh is not reading the $PATH in the ".bashrc" :( – Pradeep Kumar Jha May 17 '13 at 02:49
  • The distro on the remote server is "Red Hat Enterprise Linux Server release 5.7 (Tikanga)" – Pradeep Kumar Jha May 17 '13 at 03:39
  • @jhaprade " I don't understand why ssh is not reading the $PATH in the ".bashrc""? This could hepl explaining why: http://stackoverflow.com/q/820517/6309 and http://superuser.com/a/340445/141 – VonC May 17 '13 at 05:47
  • Yes, I read one of those posts. I have posted my problem again stating it differently here http://stackoverflow.com/questions/16600797/ssh-remote-command-no-reading-all-environment-variables/16601036?noredirect=1#comment23864161_16601036 – Pradeep Kumar Jha May 17 '13 at 05:54
  • @jhaprade While I think it's worth the effort to figure out how to get `PATH` set up correctly, in the meantime you can still use the `--receive-pack` argument. In your case, it'll look something like `git push --receive-pack=/home/hp120242/k00603/git-1.8/bin/git-receive-pack ...`. – John Szakmeister May 17 '13 at 08:25
  • I managed to contact the sysad and he said that for ssh to read the .bashrc file, I should use "ssh user@remote 'source .bashrc; env' " and this is returning me the PATH where I have installed git. But is there a way to use this format with git-push? – Pradeep Kumar Jha May 22 '13 at 07:31
  • @jszakmeister oh! looks like there was some typo when I tried the command you recommended before. I tried it again and its working fine. Thanks!! – Pradeep Kumar Jha May 22 '13 at 07:32
  • @jhaprade Great! I looked it up, and there is a `remote..receivepack` config option. I've updated my answer to include it (down at the bottom). You probably need to set `remote..uploadpack` as well. – John Szakmeister May 22 '13 at 13:04
  • @jszakmeister sorry but where should I put these lines? In the .git/config file? – Pradeep Kumar Jha May 22 '13 at 16:50
  • @jhaprade No problem. Just run the command in the config commands at the bottom of my answer, with the correct paths substituted. It'll modify your `.git/config` for the repository that you're working with. HTH! – John Szakmeister May 22 '13 at 17:38
  • @jszakmeister awesome! I ran the command in my git repository and it worked without any problems. Thanks!! – Pradeep Kumar Jha May 23 '13 at 03:16