A simpler alternative which does not involve any external scripts is to use a SSH alias. I know the original poster asked specifically not to change ~/.ssh/config, but I suspect there is a misunderstanding here.
The local user on the server is not the same as the person doing the commit and can be a different person than the one doing the 'git push'.
- on the server the hosting software can run as a single user (usually 'git')
- the identity of the person doing the commit is only git's buisness (to add to commit's meta data), is irrelevant for the server and is not subject to authentication on the server
- the identity of the 'git push'-er is relevant and is established on
systems running the git hosting software on the server based on the ssh key
For this reason, on the system doing the push one can force a specific identity even for the same local account and the same remote server, even within the same git repository by using an ssh alias following using the method explained below.
Assume you have on the gitorious.org server your regular account, let's call it 'developer'.
You don't want to automatically push using your 'developer' account [1], so you create another gitorious account for the sync, let's call it 'robot'.
For automation only the 'robot' account will be used:
Step 1: Add 'robot' to the gitorius project which needs to be pushed to.
Step 2: On the local machine create a paswordless key (this will be associated with the robot account on gitorious).
ssh-keygen -f ~/.ssh/id_rsa_robot
Step 3: upload the public key ~/.ssh/id_rsa_robot.pub on gitorious in the 'robot' account.
Step 4: The git SSH URIs on gitorious have the format git@gitorious.org:prj_or_user/subproject.git. In your ~/.ssh/config file add the following lines:
host robot.gitorious.org
HostName gitorious.org
IdentityFile ~/.ssh/id_rsa_robot
IdentitiesOnly "yes"
This will make sure that:
- whenever your use the 'robot.gitorious.org' hostname it will connect
to gitorious.org (HostName option),
- it will use the passwordless key to authenticate as robot on
gitorius.org (IdentiFile option) and
- even if you have a ssh agent running, it will ignore the default key
and use the passwordless one (IdentiesOnly "yes").
Step 5: Assuming the SSH URI on gitorious for your project is '[email protected]:project/project.git', in the local repository define a new remote 'autopush' with a slightly modified host name:
git remote add autopush [email protected]:project/project.git
The setup is done, now try to push to gitorious via the 'autopush' remote.
git push autopush master
If everything went well and there are changes to push, you should see you succesfully pushed to 'gitorious.org' as 'robot'
[1] For automatic pushes a passwordless key must be generated for the account, but attaching it to the gitorious 'developer' account would mean that the automated job can push to any of the gitourious projects where 'developer' is involved on gitorious.