With over 100 million devs all over the world, GitHub is the leading collaboration platform for programmers. What's even more cool about GitHub, it provides means for automating almost every aspect of development: from code edits, pushes, and reviews, to working with issues, managing users, and much more.
So today we’ll focus on answering a simple, yet very frequent question: how to push code to GitHub:
- You’ll learn (or recap) how to push code to GitHub from Terminal,
- Then, you’ll see how the same task is done automatically via n8n, a source-available workflow automation tool:
- Finally, we'll show how to further automate various GitHub tasks with n8n, leveraging the pre-built GitHub node. These examples can serve as an inspiration for creating versatile GitHub integrations with minimal coding.
Let’s dive in right away!
Method 1: Push code to GitHub using Command Line
If you're a proficient GitHub user, setting up a local Git repository and linking it to GitHub is likely your second nature. You can scroll right away to the method#2.
If you need a hand to push code to a new repository in GitHub, you’ll find the shortest possible getting-started tutorial in just a few paragraphs.
First of all, create a new repository in your GitHub account.
After you’ve created a new repository, GitHub will show a short memo.
Given that you possess a remote GitHub repository and have Git installed in your development environment, follow the subsequent steps and modify the command lines as needed:
mkdir -p <local-path-to-repo>
cd <local-path-to-repo>
git init
echo "# git_push_article" >> README.md
git add .
# optional commands:
# 1. If the folder owner is different from the user, add this:
git config --global --add safe.directory <local-path-to-folder>
# 2. If you test Git commands for the first time, also add this:
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git commit -m "first commit"
git branch -M main
git remote add origin 'https://<github-username>:<github-user-access-key>@github.com/<github-repo.git>'
git push -u origin main
This example essentially executes the following:
- Creates a new folder, navigates into it, and initializes Git repository,
- Generates a default README.md file and adds all existing directory files into the repository,
- Optional commands are needed only once per repository,
- Commits modifications to the local repository with a commentary and introduces a new branch called "main",
- Adds a remote repository destination on GitHub using your username and a temporary access token for convenience,
- Final command pushes code to a new repository in GitHub.
Upon successful execution, you'll find the new file on GitHub:
This was easy, right?
Method 2: Push code to GitHub using n8n nodes
Using n8n for GitHub push opens new horizons for you:
- First, n8n allows you to make changes directly in the GitHub repo. This proves particularly useful when you need to update parts of your Git documentation files without having to pull from GitHub and make local commits before pushing the changes back to the remote destination.
- Second, you can further automate Git processes. n8n enables automation of your local development cycle, checking the status of your local repo and pushing code to GitHub during off-hours.
Now, let's consider this workflow template for pushing and updating files in GitHib, where we update a README.md file.
Push via GitHub node
This approach enables you to update the GitHub repository without involving the local repo.
On the top part of the screenshot, there are three nodes:
- GitHub fetches README.md file from the repository and returns as binary data;
- Decode file node converts the base64 string into human-readable text;
- The final node pushes the edited file directly to GitHub:
{{ $json.data }}
## Updated at:
{{ $now.toISO() }}
Push via Git node / bash
Now let’s try an alternative approach and use the Git node for working with the local repo.
The bottom line of the screenshot shows several Git Nodes and one Execute Command node:
- Git pull fetches the remote repository and updates the local copy. This step is necessary because the GitHub repo was updated previously, and your local copy may be out of sync.
- The Update README and add new file node executes a series of local commands that write lines into the README.md file and create a new file.
echo '' >> {{ $('config').item.json.localrepo }}/README.md
echo '## Updated at:' >> {{ $('config').item.json.localrepo }}/README.md
echo '{{ $now.toISO() }}' >> {{ $('config').item.json.localrepo }}/README.md
echo 'Check new file' >> {{ $('config').item.json.localrepo }}/README.md
echo '' >> {{ $('config').item.json.localrepo }}/README.md
echo '# This is a new file' >> {{ $('config').item.json.localrepo }}/new_{{ $now.toFormat('yyyyddMM-hhmmss') }}.md
- The Add files, Commit and Push nodes perform exactly what their names suggest - they update all recent changes in the GitHub repo. It's important to note that the Git node works with the local repository. Therefore, you need to ensure that the Git config file is set up in advance.
Automate GitHub with n8n
Imagine streamlining your GitHub activities with n8n, navigating tasks like pull, fetch, commit and reading logs with ease. Check the full list of Git commands on the documentation page.
But that's not all. With GitHub Trigger and GitHub node, you can automate a host of actions. Catch and post new repository issues in a Slack channel, follow other public project repositories, or perform actions on your own. Create files, issue releases, and more. The scope for automation with n8n is limitless.
Finally, n8n allows you to connect GitHub with other systems. We’ve already mentioned posting new issues to Slack, but this is just the tip of the iceberg. Check out some useful community workflows, use them for free and adapt them to your needs:
Wrap up
In this tutorial, you’ve learned how to push code to GitHub automatically using two different approaches:
- Via GitHub node. This way you can work directly with a remote repository on GitHub. It’s an easy way, however, not very convenient when you need to update many files;
- Via Git node. This approach requires that n8n has access to a local Git repository. It requires a bit deeper knowledge of Git.
Alternatively, you can use git bash commands instead of Git node. This is the most advanced way of working with local repositories. You can push and pull multiple files at once or you can easily run commands such as git commit and push in a single Execute Command node.
On top of that, we’ve provided several links to free n8n workflows involving GitHub automations. They can serve as a great source for your inspiration on what to automate next.
What’s next?
You can also integrate your Git workflow with other cloud providers, such as GitLab, Bitbucket or an open-source Gitea.
Whether you're working alone, in a small team, or in an enterprise, n8n got you covered. Choose from our cloud plans and jump-start right away or explore powerful features of the Enterprise edition, trusted by many large corporations.
Join the community forum today to share your GitHub automation success stories and ideas!