I am using a repository in Gitlab. When I clone the repository, it has its usual remote origin which points to Gitlab. However, I also need to push the commits to a second repo and for that reason I set a second remote as soon as I clone the repo:
git remote add second <url_of_secondary_repo>
My question is, is there a way to store that second remote in the git config of the Gitlab repo so that I do not need to add it every time I clone the repo, but instead as soon as I clone it and query with git remote -v, I get e.g.
origin [email protected]:<user>/<repo>.git (fetch)
origin [email protected]:<user>/<repo>.git (push)
second <url_of_secondary_repo> (fetch)
second <url_of_secondary_repo> (push)
I don't see a way to commit and share git config across team, Although there is a workaround to share it through git hooks but I don't think it's worth it given your use case. Although I am still jotting it down incase.
In GIT, As soon as a repository is cloned
post-checkouthook gets called. You can put a shell command (git remote add second <url_of_secondary_repo>) in this hook file (.git/hooks/post-checkout). Git doesn't allow you to commit the hooks, But there are plenty of workaround available on internet on how to share the git hooks across your team. Now as soon as your team mate clone this repo,post-checkouthook will get executed automatically and second remote will get added to their config.Please note that post-checkout hook gets called every time you run git checkout command, So you will have to put a check in post-checkout hook to only run add remote command if second remote doesn't exist in config already.
ORMore sophisticated and better way of doing same is -
gitconfigat top level in the repo.git config --local include.path ../gitconfigin.git/hooks/post-checkoutfile. It's okay if this command get's executed every time post-checkout hook runs.Now once someone clones the repository the post-checkout hook will get executed and second remote will be added automatically, Instead of cloning the repo if they download it then second remote will be added as soon as they run checkout command for first time.
contents of gitconfig file -