Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a command right after a fetch or pull command in git?

Tags:

git

githooks

ghc

I cloned the GHC (Glasgow Haskell Compiler) repository. In order to build the compiler, you need several libraries, all of them are available as git repositories too. In order to ease ones live, the GHC hackers included a script sync-all that, when executed, updates all the dependent repositories.

Now to my question: How can I make git execute ./sync-all pull after I did a git pull automatically? I heard something about using hooks, but I don't really know, what I have to do.

like image 503
fuz Avatar asked Apr 11 '11 14:04

fuz


People also ask

What is the command after git fetch?

If you approve the changes a remote branch contains, you can merge it into a local branch with a normal git merge . So, unlike SVN, synchronizing your local repository with a remote repository is actually a two-step process: fetch, then merge. The git pull command is a convenient shortcut for this process.

How do you git fetch and pull?

Git Fetch is the command that tells the local repository that there are changes available in the remote repository without bringing the changes into the local repository. Git Pull on the other hand brings the copy of the remote directory changes into the local repository.

Do things after git pull?

When you run git pull , git actually does a fetch followed by a merge. This means you can use the post-merge hook to execute a script when a pull is completed. To set this up you just need to create an executable script in your repository's . git/hooks/ directory called post-merge .

When to use fetch and pull in git?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.


2 Answers

If you need to execute a script after a git pull on the client side (your machine), then a there is no reliable solution.

The post-merge client-side hook (mentioned here) won't be triggered by every pull (for instance, not in a pull rebase, not in case of fast-forward merge)

So an alias wrapping both commands remains a possible workaround.


Original answer (2011)

If you need that on the server side, where you would checkout the bare repo and pull from a working tree (on the server) after each push to a bare repo (on the server):

A post-receive or post-update hook is generally used for this kind of task, executed after each push.
The difference between the post-receive and post-update hooks is in the arguments: a post-receive will get both the old and the new values of all the refs in addition to their names.

The missing piece is: where that hook executes itself?

The blog post "Missing git hooks documentation" from (a great) SO contributor Mark Longair sheds some light on this:

the current working directory will be the git directory.

  • So, if this is a bare repository called “/src/git/test.git/”, that will be the current working directory – if this is a non-bare repository and the top level of the working tree is “/home/mark/test/” then the current working directory will be “/home/mark/test/.git/

Chris Johnsen details in his SO answer: "If –-git-dir or GIT_DIR are specified but none of -–work-tree, GIT_WORK_TREE and core.worktree is specified, the current working directory is regarded as the top directory of your working tree."

In other words, your working tree will also be the current directory (the .git directory), which almost certainly isn’t what you want.

Make sure your hook/post-receive script, once created and made executable, sets the GIT_WORK_TREE at the right path, in order for your ./sync-all pull to be executed in the right directory (i.e. not "xxx.git" one).

like image 56
VonC Avatar answered Sep 23 '22 03:09

VonC


When you run git pull, git actually does a fetch followed by a merge. This means you can use the post-merge hook to execute a script when a pull is completed.

To set this up you just need to create an executable script in your repository's .git/hooks/ directory called post-merge.

Note that this script will not be run if the merge fails due to conflicts.

like image 45
nullability Avatar answered Sep 26 '22 03:09

nullability