git

From Gentoo Wiki
Jump to:navigation Jump to:search

Tip
The git repositories for Gentoo developers can be found here: https://gitweb.gentoo.org/

Git is a distributed revision control and source code management tool.

This article will resume how to easily get a Git repository up and running.

Git was developed by Linus Torvalds for use developping the Linux Kernel and other open source projects. According to a talk he gave for Google, he was searching for a Source Control Management (SCM) and used three criteria, i.e. the SCM must:

  1. Be distributed;
  2. Be fast;
  3. Output exactly what was put in, or an error should be printed.

Since there were no satisfactory options Linus wrote git.

Installation

USE flags

USE flags for dev-vcs/git stupid content tracker: distributed VCS designed for speed and efficiency

blksha1 Use the new optimized SHA1 implementation
cgi Install gitweb too
curl Support fetching and pushing (requires webdav too) over http:// and https:// protocols
cvs Enable CVS (Concurrent Versions System) integration
doc Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally
emacs Add support for GNU Emacs
gnome-keyring Enable support for storing passwords via gnome-keyring
gpg Pull in gnupg for signing -- without gnupg, attempts at signing will fail at runtime!
highlight GitWeb support for app-text/highlight
iconv Enable support for the iconv character set conversion library
mediawiki Support pulling and pushing from MediaWiki
mediawiki-experimental Add experimental patches for improved MediaWiki support
nls Add Native Language Support (using gettextGNU locale utilities)
pcre Add support for Perl Compatible Regular Expressions
perforce Add support for Perforce version control system (requires manual installation of Perforce client)
perl Add optional support/bindings for the Perl language
ppcsha1 Make use of a bundled routine that is optimized for the PPC arch
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
subversion Include git-svn for dev-vcs/subversion support
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)
threads Add threads support for various packages. Usually pthreads
tk Include the 'gitk' and 'git gui' tools
webdav Adds support for push'ing to HTTP/HTTPS repositories via DAV
xinetd Add support for the xinetd super-server

Emerge

Install dev-vcs/git:

root #emerge --ask dev-vcs/git

Additional software

There are a number of additional applications that are associated with git and are of note:

Package Description
app-vim/fugitive Git wrapper plugin for vim.
dev-vcs/git-cola "Sleek and powerful" graphical user interface for Git.
dev-vcs/git-flow Git extensions to provide high-level repository operations.
dev-vcs/gitg Git repository viewer for GNOME.
dev-vcs/qgit Qt GUI for git repositories.

This is a partial selection of packages available in the Gentoo repository, see the dev-vcs category on the Gentoo Packages site, or use eix (eix --category dev-vcs), to see packages from the dev-vcs category that may be of interest.

There is additional software in the GURU repository, such as the gitui and lazygit guis.

Configuration

Before contributing to a project, it is imperative to establish a user name and email for each user. Substitute the bracketed "Larry" references (brackets and everything in-between, but leave the quotes) in the next example for a personal user name and e-mail address:

user $git config --global user.email "<larry@gentoo.org>"
user $git config --global user.name "Larry the cow"

Local

If there will be just one user of the project, or when creating something which will be shared in a distributed way, start on the local workstation.

If the intent is to to have a central server which everyone uses as the "official" server (e.g. GitHub) then it might be easier to create an empty repository there.

The next list of commands will describe how to create a repository on a workstation:

user $cd ~/src
user $mkdir hello
user $cd hello
user $touch readme.txt
user $git init

The local repository has now been created.

Note
The actual repository resides the .git folder, so don't delete it or the parent hello folder, which would mean losing everything.

Let's make some edits:

user $echo "Hello, world!" >> readme.txt

The new readme.txt file must be added (staged) before it can be included in the git repository. Use the next commands to stage the file and to make the commit:

user $git add readme.txt
user $git commit -m "Added text to readme.txt"

Server

This section will cover setting up a Git server for remote project management through SSH.

Note
The Git server is only necessary if the intent is to have an unauthenticated read-only server for people to get code from. See here: https://git-scm.com/book/en/Git-on-the-Server-Git-Daemon. If not sure then skip this section.

Initial setup

