Difference between revisions of "Git"

From ArchWiki
Jump to: navigation, search
m (Configuration)
Line 33: Line 33:
The [http://schacon.github.com/git/git-config.html man page for the git-config] utility has a fairly long list of variables which can be set.
The [http://schacon.github.com/git/git-config.html man page for the git-config] utility has a fairly long list of variables which can be set.
Here are some more basic configurations you will probably want:
The two settings you should set before using Git are your name and email. These are used to sign commits you make.
  $ git config --global user.name "Firstname Lastname"
  $ git config --global user.name "Firstname Lastname"
  $ git config --global user.email "your_email@youremail.com"
  $ git config --global user.email "your_email@youremail.com"
{{ic|color.ui}} is also a very useful option to set - it colorizes all Git output.
  $ git config --global color.ui true
  $ git config --global color.ui true

Revision as of 17:16, 30 November 2012

Summary help replacing me
Installing and using the Git VCS
Super Quick Git Guide: Generally about contributing to pacman, although it still serves as a practical Git tutorial
Concurrent Versions System

Git is the version control system (VCS) coded by Linus Torvalds (the creator of the Linux kernel) after being criticized for using the proprietary BitKeeper with the Linux kernel. Git is now used to maintain sources for the Linux kernel as well as thousands of other projects, including Pacman, Arch's package manager.

There is extensive documentation, including guides and tutorials, available from the official web site.


git can be installed with pacman from the official repositories. If you care about using Git with other VCS software, mail servers, or using Git's GUI, pay close attention to the optional dependencies.

Bash completion (e.g. hitting Template:Keypress to complete commands you are typing) should work if you add this line to your ~/.bashrc file:

source /usr/share/git/completion/git-completion.bash

Alternatively, you can install the bash-completion package to load the completions automatically for new shells.

If you want to use Git's built-in GUI (eg. gitk or git gui) you should install the tk package, or you will get a rather cryptic message:

/usr/bin/gitk: line 3: exec: wish: not found.


Git reads its configuration from a few INI type configuration files. In each git repository .git/config is used for configuration options specific to that repository. Per-user ("global") configuration in $HOME/.gitconfig is used as a fall-back from the repository configuration. You can edit the files directly but the preferred method is to use the git-config utility. For example,

$ git config --global core.editor "nano -w"

adds editor = nano -w to the [core] section of your ~/.gitconfig file.

The man page for the git-config utility has a fairly long list of variables which can be set.

The two settings you should set before using Git are your name and email. These are used to sign commits you make.

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@youremail.com"

color.ui is also a very useful option to set - it colorizes all Git output.

$ git config --global color.ui true


Parts from everywhere, much from the wonderful tutorial here: http://schacon.github.com/git/gittutorial.html

Create a repository from a remote one (in this exapmle, the Arch network scripts):

git clone git://projects.archlinux.org/netcfg.git

Update an existing clone

git pull origin
Note: git pull performs two operations: git fetch, which updates Git's local copies of remote branches, and git merge, which merges the remote branch into your local one.

Add changes to a commit

git add <files>
Note: You can also selectively add parts of files using git add -p

Remove files in the next commit

git rm <files>

Commit changes added with git add and git rm

git commit

Create a new branch

git branch <branch name>

Change to a different branch

git checkout <branch name>

Create the branch and check it out in one step:

git checkout -b <branch name>

List branches and show which one is currently checked out:

git branch

Merge a branch to the currently checked-out branch

git merge <branch name>

Delete a branch

git branch -d <branch name>
Note: git branch -d will fail if the branch has not been merged in order to keep you from accidentally deleting work. git branch -D will delete a branch regardless of its merge status.

Push a local branch or tag to a remote repository

git push <remote repository name> <branch or tag>
Note: -u can be added to make git push push this branch to that remote by default in the future.

Delete a branch or tag in a remote repository

git push <remote repository name> :<branch>

Diff between two branches

git diff <branch 1> <branch 2>

Diff between two commits IDs (found in git log)

git diff <commit 1> <commit 2>

Set nano as default editor

git config --global core.editor "nano -w"

Git Prompt

The Git package comes with a prompt script. To enable the prompt addition you will need to source the git-prompt.sh script and add $(__git_ps1 " (%s)") to you PS1 variable.

  • Copy /usr/share/git/git-prompt.sh to your home directory (e.g. ~/.git-prompt.sh).
  • Add the following line to your .bashrc/.zshrc:
source ~/.git-prompt.sh
  • For Bash:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
Note: For information about coloring your bash prompt see Color_Bash_Prompt
  • For zsh:
PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

The %s is replaced by the current branch name. The git information is displayed only if you are navigating in a git repository. You can enable extra information by setting and exporting certain variables to a non-empty value as shown in the following table:

Variable Information
GIT_PS1_SHOWDIRTYSTATE * for unstaged and + for staged changes
GIT_PS1_SHOWSTASHSTATE $ if something is stashed
GIT_PS1_SHOWUNTRACKEDFILES % if there are untracked files

In addition you can set the GIT_PS1_SHOWUPSTREAM variable to "auto" in order to see < if you are behind upstream, > if you are ahead and <> if you have diverged.

Note: If you experience that $(__git_ps1) return ((unknown)) then there's a .git folder in your current directory which doesn't contain any repository, and therefore Git does not recognize it. This can for example happen if you for some reason mistake Git's config-file to be ~/.git/config instead of ~/.gitconfig.

Transfer Protocols

Smart HTTP

Since version 1.6.6 git is able to use the HTTP(S) protocol as efficiently as SSH or GIT by utilizing the git-http-backend. Furthermore it is not only possible to clone or pull from repositories, but also to push into repositories over HTTP(S).

The setup for this is rather simple as all you need to have installed is the Apache web server (with mod_cgi, mod_alias, and mod_env enabled) and of course, git:

# pacman -S apache git

Once you have your basic setup up and running, add the following to your Apache's config usually located at /etc/httpd/conf/httpd.conf:

<Directory "/usr/lib/git-core*">
    Order allow,deny
    Allow from all

SetEnv GIT_PROJECT_ROOT /srv/git
ScriptAlias /git/ /usr/lib/git-core/git-http-backend/

The above example config assumes that your git repositories are located at /srv/git and that you want to access them via something like http(s)://your_address.tld/git/your_repo.git. Feel free to customize this to your needs.

Note: Of course you have to make sure that your Apache can read and write (if you want to enable push access) on your git repositories.

For more detailed documentation, visit the following links:


You first need to have a public SSH key. For that follow the guide at Using SSH Keys. To set up SSH itself, you need to follow the SSH guide. This assumes you have a public SSH key now and that your SSH is working. Open your SSH key in your favorite editor (default public key name is ~/.ssh/id_rsa.pub), and copy its content (Template:Keypress). Now go to your user where you have made your Git repository, since we now need to allow that SSH key to log in on that user to access the Git repository. Open ~/.ssh/authorized_keys in your favorite editor, and paste the contents of id_rsa.pub in it. Be sure it is all on one line! That is important! It should look somewhat like this:

Warning: Do not copy the line below! It is an example! It will not work if you use that line!
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCboOH6AotCh4OcwJgsB4AtXzDo9Gzhl+BAHuEvnDRHNSYIURqGN4CrP+b5Bx/iLrRFOBv58TcZz1jyJ2PaGwT74kvVOe9JCCdgw4nSMBV44cy+6cTJiv6f1tw8pHRS2H6nHC9SCSAWkMX4rpiSQ0wkhjug+GtBWOXDaotIzrFwLw== username@hostname

Now you can checkout your Git repository this way (change where needed. Here it is using the git username and localhost):

git clone git@localhost:my_repository.git

You should now get an SSH yes/no question. Type yes followed by Template:Keypress. Then you should have your repository checked out. Because this is with SSH, you also do have commit rights now. For that look at Git and Super Quick Git Guide.

Specifying a non-standard port

Connecting on a port other than 22 can be configured on a per-host basis in /etc/ssh/ssh_config or ~/.ssh/config. To set up ports for a repository, specify the path in .git/config using the port number N and the absolute path /PATH/TO/REPO:


Typically the repository resides in the home directory of the user which allows you to use tilde-expansion. Thus to connect on port N=443,

url = git@example.org:repo.git


url = ssh://git@example.org:443/~git/repo.git

Git Daemon

Note: The git daemon only allows read access. For write access see #Git SSH.

This will allow URLs like "git clone git://localhost/my_repository.git".

Edit configuration file for git-daemon /etc/conf.d/git-daemon.conf (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:

# rc.d start git-daemon

To run the git-daemon every time at boot, just append git-daemon to DAEMONS line in rc.conf.

Clients can now simply use:

git clone git://localhost/my_repository.git

GIT repositories rights

To restrict read/write access, you can simply use Unix rights, see http://sitaramc.github.com/gitolite/doc/overkill.html

For a fine-grained rights access, see gitolite and gitosis

See also