Git
Git is Article description::distributed revision control and source code management software. The goal of this article is to easily get a Git repository up and running.
Git was developed by Linus Torvalds for use on 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.
SCM must:
- Be distributed;
- Be fast;
- 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 |
libressl
|
Use dev-libs/libressl instead of dev-libs/openssl when applicable (see also the ssl useflag) |
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 |
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
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 you're the only one using your project, or if you're creating something which will be shared in a distributed way, then you should start on your workstation. If you intend 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
You're done. You've created a local repository. It's in the .git folder, so don't delete your hello folder unless you mean to lose everything.
Your repository is the .git folder inside the main hello folder. If you delete ~/src/hello, then your repo is gone.
Now, let's say we 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
In this section will cover setting up a Git server for remote project management through SSH.
The Git server is only necessary if you intend 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 needed 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:
/etc/conf.d/git-daemon
<syntaxhighlight lang="bash">GIT_USER="git" GIT_GROUP="git"</syntaxhighlight>
If wants to accept git push and allow access all direct, it needs to add two options --enable=receive-pack and --export-all to GITDAEMON_OPTS , ex:
/etc/conf.d/git-daemon
<syntaxhighlight lang="bash">GITDAEMON_OPTS="--syslog --export-all --enable=receive-pack --base-path=/var/git"</syntaxhighlight>
root #
/etc/init.d/git-daemon start
SSH keys
SSH is the preferred method to handle the secure communications between client and server. For Git to work properly, you must have private/public key logins 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.
this step must be completed before continuing
Usage
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
If the following message appears,
fatal: Interactive git shell is not enabled. hint: ~/git-shell-commands should exist and have read and execute access.
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:
user $
cd /var/git
user $
mkdir /var/git/newproject.git
user $
cd /var/git/newproject.git
user $
git init --bare
On a the client station:
user $
mkdir ~/newproject
user $
cd ~/newproject
user $
git init
user $
touch test
user $
git add test
user $
git config --global user.email "larry@gentoo.org"
user $
git config --global user.name "larry_the_cow"
user $
git commit -m 'initial commit'
user $
git remote add origin git@example.com:/var/git/newproject.git
user $
git push origin master
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
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/
Repository management via GUI
Git ships with a tk GUI. Invoke it using:
user ~/repository.git $
gitk
Make sure to compile 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 specific in an ini-style format:
.git/config
Setting lighttpd values for instaweb in a repository's configuration<syntaxhighlight lang="ini">[instaweb] ; local = true httpd = lighttpd port = 8080 browser = elinks modulePath = /usr/lib64/lighttpd/</syntaxhighlight>
Adjust the values as needed. If the local = true
light is uncommented (remove the ;
) instaweb will only be connectable from the localhost.
See also
- Cgit - A fast web-interface (CGI) for git.
- Gitolite
- Git/tracking-etc
- CVS - Gentoo's old VCS for projects and the Portage tree.
- Kernel git-bisect
External resources
- 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.