Reputation: 58863
can you recommend any no-brainer solution for setting up a git repository accessible via http(s, has cleutus suggested)? I have my own http server and I'd like to use it to host some minor private project. At home I can ssh it, but at work firewalls keep me from doing so.
Is there any free way to set up a small private git repository I can push / fetch to via http so that I can share projects between home and work? Thanks in advance!
Upvotes: 21
Views: 17031
Reputation: 382672
Git dumb HTTP protocol minimal runnable example with Python http.server
Just to provide a minimal example to https://stackoverflow.com/a/2278948/895245 :
# Get a test repo.
git clone --branch v2.40.0 https://github.com/git/git
# Make a bare clone of the repo and prepare it for dumb hosting.
git clone --bare git git-bare
cd git-bare
git --bare update-server-info
# Start your favorite http file server.
python3 -m http.server
Then on another shell:
git clone http://localhost:8000 git-bare-clone
and after a few seconds git-bare-clone
contains a git repository equivalent to the original git
.
The Python server shows the files that were requested, the initial requests are:
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /info/refs?service=git-upload-pack HTTP/1.1" 200 -
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /HEAD HTTP/1.1" 200 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/26/2c45b6a17d971cd440c6cd2fdeff1e0d081e47 HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/d5/aef6e4d58cfe1549adef5b436f3ace984e8c86 HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/33/682a5e98adfd8ba4ce0e21363c443bd273eb77 HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/ca/9b793bda20c7d011c96895e9407fac2df9648b HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/8c/178f72b54f387b84388d093a920ae45b8659dd HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
127.0.0.1 - - [08/Apr/2023 15:08:40] "GET /objects/info/http-alternates HTTP/1.1" 404 -
127.0.0.1 - - [08/Apr/2023 15:08:40] code 404, message File not found
We see that some files were not found, but everything seemed to work so maybe it is expected.
Tested on git 2.37.2, Python 3.10.6, Ubuntu 22.10.
Upvotes: 2
Reputation: 10138
And what is more, DAV is significantly slower than the new "smart-http" support since git 1.6.6. The new method allows the entire pack to be transmitted at once, and not as individual files.
You can also use gitweb to provide browsable URLs at the same location.
Note: Because access is controlled by apache you can add any Auth requirements (htaccess or ldap, etc) to the setup for each repository.
Just make a new git_support.conf file, and include it in apache (add include statement in httpd.conf)
#
# Basic setup for git-http-backend
#
SetEnv GIT_PROJECT_ROOT /opt/git_repos
SetEnv GIT_HTTP_EXPORT_ALL
SetEnv REMOTE_USER=$REDIRECT_REMOTE_USER #IMportant !!! This could be your problem if missing
<Directory /opt/git> # both http_backend and gitweb should be somewhere under here
AllowOverride None
Options +ExecCGI -Includes #Important! Lets apache execute the script!
Order allow,deny
Allow from all
</Directory>
# This pattern matches git operations and passes them to http-backend
ScriptAliasMatch \
"(?x)^/git/(.*/(HEAD | \
info/refs | \
objects/(info/[^/]+ | \
[0-9a-f]{2}/[0-9a-f]{38} | \
pack/pack-[0-9a-f]{40}\.(pack|idx)) | \
git-(upload|receive)-pack))$" \
/opt/git/libexec/git-core/git-http-backend/$1
# Anything not matched above goes to displayable gitweb interface
ScriptAlias /git /opt/git/cgi-bin/gitweb.cgi/
The result is the ability to push/pull:
me@machine /tmp/eddies $ git pull
Already up-to-date.
me@machine /tmp/eddies $ touch changedFile
me@machine /tmp/eddies $ git add .
me@machine /tmp/eddies $ git commit -am"commiting change"
[master ca7f6ed] commiting change
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 changedFile
me@machine /tmp/eddies $ git push origin master
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 239 bytes, done.
Total 2 (delta 1), reused 0 (delta 0)
To http://mysecretdomain.com/git/eddies
0f626a9..ca7f6ed master -> master
And you can browse those changes online..
Source: http://repo.or.cz/w/alt-git.git?a=blob_plain;f=gitweb/README
Upvotes: 2
Reputation: 2829
Git supports this natively. You'll need an HTTP server, of course.
Put your (bare) repository in a folder that can be accessed by your web server. In that directory, run the following commands:
$ git --bare update-server-info
$ mv hooks/post-update.sample hooks/post-update
The first command provides extra information so the web server knows what to do with the repository. The second command makes sure that the information gets updated any time someone pushes to the repository.
You can find that information here: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#setting-up-a-public-repository
Upvotes: 30