git

From Gentoo Wiki
Jump to:navigation Jump to:search

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

Git was created by Linus Torvalds for use developing 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) to meet three criteria:

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

This article will cover getting started with Git, and general usage.

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

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
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
keyring Enable support for freedesktop.org Secret Service API password store
mediawiki Support pulling and pushing from MediaWiki
nls Add Native Language Support (using gettext - GNU 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
safe-directory Respect the safe.directory setting
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)
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 Notes
app-vim/fugitive Git wrapper plugin for vim.
dev-vcs/git-cola "Sleek and powerful" graphical user interface for Git. As of v4.0.1, git-cola cannot open a fully history clone of gentoo.git without serious application freezes. Try a shallow clone if experiencing freezing.
dev-vcs/git-flow Git extensions to provide high-level repository operations.
dev-vcs/gitg Git repository viewer for GNOME.
dev-vcs/qgit Qt-based GUI for git repositories. As of v2.10, qgit has no issues opening a full history clone of gentoo.git.

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"

Bash completion

To setup bash completion (see here for more info):

FILE ~/.config/bashrc
.  /usr/share/bash-completion/completions/git

Status in bash prompt

It is possible to configure the Bash prompt to show information such as the name of the current branch, flag of uncommited changes, number of commits that were not pushed, etc., all without additional software[1]:

FILE ~/.config/bashrc
source /usr/share/git/git-prompt.sh
export PS1='\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[01;33m\]$(__git_ps1)\[\033[01;34m\] \$\[\033[00m\] '
export GIT_PS1_SHOWDIRTYSTATE=1

To see numbers like [master ↑·2|●1✚ 1], and with time: use https://github.com/magicmonty/bash-git-prompt.

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.

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 Creating a patch.

Bisecting with live ebuilds

See Bisecting with live ebuilds.

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