Git: git push remote with wrong username

Holà !

When you want to add on your local workstation a remote repository so you can push your work there, you may have executed a command similar to this one :

  • git remote add web ssh://example.server.tld/home/user/git/project.git

and then, when you would like to push your modifications (and create a master branch), you did

  • git push web +master:refs/heads/master

but this time, git asked for your password, and if your workstation username is different from your username on the server, well, you gonna have a bad time !

Any way, i have the solution for you:
First you have to remove the remote from your local repository :

  • git remote rm web

and then recreate it but this time changing the url style

  • git remote add web yourusernameontheserver@example.server.tld:/home/user/git/project.git

and now, when you push your work

Counting objects: x, done.
Delta compression using up to x threads.
Compressing objects: 100% (x/x), done.
Writing objects: 100% (x/x), 7.21 KiB, done.
Total x (delta 0), reused 0 (delta 0)
To yourusernameontheserver@example.server.tld:/home/user/git/project.git
* [new branch] master -> master

Yeeha ! :)

3 thoughts on “Git: git push remote with wrong username

  1. I create configuration in my `~/.ssh/config` file like this:

    Host repo
    Hostname example.com
    User myuser

    This way I can easily ssh to the server or run git commands by specifying simply `repo` as the “hostname” instead of the full `myuser@example.com`.

    And btw, a simpler way to push a branch to your remote is like this:

    bzr push remotename branchname

    When you run it for the first time, it’s good to include the `-u` flag as well, to set up your local branch to “track” the remote branch. That way next time you can push the branch there with simply `git push`, without additional parameters.

    An even greater benefit of tracking the remote is that the status command will give you useful reminders when you are a few revisions behind or ahead of the remote, so that you know that “it’s time to push again”, or pull again.

Leave a Reply