Is it possible to run automatically a .bash script after running git clone ..repo..
inside the terminal? At first I wanted to run commands like rm -rf .git
and composer install
inside the post-checkout hook but this file can only be used locally. So are there other options to do this?

- 1,143
- 2
- 19
- 39
-
Where would you like it to run if not locally? – ElpieKay Jul 26 '17 at 12:31
-
It must run locally but I don't wan't to create a file by hand for every user that's cloning a project. – CodeWhisperer Jul 26 '17 at 12:44
2 Answers
mkdir ~/global_hooks
#create and edit the post-checkout inside
echo '#!/bin/bash' >> ~/global_hooks/post-checkout
echo 'rm -rf .git' >> ~/global_hooks/post-checkout
#...
chmod 755 ~/global_hooks/post-checkout
git config --global core.hooksPath ~/global_hooks/
Any new clone with a checkout (without -n
or --mirror
or --bare
) will trigger ~/global_hooks/post-checkout
and .git
will be removed. But this is not a good idea because you'll have to enable and disable the hook for different clones. Besides, every user needs to make the configuration, so it's a bit annoying.
What's worse, a git-checkout in a repository without disabling or overriding the global hook will remove its .git
. So this is just for fun but not practical at all.
Edit 2022-06-02:
Some improvements to make it a bit more practical.
First, DO NOT run git config --global core.hooksPath ~/global_hooks/
.
Second, update ~/global_hooks/post-checkout
#!/bin/bash
enable=$(git config --get my.fun)
if [[ "${enable}" = yes ]];then
echo Removing .git
rm -rf .git
fi
Now it's possible to enable or disable the hook according to demand.
By default, a new clone or checkout does not invoke the hook, so the .git
is safe.
When you really want to remove .git
after it's cloned, add 2 options to git
.
git -c core.hookspath=~/global_hooks/ -c my.fun=yes clone $repo_url -b $branch
-c foo.bar=baz
is a temporary configuration value which works only for this git command. my.fun
is a customized configuration word and you could replace it with other words you like.

- 27,194
- 6
- 32
- 53
-
`post-checkout` is run after all checkouts, and not just after a clone. Which is just something to be aware of, if you make a different version of this, where you only temporarily disable the hook when you run your clone of repos where you don't want `.git/` deleted, so you don't accidentally end up losing the `.git/` folder next time you run a checkout (e.g. checkout a different branch). (not an issue in the latter solution above, as you only include the hook specifically in the clone command) – Svend Hansen Aug 22 '23 at 21:35
So, you want a repo configured such that when someone clones it, it immediately turns around and deletes everything but the work tree (making it no longer a clone of the repo)? No, git
does not have a way to do that.
If you just want to automate these steps because you have some frequent use case for it (maybe an unusual deployment mechanism?), then you can write a wrapper script to be run in lieu of the git clone
command.
But if you literally want this triggered by the git clone
command itself, that's not possible. (It's also not useful; but that's none of my business.)

- 42,148
- 4
- 35
- 52