Difference between revisions of "Git"

From ArchWiki
Jump to: navigation, search
(Git SSH)
(Replaced poorly written note about bash color prompt with a simpler version. This article shouldn't be about bash color prompts, and therefore the example was removed.)
(25 intermediate revisions by 15 users not shown)
Line 1: Line 1:
[[Category: Development (English)]]
+
[[Category:Version Control System]]
{{i18n|Git}}
+
[[zh-CN:Git]]
 
+
 
{{Article summary start}}
 
{{Article summary start}}
 
{{Article summary text|Installing and using the Git VCS}}
 
{{Article summary text|Installing and using the Git VCS}}
Line 10: Line 9:
 
{{Article summary wiki|Subversion}}
 
{{Article summary wiki|Subversion}}
 
{{Article summary wiki|Concurrent Versions System}}
 
{{Article summary wiki|Concurrent Versions System}}
{{Article summary link|github|http://github.com/}}
+
{{Article summary link|GitHub|https://github.com/}}
 
{{Article summary end}}
 
{{Article summary end}}
  
Line 18: Line 17:
  
 
==Installation==
 
==Installation==
{{Package Official|git}} can be installed with [[pacman]] from the [extra] repository. If you care about using Git with other VCS software, mail servers, or using Git's GUI pay close attention to the optional dependencies.
+
{{Pkg|git}} can be installed with [[pacman]] from the [[Official Repositories|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 tab to complete commands you are typing) should work if you:
+
Bash completion (e.g. hitting {{keypress|Tab}} to complete commands you are typing) should work if you add this line to your {{ic|~/.bashrc}} file:
source /usr/share/git/completion/git-completion.bash
+
{{bc|source /usr/share/git/completion/git-completion.bash}}
Alternatively, you can install the {{Package Official|bash-completion}} package to load the completions automatically for new shells.
+
Alternatively, you can install the {{Pkg|bash-completion}} package to load the completions automatically for new shells.
  
If you want to use Git's built-in GUI (eg. {{Ic|gitk}} or {{Ic|git gui}}) you should install the {{Package Official|tk}} package or you will get a rather cryptic message:
+
If you want to use Git's built-in GUI (eg. {{Ic|gitk}} or {{Ic|git gui}}) you should install the {{Pkg|tk}} package, or you will get a rather cryptic message:
/usr/bin/gitk: line 3: exec: wish: not found.
+
{{bc|/usr/bin/gitk: line 3: exec: wish: not found.}}
  
 
== Configuration ==
 
== Configuration ==
Git reads its configuration from a few INI type configuration files.  In each git repository {{filename|.git/config}} is used for configuration options specific to that repository.  Per-user ("global") configuration in {{filename|$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 reads its configuration from a few INI type configuration files.  In each git repository {{ic|.git/config}} is used for configuration options specific to that repository.  Per-user ("global") configuration in {{ic|$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"
 
  $ git config --global core.editor "nano -w"
adds {{Ic|<nowiki>editor = nano -w</nowiki>}} to the {{Ic|<nowiki>[core]</nowiki>}} section of your {{filename|~/.gitconfig}} file.
+
adds {{Ic|<nowiki>editor = nano -w</nowiki>}} to the {{Ic|<nowiki>[core]</nowiki>}} section of your {{ic|~/.gitconfig}} file.
  
The [http://www.kernel.org/pub/software/scm/git/docs/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:
 
Here are some more basic configurations you will probably want:
Line 38: Line 37:
 
  $ git config --global user.email "your_email@youremail.com"
 
  $ git config --global user.email "your_email@youremail.com"
 
  $ git config --global color.ui true
 
  $ git config --global color.ui true
$ git config --global --list
 
  
 
==Cheatsheet==
 
==Cheatsheet==
Line 46: Line 44:
  
 
Pull the network scripts with
 
Pull the network scripts with
  git clone <nowiki>http://archlinux.org/~james/projects/network.git</nowiki>
+
  git clone <nowiki>git://projects.archlinux.org/netcfg.git</nowiki>
 
Update an existing clone
 
Update an existing clone
 
  git pull origin
 
  git pull origin
Line 77: Line 75:
  
 
==Git Prompt==
 
==Git Prompt==
Git package comes with a bash completion file. This file, also contains the necessary functions to provide git information on your bash or zsh shell prompt. To enable it add {{Ic|$(__git_ps1 " (%s)")}} to you PS1 variable.
+
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 {{Ic|$(__git_ps1 " (%s)")}} to you PS1 variable.
* For bash:
+
* Copy {{Ic|/usr/share/git/git-prompt.sh}} to your home directory (e.g. {{Ic|~/.git-prompt.sh}}).
 +
* Add the following line to your .bashrc/.zshrc:
 +
source ~/.git-prompt.sh
 +
* For Bash:
 
  PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
 
  PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
 +
{{Note|For information about coloring your bash prompt see [[Color_Bash_Prompt]]}}
 +
 
* For zsh:
 
* For zsh:
 
  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
 
  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
Line 98: Line 101:
 
In addition you can set the {{Ic|GIT_PS1_SHOWUPSTREAM}} variable to {{Ic|"auto"}} in order to see {{Ic|'''<'''}} if you are behind upstream, {{Ic|'''>'''}} if you are ahead and {{Ic|'''<>'''}} if you have diverged.
 
In addition you can set the {{Ic|GIT_PS1_SHOWUPSTREAM}} variable to {{Ic|"auto"}} in order to see {{Ic|'''<'''}} if you are behind upstream, {{Ic|'''>'''}} if you are ahead and {{Ic|'''<>'''}} if you have diverged.
  
{{Note|If you don't use bash completion consider sourcing '''''/usr/share/git/completion/git-completion.bash''''' in your '''.bashrc'''/'''.zshrc'''}}
+
{{Note|If you experience that {{Ic|$(__git_ps1)}} return {{Ic|((unknown))}} then there's a {{Ic|.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 {{Ic|~/.git/config}} instead of {{Ic|~/.gitconfig}}.}}
  
 
==Transfer Protocols==
 
==Transfer Protocols==
Line 104: Line 107:
 
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).
 
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 webserver (with mod_cgi, mod_alias, and mod_env enabled) and of course, git:
+
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
 
  # pacman -S apache git
  
Once you have your basic setup up and running, add the following to your Apache's config usually located at {{filename|/etc/httpd/conf/httpd.conf}}:
+
Once you have your basic setup up and running, add the following to your Apache's config usually located at {{ic|/etc/httpd/conf/httpd.conf}}:
 
  <Directory "/usr/lib/git-core*">
 
  <Directory "/usr/lib/git-core*">
 
     Order allow,deny
 
     Order allow,deny
Line 117: Line 120:
 
  ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
 
  ScriptAlias /git/ /usr/lib/git-core/git-http-backend/
  
The above example config assumes that your git repositories are located at {{filename|/srv/git}} and that you want to access them via something like <nowiki>http(s)://your_address.tld/git/your_repo.git</nowiki>. Feel free to customize this to your needs.
+
The above example config assumes that your git repositories are located at {{ic|/srv/git}} and that you want to access them via something like <nowiki>http(s)://your_address.tld/git/your_repo.git</nowiki>. 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.}}
 
{{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.}}
Line 123: Line 126:
 
For more detailed documentation, visit the following links:
 
For more detailed documentation, visit the following links:
 
* http://progit.org/2010/03/04/smart-http.html
 
* http://progit.org/2010/03/04/smart-http.html
* http://www.kernel.org/pub/software/scm/git/docs/git-http-backend.html
+
* https://www.kernel.org/pub/software/scm/git/docs/v1.7.10.1/git-http-backend.html
  
 
===Git SSH===
 
===Git SSH===
You first need to have a public SSH key. For that follow the guide at [[Using SSH Keys]]. To setup SSH itself you need to follow the [[SSH]] guide. I assume you have a public SSH key now and your SSH is working.
+
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 id_rsa.pub and is located in {{filename|~/.ssh}}) and copy its content (CTRL + C).
+
Open your SSH key in your favorite editor (default public key name is {{ic|~/.ssh/id_rsa.pub}}), and copy its content ({{keypress|Ctrl+c}}).
Now go to your user where you have made your git repository, since we now need to allow that SSH key to login on that user to access the GIT repository.
+
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 this file in your favorite editor (i use nano)
+
Open {{ic|~/.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:
nano ~/.ssh/authorized_keys
+
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!}}
 
{{Warning|Do not copy the line below! It is an example! It will not work if you use that line!}}
 
<pre style='overflow:auto'>
 
<pre style='overflow:auto'>
 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCboOH6AotCh4OcwJgsB4AtXzDo9Gzhl+BAHuEvnDRHNSYIURqGN4CrP+b5Bx/iLrRFOBv58TcZz1jyJ2PaGwT74kvVOe9JCCdgw4nSMBV44cy+6cTJiv6f1tw8pHRS2H6nHC9SCSAWkMX4rpiSQ0wkhjug+GtBWOXDaotIzrFwLw== username@hostname
 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCboOH6AotCh4OcwJgsB4AtXzDo9Gzhl+BAHuEvnDRHNSYIURqGN4CrP+b5Bx/iLrRFOBv58TcZz1jyJ2PaGwT74kvVOe9JCCdgw4nSMBV44cy+6cTJiv6f1tw8pHRS2H6nHC9SCSAWkMX4rpiSQ0wkhjug+GtBWOXDaotIzrFwLw== username@hostname
 
</pre>
 
</pre>
Now you can checkout your git repo this way (change where needed. Here it is using the git username and localhost):
+
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
 
  git clone git@localhost:my_repository.git
You should now get an SSH yes/no question. Type yes followed by enter. Then you should have your repository checked out. Since this is with SSH you also do have commit rights now. For that look at [[Git]] and [[Super Quick Git Guide]].
+
You should now get an SSH yes/no question. Type {{ic|yes}} followed by {{keypress|Enter}}. 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===
+
====Specifying a non-standard port====
Connecting on a port other than 22 can be configured on a per-host basis in {{ic|/etc/ssh/ssh_config}} or {{ic|~/.ssh/config}}. To set up ports for a repo, specify the path in {{ic|.git/config}} using the port number N and the '''absolute path''' /PATH/TO/REPO:
+
Connecting on a port other than 22 can be configured on a per-host basis in {{ic|/etc/ssh/ssh_config}} or {{ic|~/.ssh/config}}. To set up ports for a repository, specify the path in {{ic|.git/config}} using the port number {{ic|N}} and the ''absolute path'' {{ic|/PATH/TO/REPO}}:
 
  ssh://user@example.org:N/PATH/TO/REPO
 
  ssh://user@example.org:N/PATH/TO/REPO
Typically the repo resides in the home directory of the user which allows you to use tilde-expansion. Thus to connect on port 443,
+
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 = git@example.org:repo.git
 
becomes:
 
becomes:
 
  url = ssh://git@example.org:443/~git/repo.git
 
  url = ssh://git@example.org:443/~git/repo.git
  
===GIT Daemon===
+
===Git Daemon===
 
{{Note|The git daemon only allows read access. For write access see [[#Git SSH]].}}
 
{{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".
 
This will allow URLs like "git clone git://localhost/my_repository.git".
  
Edit configuration file for git-dameon {{filename|/etc/conf.d/git-daemon.conf}} (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:
+
Edit configuration file for git-daemon {{ic|/etc/conf.d/git-daemon.conf}} (GIT_REPO is a place with your git projects), then start git-daemon with root privileges:
  # /etc/rc.d/git-daemon start
+
  # rc.d start git-daemon
  
 
To run the git-daemon every time at boot, just append {{Ic|git-daemon}} to {{Ic|DAEMONS}} line in [[rc.conf]].
 
To run the git-daemon every time at boot, just append {{Ic|git-daemon}} to {{Ic|DAEMONS}} line in [[rc.conf]].
Line 165: Line 166:
 
For a fine-grained rights access, see [[gitolite]] and [[gitosis]]
 
For a fine-grained rights access, see [[gitolite]] and [[gitosis]]
  
==References==
+
==See also==
* http://www.kernel.org/pub/software/scm/git/docs/
+
* [http://git-scm.com/book Pro Git book]
* http://book.git-scm.com/index.html
+
* [http://gitref.org/ Git Reference]
* http://gitref.org/
+
* https://www.kernel.org/pub/software/scm/git/docs/
* http://help.github.com/
+
* https://help.github.com/

Revision as of 14:34, 15 November 2012

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki: Generally about contributing to pacman, although it still serves as a practical Git tutorial Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary link Template:Article summary end

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.

Installation

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.

Configuration

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.

Here are some more basic configurations you will probably want:

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

Cheatsheet

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

Additionally see Super Quick Git Guide.

Pull the network scripts with

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

Update an existing clone

git pull origin

Commit changes

git commit -a -m "changelog message"

To create a new branch

git branch somebranch

Change to a different branch

git checkout differentbranch

Merge a branch to current active branch

git merge somebranch

Delete a branch

git branch -d somebranch

Push a local branch or tag to a remote repository

git push REMOTENAME BRANCHNAME

Delete a branch or tag in a remote repository

git push REMOTENAME :BRANCHNAME

Diff between two branches

git diff master..somebranch

Diff between two commit IDs (found in git log)

git diff e9780c7cba2855350e914fde227a79bb63c1351d..8b014e40346b38b3b9bfc41359b4e8a68e804c0d

Diff between the last two commits

git diff HEAD^ HEAD

Patchset between two branches (follows same syntax as git diff afaik)

git format-patch master..somebranch

Or better: http://wiki.winehq.org/GitWine#head-f7a29e7ed999b5924748a60c5a1cd4a019032d26

git format-patch -o out origin

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
</Directory>

SetEnv GIT_PROJECT_ROOT /srv/git
SetEnv GIT_HTTP_EXPORT_ALL
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:

Git SSH

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:

ssh://user@example.org:N/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

becomes:

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