48

Checking the environment variables and also HTTP configuration options does not reveal something. Is there a way to do this?

nobeh
  • 9,784
  • 10
  • 49
  • 66

5 Answers5

78

The more generic solution (that also works for BSD) is to edit the global /etc/ssh/ssh_config or per-user ~/.ssh/config and add/replace the entry:

AddressFamily any 

with the following line (where inet corresponds to ipv4):

AddressFamily inet

You can also set this for just a single host:

Host example.com
    AddressFamily inet
Pedram
  • 921
  • 1
  • 10
  • 17
Anthony C Howe
  • 781
  • 5
  • 6
  • 21
    Explanation: `inet` means ipv4 and `inet6` means ipv6. – user1278519 Dec 29 '16 at 11:09
  • This is only applicable if the remote host uses SSH. Given the OP mentioned checking HTTP options, it sounds like this specific case uses HTTP. – Jeremy Visser May 04 '17 at 01:18
  • 2
    This solution is perfect for bitbucket as their IPv6 solution is much much slower than their IPv4 when using git. – Colton Dec 11 '17 at 06:47
  • +1 thank you - this helped me: When I use Speedify VPN and want to push to a gitlab repo, I would usually get a long delay (almost feels like a hang/freeze) before the git push works. But I put your setting `AddressFamily inet` in the config file entry for gitlab and this now means git pushes happen quickly, near instantly and probably the same as when not running the Speedify VPN. Thank you! – therobyouknow May 06 '20 at 22:34
  • 2
    Note that on Windows, the config file is located here: C:\Program Files\Git\etc\ssh\ssh_config. See also: https://stackoverflow.com/questions/26266778/ssh-config-file-for-windows-git – Steve Melnikoff Nov 18 '20 at 10:17
42

With git 2.8 (March 2016), you can force git fetch/push/clone to use IPV4 or IPV6.
(for git pull, see below Git 2.16, Q1 2018)

See commit c915f11 (03 Feb 2016) by Eric Wong (ele828).
(Merged by Junio C Hamano -- gitster -- in commit e84d5e9, 24 Feb 2016)

connect & http: support -4 and -6 switches for remote operations

Sometimes it is necessary to force IPv4-only or IPv6-only operation on networks where name lookups may return a non-routable address and stall remote operations.

-4, --ipv4:

Use IPv4 addresses only, ignoring IPv6 addresses.

-6; --ipv6:

Use IPv6 addresses only, ignoring IPv4 addresses.


Update Git 2.16 (Q1 2018): Contrary to the documentation, "git pull -4/-6 other-args" did not ask the underlying "git fetch" to go over IPv4/IPv6, which has been corrected.

See commit ffb4568 (19 Nov 2017) by Shuyu Wei (``).
(Merged by Junio C Hamano -- gitster -- in commit c2b6135, 27 Nov 2017)


With Git 2.29 (Q4 2020), "git fetch --all --ipv4/--ipv6(man)" forgot to pass the protocol options to instances of the "git fetch" that talk to individual remotes, which has been corrected.

See commit 4e735c1 (15 Sep 2020) by Alex Riesen (ar-cetitec).
(Merged by Junio C Hamano -- gitster -- in commit 6854689, 22 Sep 2020)

fetch: pass --ipv4 and --ipv6 options to sub-fetches

Signed-off-by: Alex Riesen

The options indicate user intent for the whole fetch operation, and ignoring them in sub-fetches (i.e. "--all" and recursive fetching of submodules) is quite unexpected when, for instance, it is intended to limit all of the communication to a specific transport protocol for some reason.


With Git 2.42 (Q3 2023), the invalid options --no-ipv4 ou --no-ipv6 are rejected early.

See commit a2dad48, commit ae2c912 (18 Jul 2023) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit 9562f19, 27 Jul 2023)

fetch: reject --no-ipv[46]

Now we have introduced OPT_IPVERSION(), tweak its implementation so that "git clone"(man), "git fetch"(man), and "git push"(man) reject the negated form of Use only IP version N options.

You will therefore see "unknown option" if you are using --no-ipv[46].

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    @nobeh Yes, I just compiled and tested git 2.8-rc0 in my ubuntu. That seems to work just fine. – VonC Feb 27 '16 at 17:38
  • 1
    @nobeh I believe this answer is better than my (currently accepted) answer. Consider marking this one as the accepted answer. – Jeremy Visser Nov 14 '18 at 03:30
14

In 99% of cases, you should not be doing this. The real answer to the question is fix your IPv6 connection.

Failing that, you can edit /etc/gai.conf to prefer IPv4 over IPv6. gai.conf modifies the behaviour of getaddrinfo(), which almost all IPv6–supporting applications use to resolve hostnames.

Almost all systems ship with a copy of /etc/gai.conf within their glibc or libc package. If it is missing from /etc, usually I find a template copy is lurking somewhere within /usr/share/doc, for you to copy into /etc and modify accordingly.

Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
  • 11
    It's not MY connection that needs fixing, but the WIFI-du-jour that I happen to sit on... – Volker Stolz May 19 '14 at 11:19
  • Was not aware of this file. Good redirection away from solving an issue at the wrong place. – sshow Oct 19 '17 at 17:39
  • 1
    This seems to be a good solution, as it just changes order of preference, just uncomment the 4th line in comment as mentioned in gist ( Note: Not my gist ) https://gist.github.com/hackjutsu/d189a0cc489d54e3920041e1231fb179 – Haribk Aug 30 '21 at 08:43
  • "The real answer to the question is fix your IPv6 connection.". This sounds like great advice, thanks. I indeed prefer to understand this and fix it b/f having to resort to workarounds. However, can you give some pointers as to what may be wrong with the connection? I mean, is it a WiFi, router or nm Network adapter setting? – Kiteloopdesign Feb 28 '23 at 00:10
  • @Kiteloopdesign There are so many variables at play I literally can’t give specific advice. Everything you’ll need to check literally falls under general network troubleshooting. – Jeremy Visser Mar 02 '23 at 02:21
1

There are situations where upgrade git or where IPV6 is running but not working as expected, for example, under Windows Linux subsystem, my solutions is simple: just add the git/httpb/ssh IPV4 location on /etc/hosts.

Use host to get the IPV4 address and append it to /etc/hosts with name for your own use, for example, 1.1.1.1 git4.server.com

fcm
  • 1,247
  • 15
  • 28
0

For those that are using Ubuntu/Debian, the best way to have the latest git version is following the steps below, taken from here.

sudo add-apt-repository ppa:git-core/ppa  
sudo apt-get update  
sudo apt-get install git  

Updating for a version 2.19+ plus the solution above, proposed by Anthony C Howe has solved my problem.