Start by creating the required group, user, and home directory. The user uses the git-shell to prevent normal shell access.

root #groupadd git
root #useradd -m -g git -d /var/git -s /usr/bin/git-shell git


Edit /etc/conf.d/git-daemon to change user from "nobody" to "git" and start the daemon:

FILE /etc/conf.d/git-daemon
GIT_USER="git"
GIT_GROUP="git"

If desired to accept git push and allow access all direct, it needs two options --enable=receive-pack and --export-all in GITDAEMON_OPTS , e.g.:

FILE /etc/conf.d/git-daemon
GITDAEMON_OPTS="--syslog --export-all --enable=receive-pack --base-path=/var/git"

Start the daemon:

root #/etc/init.d/git-daemon start

SSH keys

SSH is the preferred method to handle the secure communications between client and server.

Important
For Git to work properly, the private/public key logins must be enabled and all client public keys added to /var/git/.ssh/authorized_keys.
Tip
For more information and instructions on how to enable, create, and share keys, please see the SSH - Passwordless Authentication wiki page.

Usage

Creating a patch

See Patches#Using_git.

Create a repository and make the initial commit

On the server:

Become the git user to make sure all objects are owned by this user:

root #su git
Note
If the following message appears:
root #su git
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Then temporarily set the login shell to /bin/bash (usermod -s /bin/bash git) and set it back after creating the bare repository (usermod -s /usr/bin/git-shell git). This may be also necessary when setting ssh access (see also this post).

Create a bare repository:

git $cd /var/git
git $mkdir /var/git/newproject.git
git $cd /var/git/newproject.git
git $git init --bare

On a client station:

git $mkdir ~/newproject
git $cd ~/newproject
git $git init
git $touch test
git $git add test
git $git config --global user.email "larry@gentoo.org"
git $git config --global user.name "larry_the_cow"
git $git commit -m 'initial commit'
git $git remote add origin git@example.com:/var/git/newproject.git
git $git push origin master

Writing to config this way will create ~/.gitconfig, but it can be moved to ~/.config/git/config, to house the git config under git.

Note
If the SSH server is not running with the default port, adding the remote should look like this, with the appropriate port (in this case 59876) following the host name:
user $git remote add origin ssh://git@example.com:59876/var/git/newproject.git

Common commands

Clone a repository:

user $git clone git@example.com:/newproject.git
user $git clone git://example.com:/newproject.git
Note
The following syntaxes apply in the example above:
user $ssh://[user@]host.xz[:port]/path/to/repo.git/
user $git://host.xz[:port]/path/to/repo.git/
For more, see man git-pull(1)

Repository management via GUI

Git ships with a tk GUI. Invoke it using:

user ~/repository.git $gitk
Note
Make sure to install git with tk USE flag. Also make sure to be within a git repository.

Serving and managing repositories via builtin web interface

Git comes with a built-in web interface called gitweb. It can run on a variety of web servers:

  • lighttpd - No configuration necessary.
  • Apache - Some configuration necessary.
  • nginx - A small, robust, and high-performance HTTP server and reverse proxy.

In order to use gitweb, be sure one of the three web servers has been installed and git has been built with the cgi USE flag.

There is a simple setup script that will create a working default configuration, start a web server (the default configuration is for lighttpd) and open the URL in a browser. Navigate to the repositories directory. Once inside, type:

user ~/repository.git $git instaweb

If git instaweb opens a 404 error, enable the cgi USE flag and rebuild git.

Help

Find out more about the options using the the built-in help output:

user ~/repository.git $git help instaweb

For additional help, consider reading the contextual man page:

user ~/repository.git $man git instaweb

Configuration

Per-project configuration can be set in the repositories .git/config file:

user ~/repository.git $vim .git/config

Values in this file should be in an ini-style format:

FILE .git/configSetting lighttpd values for instaweb in a repository's configuration
[instaweb]
        ; local = true
        httpd = lighttpd
        port = 8080
        browser = elinks
        modulePath = /usr/lib64/lighttpd/

Adjust the values as needed. If the local = true line is uncommented (remove the ;), instaweb will only be reachable from the localhost.

See also

External resources