How does GitHub know when to send a git repo or a web page?

How does GitHub know when someone is trying to clone a repo and how does it know when someone is just accessing the web page for it?

Is this something that can be done in Apache or nginx?

2 Likes

This is the git “smart http” protocol. Some relevant background to read: here and here. The latter mentions Apache configuration stuff.

Without digging into the details, I’d assume that the HTTP requests are different. [Browser request vs git application request] If I wanted to know more, I’d use something like fiddler to examine the network traffic.

2 Likes

When you run git clone https://github.com/github/gitignore, the http request sent by git is actually https://github.com/github/gitignore/info/refs?service=git-upload-pack. GitHub handles it by calling git instead of serving html.

Try it: open the above link (or use any other public repo) in your browser (or curl) and see what you get back.

So I could create a system like GitHub by returning the refs file for a request too https://github.com/github/gitignore/info/refs?service=git-upload-pack and the HTML file for a request to https://www.github.com/GitHub/gitignore?