DEV Community

Maria Campbell
Maria Campbell

Posted on

Git instaweb and local Git commits

This post was originally published on interglobalmedianetwork.com.

I recently came across “git instaweb”. I had never heard of it before, and wanted to learn more about it. I first
learned
that it permits me to browse my local repositories on a localhost via something called GitWeb.

In order to be able to use git instaweb successfully on macOS (currently I am on macOS Ventura, and what I was on when trying to implement git instaweb), you have to
have lighttpd installed as well. It can be installed using the Homebrew package manager for macOS, and luckily, I
already had it installed and have installed many a package on my MacBook Pro via Homebrew.

But what IS lighttpd? It is

A secure, fast compliant, and very flexible web server that has been optimized for high-performance environments. lighttpd uses memory and CPU efficiently and has lower resource use than other popular web servers. Its advanced feature-set (FastCGI, CGI, Auth, Output-Compression, URL-Rewriting and much more) make lighttpd the perfect web server for all systems, small and large. lighttpd is released under the Open Source revised BSD license. - the LIGHTTPD docs

In order to install lighttpd with Homebrew, run the command brew install lighttpd. Once you have it installed, you will be able to successfully run the git instaweb command in Terminal (or in my case, iTerm2). If
you do not have Homebrew installed on your Mac, please visit the Homebrew website for installation
instructions
.

git instaweb lets you browse your working repository in GitWeb. It
is a simple script/command to set up GitWeb and a web server inside your local repository so that you can browse it. GitWeb
is also known as “Git on the Server”, and it is a simple web-based visualizer. Git comes with this feature, and GitWeb is a CGI (Common Gateway Interface) script which enables web-based visualization.

A CGI, aka Common Gateway Interface,

is an interface specification that enables web servers to execute an external program, typically to process user requests. Such programs are often written in a scripting language and are commonly referred to as CGI scripts.

A typical use case occurs when a web user submits a web form on a web page that uses CGI. The form’s data is sent to the web server within an HTTP request with a URL denoting a CGI script. The web server then launches the CGI script in a new computer process, passing the form data to it. This output of the CGI script, usually in the form of HTML, is returned by the script to the Web server, and the server relays it back to the browser as its response to the browser’s request. Developed in the early 1990s, CGI was the earliest common method available that allowed a web page to be interactive. - Common Gateway Interface on Wikipedia

We are not dealing with traditional HTTP requests in this particular scenario or HTML forms, but the above information still
gives you an idea of what a CGI script is. And with the help of git instaweb and lighttpd, there are a number of options, including an --httpd daemon command line that can be executed.

And what does daemon mean? According to a thread entitled "Why should I create a daemon instead of a command line tool?" on stackoverflow,

A daemon is typically a background process, so if you want your program to sit in the background and monitor something (e.g. report log file size increase every 5 minutes the last hour) and then report the results when someone watches a web page, a deamon is the correct choice.

If you instead want to do something that can be done immediately (e.g. report current log file size), a command line tool is easier to create and maintain.

So that is why we would use a daemon which would be triggered by a Git command in the Command Line Interface (aka
Terminal) on macOS.

This all is so fascinating, and I did not realize until now that we could surf our local repositories as if we were on a
remote server such as github.com!

For example, if I go into my local repository called example-portfolio-site-github and run the Git command
git instaweb, the following is returned:



2022-12-27 10:12:59: (network.c.540) can't bind to socket: 0.0.0.0:1234: Address already in use
Could not execute http daemon lighttpd -f.


Enter fullscreen mode Exit fullscreen mode

However, if I run the Git command git instaweb -p 4321, for example, defining a specific port, 4321 in this case, a web browser instance opens up in my default browser, and for me, my example-portfolio-site-github local repo looked like the following:

Image description

The above page lists the remote branches associated with the local repository via SSH. There is a remote main branch and a
remote gh-pages branch on Github.

If I click on the “summary” link to the right of “Last Change”, it looks like the following:

Image description

“summary” contains the shortlog of the latest commits I made to the local repository under “shortlog”, and the local branches associated with the local repo are listed under “heads”. That is because a Git “head” refers to the
branches that have been checked out in the history of the local repository. If I run the command git branch, which lists all the branches associated with my local repository, in the case of this repository, I get back the following:



hamburger-icon-from-scratch
main
refactor-app-to-match-namecheap-version


Enter fullscreen mode Exit fullscreen mode

And this is what it looks like in VS Code’s Integrated Terminal:

Image description

“remotes” refers to the remote repository associated with the local repository. Here, “origin” is listed, and the url which is associated with origin on Github, is
git@github.com:interglobalmedia/example-portfolio-site-github.git. If I go into this repository on Github, and select the green
“Code” button, I get the following:

Image description

Since I use SSH when pushing local commits to my remote origin branch on Github, my SSH url is associated with my git push origin main commands. How do I know that?
Because when I push my first commit to my remote branch on Github, I run the Git command git remote add origin git@github.com:interglobalmedia/example-portfolio-site-github.git. This is the git remote add origin
command I ran for this particular local repository, so that it would become associated with its remote url created by
Github when I created the remote repository there.

When I actually pushed the changes to remote origin for the first time, I ran the command git push -u origin main. What does the -u stand for? -u is short for upstream remote, and it is used to set origin as the upstream remote in your git config. So when you push your local branch commits with the git push -u option, that local branch is linked with the remote branch automatically. The advantage to this is that you may use git pull without any arguments. This means that you can run the git pull command without the need to
add the remote repository url as an argument!

Happy GitWebbing!

Related Resources

Top comments (0)