- 1.1 O nadzoru različic
- 1.2 Kratka zgodovina Gita
- 1.3 Kaj je Git?
- 1.4 Ukazna vrstica
- 1.5 Git namestitev
- 1.6 Prva namestitev Gita
- 1.7 Pridobivanje pomoči
- 1.8 Povzetek
2. Osnove Git
- 2.1 Pridobivanje repozitorija Git
- 2.2 Snemanje sprememb v repozitorij
- 2.3 Pregled zgodovine potrditev
- 2.4 Razveljavljanje stvari
- 2.5 Delo z daljavami
- 2.6 Označevanje
- 2.7 Git aliasi
- 2.8 Povzetek
3. Veje Git
- 3.1 Veje na kratko
- 3.2 Osnove vej in združevanja
- 3.3 Upravljanje vej
- 3.4 Potek dela z vejami
- 3.5 Oddaljene veje
- 3.6 Ponovno baziranje (rebasing)
- 3.7 Povzetek
4. Git na strežniku
- 4.1 Protokoli
- 4.2 Pridobiti Git na strežnik
- 4.3 Generiranje vaših javnih ključev SSH
- 4.4 Nastavitev strežnika
- 4.5 Prikriti proces Git
- 4.6 Pametni HTTP
- 4.7 GitWeb
- 4.8 GitLab
- 4.9 Tretje osebne opcije gostovanja
- 4.10 Povzetek
5. Distribuirani Git
- 5.1 Razdeljeni poteki dela
- 5.2 Prispevanje projektu
- 5.3 Vzdrževanje projekta
- 5.4 Povzetek
7. Orodja Git
- 7.1 Revision Selection
- 7.2 Interactive Staging
- 7.3 Stashing and Cleaning
- 7.4 Signing Your Work
- 7.5 Searching
- 7.6 Rewriting History
- 7.7 Reset Demystified
- 7.8 Advanced Merging
- 7.9 Rerere
- 7.10 Debugging with Git
- 7.11 Submodules
- 7.12 Bundling
- 7.13 Replace
- 7.14 Credential Storage
- 7.15 Povzetek
8. Prilagoditev Gita
- 8.1 Git Configuration
- 8.2 Git Attributes
- 8.3 Kljuke Git
- 8.4 An Example Git-Enforced Policy
- 8.5 Povzetek
9. Git in ostali sistemi
- 9.1 Git kot klient
- 9.2 Migracija na Git
- 9.3 Povzetek
10. Notranjost Gita
- 10.1 Napeljava in keramika
- 10.2 Git Objects
- 10.3 Git References
- 10.4 Packfiles
- 10.5 Refspec
- 10.6 Transfer Protocols
- 10.7 Maintenance and Data Recovery
- 10.8 Environment Variables
- 10.9 Povzetek
A1. Dodatek A: Git v drugih okoljih
- A1.1 Grafični vmesniki
- A1.2 Git v programu Visual Studio
- A1.3 Git v Visual Studio Code
- A1.4 Git v IntelliJ / PyCharm / WebStorm / PhpStorm / RubyMine
- A1.5 Git v Sublime Text
- A1.6 Git v Bashu
- A1.7 Git v Zsh
- A1.8 Git v Powershellu
- A1.9 Povzetek
A2. Dodatek B: Vdelava Gita v vašo aplikacijo
- A2.1 Git v ukazni vrstici
- A2.2 Libgit2
- A2.3 JGit
- A2.4 go-git
- A2.5 Dulwich
A3. Dodatek C: Ukazi Git
- A3.1 Nastavitev in konfiguracija
- A3.2 Pridobivanje in ustvarjanje projektov
- A3.3 Osnove posnetkov
- A3.4 Veje in združevanje
- A3.5 Deljenje in posodabljanje projektov
- A3.6 Pregled in primerjava
- A3.7 Razhroščevanje
- A3.8 Popravljanje
- A3.9 E-pošta
- A3.10 Zunanji sistemi
- A3.11 Administracija
- A3.12 Orodja za vododovodne sisteme
8.3 Prilagoditev Gita - Kljuke Git
Kot mnogi drugi sistemi nadzora različic ima Git način zagona skript po meri, ko se zgodijo določene pomembne akcije. Na voljo sta dve skupini teh kljuk: na strani odjemalca in na strani strežnika. Kljuke na strani odjemalca se sprožijo z operacijami, kot so pošiljanje in združevanje, medtem ko so kljuke strežniške strani gnane na operacijah omrežja, kot je prejemanje potisnjenih potrditev. Te kljuke lahko uporabite iz najrazličnejših razlogov.
Installing a Hook
The hooks are all stored in the
hooks subdirectory of the Git directory.
In most projects, that’s
When you initialize a new repository with
git init, Git populates the hooks directory with a bunch of example scripts, many of which are useful by themselves; but they also document the input values of each script.
All the examples are written as shell scripts, with some Perl thrown in, but any properly named executable scripts will work fine – you can write them in Ruby or Python or whatever language you are familiar with.
If you want to use the bundled hook scripts, you’ll have to rename them; their file names all end with
To enable a hook script, put a file in the
hooks subdirectory of your .git directory that is named appropriately (without any extension) and is executable.
From that point forward, it should be called.
We’ll cover most of the major hook filenames here.
There are a lot of client-side hooks. This section splits them into committing-workflow hooks, email-workflow scripts, and everything else.
It’s important to note that client-side hooks are not copied when you clone a repository. If your intent with these scripts is to enforce a policy, you’ll probably want to do that on the server side; see the example in An Example Git-Enforced Policy.
The first four hooks have to do with the committing process.
pre-commit hook is run first, before you even type in a commit message.
It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.
Exiting non-zero from this hook aborts the commit, although you can bypass it with
git commit --no-verify.
You can do things like check for code style (run
lint or something equivalent), check for trailing whitespace (the default hook does exactly this), or check for appropriate documentation on new methods.
prepare-commit-msg hook is run before the commit message editor is fired up but after the default message is created.
It lets you edit the default message before the commit author sees it.
This hook takes a few parameters: the path to the file that holds the commit message so far, the type of commit, and the commit SHA-1 if this is an amended commit.
This hook generally isn’t useful for normal commits; rather, it’s good for commits where the default message is auto-generated, such as templated commit messages, merge commits, squashed commits, and amended commits.
You may use it in conjunction with a commit template to programmatically insert information.
commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer.
If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.
In the last section of this chapter, we’ll demonstrate using this hook to check that your commit message is conformant to a required pattern.
After the entire commit process is completed, the
post-commit hook runs.
It doesn’t take any parameters, but you can easily get the last commit by running
git log -1 HEAD.
Generally, this script is used for notification or something similar.
Email Workflow Hooks
You can set up three client-side hooks for an email-based workflow.
They’re all invoked by the
git am command, so if you aren’t using that command in your workflow, you can safely skip to the next section.
If you’re taking patches over email prepared by
git format-patch, then some of these may be helpful to you.
The first hook that is run is
It takes a single argument: the name of the temporary file that contains the proposed commit message.
Git aborts the patch if this script exits non-zero.
You can use this to make sure a commit message is properly formatted, or to normalize the message by having the script edit it in place.
The next hook to run when applying patches via
git am is
Somewhat confusingly, it is run after the patch is applied but before a commit is made, so you can use it to inspect the snapshot before making the commit.
You can run tests or otherwise inspect the working tree with this script.
If something is missing or the tests don’t pass, exiting non-zero aborts the
git am script without committing the patch.
The last hook to run during a
git am operation is
post-applypatch, which runs after the commit is made.
You can use it to notify a group or the author of the patch you pulled in that you’ve done so.
You can’t stop the patching process with this script.
Other Client Hooks
pre-rebase hook runs before you rebase anything and can halt the process by exiting non-zero.
You can use this hook to disallow rebasing any commits that have already been pushed.
pre-rebase hook that Git installs does this, although it makes some assumptions that may not match with your workflow.
post-rewrite hook is run by commands that replace commits, such as
git commit --amend and
git rebase (though not by
Its single argument is which command triggered the rewrite, and it receives a list of rewrites on
This hook has many of the same uses as the
After you run a successful
git checkout, the
post-checkout hook runs; you can use it to set up your working directory properly for your project environment.
This may mean moving in large binary files that you don’t want source controlled, auto-generating documentation, or something along those lines.
post-merge hook runs after a successful
You can use it to restore data in the working tree that Git can’t track, such as permissions data.
This hook can likewise validate the presence of files external to Git control that you may want copied in when the working tree changes.
pre-push hook runs during
git push, after the remote refs have been updated but before any objects have been transferred.
It receives the name and location of the remote as parameters, and a list of to-be-updated refs through
You can use it to validate a set of ref updates before a push occurs (a non-zero exit code will abort the push).
Git occasionally does garbage collection as part of its normal operation, by invoking
git gc --auto.
pre-auto-gc hook is invoked just before the garbage collection takes place, and can be used to notify you that this is happening, or to abort the collection if now isn’t a good time.
In addition to the client-side hooks, you can use a couple of important server-side hooks as a system administrator to enforce nearly any kind of policy for your project. These scripts run before and after pushes to the server. The pre hooks can exit non-zero at any time to reject the push as well as print an error message back to the client; you can set up a push policy that’s as complex as you wish.
The first script to run when handling a push from a client is
It takes a list of references that are being pushed from stdin; if it exits non-zero, none of them are accepted.
You can use this hook to do things like make sure none of the updated references are non-fast-forwards, or to do access control for all the refs and files they’re modifying with the push.
update script is very similar to the
pre-receive script, except that it’s run once for each branch the pusher is trying to update.
If the pusher is trying to push to multiple branches,
pre-receive runs only once, whereas update runs once per branch they’re pushing to.
Instead of reading from stdin, this script takes three arguments: the name of the reference (branch), the SHA-1 that reference pointed to before the push, and the SHA-1 the user is trying to push.
If the update script exits non-zero, only that reference is rejected; other references can still be updated.
post-receive hook runs after the entire process is completed and can be used to update other services or notify users.
It takes the same stdin data as the
Examples include emailing a list, notifying a continuous integration server, or updating a ticket-tracking system – you can even parse the commit messages to see if any tickets need to be opened, modified, or closed.
This script can’t stop the push process, but the client doesn’t disconnect until it has completed, so be careful if you try to do anything that may take a long time.
If you’re writing a script/hook that others will need to read, prefer the long versions of command-line flags; six months from now you’ll thank us.