DEV Community

Cover image for Push to Multiple Repositories Simultaneously Using Git Hooks
David Ochiel
David Ochiel

Posted on

Push to Multiple Repositories Simultaneously Using Git Hooks

Automating repetitive tasks is crucial in software development to enhance workflow efficiency. In this article, we'll dive into leveraging git hooks to automate actions after each commit, to push to two different repositories, one in github, and another in gitea.

What are hooks?

Hooks are scripts that git executes in response to certain events. There are various types of hooks, including pre-commit, post-commit, pre-push, and more. In git, you can use hooks to perform tasks like sending notifications, triggering builds, or updating deployments automatically after each commit. Check out this documentation for more on hooks.

Setting Up a Post-Commit Hook

To set up a post-commit hook in your git repository, follow these steps:

  • Create a directory, say, myproject using mkdir myproject, where you will place you project files and directories.
  • Navigate to the directory using cd myproject/, customize your development environment using the git config --global convention, and initialize a git repository, git init.
  • Create the remote repositories for both gitea and github. For github, you will need to have a PAT for push authentication.
# gitea
git remote add gitea <repository-url>

# github
git remote add github https://<your-token>@github.com/<username>/<repository-name>.git

Enter fullscreen mode Exit fullscreen mode
  • After that, when you run, git remote you should have the following as your remote repositories:
gitea
github
Enter fullscreen mode Exit fullscreen mode
  • Navigate to the .git directory and create a post-commit script in the hooks directory. Note that the post-commit script doesn't have to have an extension.
cd .git/
nano hooks/post-commit
Enter fullscreen mode Exit fullscreen mode

The nano text editor will open as in the image below:

Image description

Write your desired actions or commands in the script. In this case, copy and paste the following:

# Push changes to Gitea
git push gitea master

# Push changes to GitHub
git push github master
Enter fullscreen mode Exit fullscreen mode

The result should be as per the image below:

Image description

To save your file, press CTRL+X, then y, then press ENTER key.

  • Ensure the script has execute permissions.
chmod +x hooks/post-commit
Enter fullscreen mode Exit fullscreen mode
  • Start working on your project
# move back from the .git directory
cd ..

# work on your project
touch README.md
git add .
git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

So why wait? Start using hooks today and take your development workflow to the next level!

If you have any questions, concerns, or want to share your experiences with hooks, feel free to leave a comment below.

Happy coding :)

Top comments (4)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

Good post, but you might be reinventing the wheel a little here... most online Git repo sites (including Gitea) have a mirroring feature where any activity in one repo can be mirrored in repos elsewhere.

Collapse
 
ccoveille profile image
Christophe Colombier

I don't mind people reinventing the wheel. So I would say great idea. Some others solution exist, yes. But I'm sure @nguonodave learned things by doing it, and was happy to share his idea and how he did it. Now, he may look at gitea features or equivalent, and might learn new things.

Learning is a journey, everyone has his path.

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ

It wasn't meant as a criticism... just pointing it out. I'm a big advocate generally of wheel reinventing - you learn lots, and might end up making a better wheel.

Thread Thread
 
nguonodave profile image
David Ochiel

Thanks for the insight! Actually you are absolutely right. Mirroring features are super convenient. My main goal, however, was more about understanding git's inner workings, like how hooks operate, rather than solely focusing on pushing to multiple repositories. It's been a rewarding exploration into the mechanics behind git's automation, teaching me valuable insights along the way.