106

I have git proxy config as 'http.proxy=http://userId:pwd@123@ipaddress:port' but while cloning a remote project, I'm getting error as

Cloning into git...
error: Couldn't resolve proxy '123@ipaddress' while accessing http://git.kernel.org/pub/scm/git/git.git/info/refs

fatal: HTTP request failed

How to escape the '@' character in password?

Pls note: I cannot change the password.

Karthik
  • 1,363
  • 2
  • 12
  • 13

5 Answers5

196

I'd try using the URL Encoded value of the @ symbol (%40) if you're passing the password in the proxy url:

http.proxy=http://userId:pwd%40123@ipaddress:port
Matthieu Charbonnier
  • 2,794
  • 25
  • 33
John Weldon
  • 39,849
  • 11
  • 94
  • 127
  • 3
    I'm trying to do this same trick but with 'git clone http://user:pwd%40123@ip:port' and it doesn't work. So I guess git doesn't use curl in this situation? – Joseph Garvin Feb 23 '12 at 16:59
  • interesting... not sure. It's probably worth trying to see what actually gets sent across the wire in this case. – John Weldon Feb 23 '12 at 17:27
  • 1
    This helped me. You should separately url-encode both name and password parts. This will help you avoid any problems with special chars. For example there can be ":" sign which will also lead to problems. – Stalinko Mar 18 '16 at 06:45
  • 2
    This is also awesome for when you have '@' in the username e.g. when you have email addresses as usernames. – ramdesh Sep 07 '16 at 11:01
  • Thank you so much for solving my problem after two days' struggle...really hard to find the reason for this kind of problem. – lleiou Feb 01 '18 at 02:42
  • Thank you @JohnWeldon it solve my issue also – Damith Udayanga Mar 16 '21 at 03:41
  • Hello. Is there a list of URL Encoded values so i can save it and consult when i need it ? – Raul Chiarella Jan 10 '22 at 14:39
79

Note (November 2013)

Encoding the url (especially any special character in a password) is the right solution.
The .netrc mentioned below is only for remote repo url, not for the proxy used to resolve said remote repo url.

For said encoding, see "Percent-encoding":

Percent-encoding, also known as URL encoding, is a mechanism for encoding information in a Uniform Resource Identifier (URI) under certain circumstances. Although it is known as URL encoding it is, in fact, used more generally within the main Uniform Resource Identifier (URI) set, which includes both Uniform Resource Locator (URL) and Uniform Resource Name (URN). As such, it is also used in the preparation of data of the application/x-www-form-urlencoded media type, as is often used in the submission of HTML form data in HTTP requests.

Reserved characters after percent-encoding:

!   #   $    &   '   (   )   *   +   ,   /   :   ;   =   ?   @   [   ]
%21 %23 %24 %26 %27 %28 %29 %2A %2B %2C %2F %3A %3B %3D %3F %40 %5B %5D

Original answer (May 2011)

Two comments:

  • having a password for a server accessed with http (not https) is... strange. The password isn't encrypted during communications between client and server;

  • you could setup a .netrc (or _netrc for Windows) in your $HOME, with the following content

    machine ipaddress:port
    login userId
    password pwd@

The curl used by Git bbehind the scene would handle the encoding just fine, @ or no @.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for ur answers. The server was network drive where only myself and other 2 developers will be using. So I think there is no need for encryption. And I will try using netrc sometime. – Karthik May 30 '11 at 09:01
  • @Karthik: that will allow you to simply use: `http://123@ipaddress:port/...` as a cloning address, without having to add user and password information. – VonC May 30 '11 at 09:19
12

You have to percent-encode | encode the special characters. E.g. instead of this:

http://user:Pa@s@http-gateway.domain.org:80

you write this:

http://user:Pa%40s@http-gateway.domain.org:80

So @ gets replaced with %40.

Ravi Parekh
  • 5,253
  • 9
  • 46
  • 58
11

URL encode any unusual characters.

List of url codes.

@ character is %40

In my git config file, I have encoded 'just' the user name for instance:

https://myemail%40gmail.com@myrepo.org/api.git
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
conal_lab24
  • 154
  • 1
  • 2
7

For example, your password stored in environment variable GIT_PASSWORD, username - GIT_USERNAME, then:

git clone http://${GIT_USERNAME}:$(echo -n $GIT_PASSWORD | hexdump -v -e '"x" 1/1 "%02X"' | tr x %)@repository.git

Explanation of: echo -n $GIT_PASSWORD | hexdump -v -e '"x" 1/1 "%02X"' | tr x %

  1. Print password: $GIT_PASSWORD <- hello
  2. Convert 'hello' to hex: hello <- x68x65x6Cx6Cx6F
  3. Change each 'x' to '%': x68x65x6Cx6Cx6F <- %68%65%6C%6C%6F
isnullxbh
  • 807
  • 13
  • 20