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:
- be distributed
- be fast
- output exactly what was put in, or an error should be printed.
This article will cover getting started with Git, and general usage.
The git repositories for Gentoo developers can be found here: https://gitweb.gentoo.org/
USE flags for dev-vcs/git Stupid content tracker: distributed VCS designed for speed and efficiency
||Use the new optimized SHA1 implementation|
||Install gitweb too|
||Support fetching and pushing (requires webdav too) over http:// and https:// protocols|
||Enable CVS (Concurrent Versions System) integration|
||Add extra documentation (API, Javadoc, etc). It is recommended to enable per package instead of globally|
||Enable support for storing passwords via gnome-keyring|
||Pull in gnupg for signing -- without gnupg, attempts at signing will fail at runtime!|
||GitWeb support for app-text/highlight|
||Enable support for the iconv character set conversion library|
||Support pulling and pushing from MediaWiki|
||Add experimental patches for improved MediaWiki support|
||Add Native Language Support (using gettextGNU locale utilities)|
||Add support for Perl Compatible Regular Expressions|
||Add support for Perforce version control system (requires manual installation of Perforce client)|
||Add optional support/bindings for the Perl language|
||Respect the safe.directory setting|
||!!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur|
||Include git-svn for dev-vcs/subversion support|
||Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)|
||Include the 'gitk' and 'git gui' tools|
||Adds support for push'ing to HTTP/HTTPS repositories via DAV|
||Add support for the xinetd super-server|
emerge --ask dev-vcs/git
There are a number of additional applications that are associated with Git and are of note:
|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.
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:
git config --global user.email "<email@example.com>"
git config --global user.name "<Larry the cow>"
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:
The local repository has now been created.
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:
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:
git add readme.txt
git commit -m "Added text to readme.txt"
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:
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.
This section will cover setting up a Git server for remote project management through SSH.
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.
Start by creating the required group, user, and home directory. The user uses the
git-shell to prevent normal shell access.
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:
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.:
GITDAEMON_OPTS="--syslog --export-all --enable=receive-pack --base-path=/var/git"
Start the daemon:
SSH is the preferred method to handle the secure communications between client and server.
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.
For more information and instructions on how to enable, create, and share keys, please see the SSH - Passwordless Authentication wiki page.
Creating a patch
See Creating a patch.
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:
If the following message appears:
fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access.
Create a bare repository:
git init --bare
On a client station:
git add test
git config --global user.email "firstname.lastname@example.org"
git config --global user.name "larry_the_cow"
git commit -m 'initial commit'
git remote add origin email@example.com:/var/git/newproject.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.
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:
git remote add origin ssh://firstname.lastname@example.org:59876/var/git/newproject.git
Clone a repository:
git clone email@example.com:/newproject.git
git clone git://example.com:/newproject.git
The following syntaxes apply in the example above:
Repository management via GUI
Git ships with a tk GUI. Invoke it using:
user ~/repository.git $
Make sure to install git with
tkUSE 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 $
If git instaweb opens a 404 error, enable the
cgi USE flag and rebuild git.
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
Per-project configuration can be set in the repositories .git/config file:
user ~/repository.git $
Values in this file should be in an ini-style format:
[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.
- Cgit — a fast web-interface (CGI) for git written in the C programming language.
- Git/Tweaks — aims to document some neat bonus features and config options available in git.
- Kernel git-bisect — a Git tool to find the commit that caused problems between versions.
- https://www.codeschool.com/courses/git-real - Free, good, basic first-time tutorial
- git flow documentation - Client side scripts to make git repository management a snap.
- The Official Git Handbook - Hosted at the official git website.
- Git - The simple guide
- Git from the inside out - A well written publication from the Recurse Center. This article addresses what happens beneath the surface when using git.
- Lesser known git commands - A blog entry on helpful commands that go unnoticed.