{"id":32725,"date":"2026-11-29T22:05:38","date_gmt":"2026-11-29T22:05:38","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=32725"},"modified":"2026-11-29T22:05:38","modified_gmt":"2026-11-29T22:05:38","slug":"shell-script-to-git-add-and-commit-and-push-each-directory-at-a-time","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/shell-script-to-git-add-and-commit-and-push-each-directory-at-a-time\/","title":{"rendered":"Shell script to git add and commit and push each directory at a time"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This page shows how to use a Bash script to stage, commit and push changes one directory at a time. That can mean a folder full of separate Git repositories, or one repository where each top-level directory gets its own commit. Below the original script you will find what is wrong with it, a corrected version for each case, and the errors you are most likely to see when you run them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This script will loop through each directory in the repository, checking if it is a Git repository. If it is, it will add all changes in the directory, commit them with a message that includes the directory path, and push the changes to the remote repository. Note that this script assumes that the repository is structured such that each directory is a separate Git repository.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/5daa095b7af5e406196cfe83d2f13f7d.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Two different problems behind the same title<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">&quot;Commit each directory at a time&quot; can mean two things, and they need different scripts:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Layout<\/th><th>What you want<\/th><th>Script to use<\/th><\/tr><\/thead><tbody><tr><td>A parent folder (for example <code>~\/projects<\/code>) containing many independent repositories<\/td><td>Commit and push every repository in one run<\/td><td>Script A: multi-repository loop<\/td><\/tr><tr><td>One repository with several top-level folders (for example <code>frontend\/<\/code>, <code>backend\/<\/code>, <code>infra\/<\/code>)<\/td><td>One commit per folder so the history stays readable, then push<\/td><td>Script B: one commit per directory<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The script above is aimed at the first layout, but it starts with <code>git rev-parse --show-toplevel<\/code>. That command only works inside a repository, so the script fails in exactly the layout it was written for.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is wrong with the original script<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>It needs a parent repository.<\/strong> <code>git rev-parse --show-toplevel<\/code> exits with <code>fatal: not a git repository<\/code> when it runs in a plain folder that holds several repositories.<\/li>\n\n\n<li><strong>Paths with spaces break it.<\/strong> <code>for dir in $(find ...)<\/code> splits the output on whitespace and expands glob characters, so <code>My Project<\/code> turns into two invalid paths.<\/li>\n\n\n<li><strong>It walks inside <code>.git<\/code>.<\/strong> <code>find -type d<\/code> lists every folder under every <code>.git<\/code> directory, often thousands of them, and runs <code>cd<\/code> into each one for nothing.<\/li>\n\n\n<li><strong>It ignores failed <code>cd<\/code> commands.<\/strong> If <code>cd &quot;$dir&quot;<\/code> fails, the Git commands that follow run in whatever folder the script happens to be in.<\/li>\n\n\n<li><strong>It misses some repositories.<\/strong> <code>[ -d .git ]<\/code> is false for submodules and linked worktrees, because there <code>.git<\/code> is a file, not a folder.<\/li>\n\n\n<li><strong>It assumes every repository has changes and an upstream.<\/strong> In a clean repository, <code>git commit<\/code> exits with an error. On a new branch with no upstream, <code>git push<\/code> fails.<\/li>\n\n\n<li><strong>It has stray comment lines.<\/strong> Generated text was pasted into the code as comments. They are harmless but make the script harder to read.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Script A: commit and push every repository under a folder<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This version uses <code>find ... -print0<\/code> together with <code>read -d &#039;&#039;<\/code>, so any file name works, spaces included. <code>-prune<\/code> stops <code>find<\/code> from looking inside <code>.git<\/code>. <code>git -C<\/code> runs each command in the target repository without any <code>cd<\/code>.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/usr\/bin\/env bash<\/span>\n<span class=\"hljs-comment\"># Commit and push every Git repository found under a parent directory.<\/span>\n<span class=\"hljs-comment\"># Usage: .\/commit-all-repos.sh &#91;parent_dir] &#91;commit message]<\/span>\nset -uo pipefail\n\nbase_dir=<span class=\"hljs-string\">\"${1:-.}\"<\/span>\nmessage=<span class=\"hljs-string\">\"${2:-Automated commit}\"<\/span>\nfailed=()\n\n<span class=\"hljs-keyword\">while<\/span> IFS= read -r -d <span class=\"hljs-string\">''<\/span> git_entry; <span class=\"hljs-keyword\">do<\/span>\n  repo=<span class=\"hljs-string\">\"$(dirname \"<\/span>$git_entry<span class=\"hljs-string\">\")\"<\/span>\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"==&gt; $repo\"<\/span>\n\n  <span class=\"hljs-comment\"># Commit only if there are tracked or untracked changes<\/span>\n  <span class=\"hljs-keyword\">if<\/span> &#91; -n <span class=\"hljs-string\">\"$(git -C \"<\/span>$repo<span class=\"hljs-string\">\" status --porcelain)\"<\/span> ]; then\n    git -C <span class=\"hljs-string\">\"$repo\"<\/span> add -A &amp;&amp;\n      git -C <span class=\"hljs-string\">\"$repo\"<\/span> commit -q -m <span class=\"hljs-string\">\"$message ($(basename \"<\/span>$repo<span class=\"hljs-string\">\"))\"<\/span> ||\n      { failed+=(<span class=\"hljs-string\">\"$repo (commit)\"<\/span>); <span class=\"hljs-keyword\">continue<\/span>; }\n  <span class=\"hljs-keyword\">else<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"    no local changes\"<\/span>\n  fi\n\n  <span class=\"hljs-comment\"># Skip push on a detached HEAD<\/span>\n  branch=<span class=\"hljs-string\">\"$(git -C \"<\/span>$repo<span class=\"hljs-string\">\" symbolic-ref --quiet --short HEAD)\"<\/span> ||\n    { <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"    detached HEAD, skipping push\"<\/span>; <span class=\"hljs-keyword\">continue<\/span>; }\n\n  <span class=\"hljs-keyword\">if<\/span> git -C <span class=\"hljs-string\">\"$repo\"<\/span> rev-parse --quiet --verify <span class=\"hljs-string\">'@{u}'<\/span> &gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span>; then\n    git -C <span class=\"hljs-string\">\"$repo\"<\/span> push -q || failed+=(<span class=\"hljs-string\">\"$repo (push)\"<\/span>)\n  elif git -C <span class=\"hljs-string\">\"$repo\"<\/span> remote get-url origin &gt;\/dev\/<span class=\"hljs-keyword\">null<\/span> <span class=\"hljs-number\">2<\/span>&gt;&amp;<span class=\"hljs-number\">1<\/span>; then\n    git -C <span class=\"hljs-string\">\"$repo\"<\/span> push -q -u origin <span class=\"hljs-string\">\"$branch\"<\/span> || failed+=(<span class=\"hljs-string\">\"$repo (push)\"<\/span>)\n  <span class=\"hljs-keyword\">else<\/span>\n    <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"    no remote 'origin', skipping push\"<\/span>\n  fi\ndone &lt; &lt;(find <span class=\"hljs-string\">\"$base_dir\"<\/span> -name .git -prune -print0)\n\n<span class=\"hljs-keyword\">if<\/span> &#91; <span class=\"hljs-string\">\"${#failed&#91;@]}\"<\/span> -gt <span class=\"hljs-number\">0<\/span> ]; then\n  printf <span class=\"hljs-string\">'FAILED: %s\\n'<\/span> <span class=\"hljs-string\">\"${failed&#91;@]}\"<\/span> &gt;&amp;<span class=\"hljs-number\">2<\/span>\n  <span class=\"hljs-keyword\">exit<\/span> <span class=\"hljs-number\">1<\/span>\nfi\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"All repositories processed.\"<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>find &quot;$base_dir&quot; -name .git -prune -print0<\/code> finds each <code>.git<\/code> entry, whether it is a folder or a file, and does not descend into it.<\/li>\n\n\n<li><code>git status --porcelain<\/code> prints nothing when the repository is clean, so clean repositories skip the commit and do not produce an error.<\/li>\n\n\n<li><code>rev-parse --verify &#039;@{u}&#039;<\/code> checks whether the current branch has an upstream. If it does not, the script pushes with <code>-u origin &lt;branch&gt;<\/code> to create one.<\/li>\n\n\n<li>Failures are collected in <code>failed<\/code> instead of stopping the run. The script exits with status 1 at the end if any repository failed, which suits cron jobs and CI.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Script B: one commit per directory inside a single repository<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use this when everything lives in one repository and you want a separate commit for each top-level folder. It pushes once at the end, which sends all the new commits together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">#!\/usr\/bin\/env bash<\/span>\n<span class=\"hljs-comment\"># Create one commit per top-level directory, then push once.<\/span>\nset -euo pipefail\n\ncd <span class=\"hljs-string\">\"$(git rev-parse --show-toplevel)\"<\/span>\n\n<span class=\"hljs-keyword\">for<\/span> dir in *\/; <span class=\"hljs-keyword\">do<\/span>\n  dir=<span class=\"hljs-string\">\"${dir%\/}\"<\/span>\n  &#91; -d <span class=\"hljs-string\">\"$dir\"<\/span> ] || <span class=\"hljs-keyword\">continue<\/span>                  <span class=\"hljs-comment\"># no subdirectories at all<\/span>\n\n  <span class=\"hljs-comment\"># Skip folders with no changes (also skips ignored and empty folders)<\/span>\n  &#91; -n <span class=\"hljs-string\">\"$(git status --porcelain -- \"<\/span>$dir<span class=\"hljs-string\">\")\"<\/span> ] || <span class=\"hljs-keyword\">continue<\/span>\n\n  git add -A -- <span class=\"hljs-string\">\"$dir\"<\/span>\n  git commit -q -m <span class=\"hljs-string\">\"Update $dir\"<\/span> -- <span class=\"hljs-string\">\"$dir\"<\/span>\n  <span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-string\">\"Committed $dir\"<\/span>\ndone\n\ngit push<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li><code>git add -A -- &quot;$dir&quot;<\/code> stages new, changed and deleted files, but only inside that folder.<\/li>\n\n\n<li><code>git commit ... -- &quot;$dir&quot;<\/code> commits only that path, so changes you staged elsewhere earlier do not slip into the wrong commit.<\/li>\n\n\n<li><code>*\/<\/code> does not match hidden folders such as <code>.github\/<\/code>, and files in the repository root are not committed. Add a final <code>git add -A &amp;&amp; git commit -m &quot;Update root files&quot;<\/code> if you need those too.<\/li>\n\n\n<li>To push after each commit instead of once at the end, move <code>git push<\/code> inside the loop.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Running the scripts<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Save the script, for example as <code>commit-all-repos.sh<\/code>, and make it executable: <code>chmod +x commit-all-repos.sh<\/code>.<\/li>\n\n\n<li>Check what will be committed before the first run. For Script A: <code>for d in *\/; do echo &quot;== $d&quot;; git -C &quot;$d&quot; status -s 2&gt;\/dev\/null; done<\/code>. For Script B: <code>git status -s<\/code>.<\/li>\n\n\n<li>Make sure pushes work without a prompt: an SSH key loaded in your agent, or an HTTPS credential helper with a token.<\/li>\n\n\n<li>Run it. Script A: <code>.\/commit-all-repos.sh ~\/projects &quot;Nightly sync&quot;<\/code>. Script B: run it from anywhere inside the repository.<\/li>\n\n\n<li>Confirm the result with <code>git log --oneline -5<\/code> in one of the repositories.<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">==&gt; <span class=\"hljs-regexp\">\/home\/<\/span>dev\/projects\/api\n==&gt; <span class=\"hljs-regexp\">\/home\/<\/span>dev\/projects\/docs\n    no local changes\n==&gt; <span class=\"hljs-regexp\">\/home\/<\/span>dev\/projects\/scratch\n    no remote <span class=\"hljs-string\">'origin'<\/span>, skipping push\nAll repositories processed.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>fatal: not a git repository<\/code><\/td><td>The original script was run from a folder that is not a repository<\/td><td>Use Script A, which does not need a parent repository<\/td><\/tr><tr><td><code>nothing to commit, working tree clean<\/code><\/td><td><code>git commit<\/code> ran in a repository with no changes<\/td><td>Check <code>git status --porcelain<\/code> first, as both scripts do<\/td><\/tr><tr><td><code>fatal: The current branch has no upstream branch<\/code><\/td><td>A new local branch was never pushed<\/td><td>Push with <code>git push -u origin &lt;branch&gt;<\/code>. Script A does this automatically<\/td><\/tr><tr><td><code>Author identity unknown<\/code><\/td><td><code>user.name<\/code> or <code>user.email<\/code> is not set<\/td><td>Run <code>git config --global user.name &quot;Name&quot;<\/code> and <code>git config --global user.email you@example.com<\/code><\/td><\/tr><tr><td><code>! [rejected] ... (fetch first)<\/code><\/td><td>The remote has commits you do not have locally<\/td><td>Run <code>git pull --rebase<\/code> in that repository, then run the script again<\/td><\/tr><tr><td><code>Permission denied (publickey)<\/code><\/td><td>No SSH key is available, which is common under cron<\/td><td>Load the key in <code>ssh-agent<\/code> or set <code>GIT_SSH_COMMAND<\/code> to point at a key file<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Safety notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>git add -A<\/code> commits everything that <code>.gitignore<\/code> does not exclude, including <code>.env<\/code> files, credentials and build output. Check each repository&#039;s <code>.gitignore<\/code> before you automate this.<\/li>\n\n\n<li>Automatic commits with generic messages make history hard to read. Keep these scripts for sync and backup repositories, not for shared code that people review.<\/li>\n\n\n<li>Never add <code>--force<\/code> to the push in a bulk script. One bad run could overwrite other people&#039;s work in every repository at once.<\/li>\n\n\n<li>For large trees, add <code>-maxdepth 3<\/code> to the <code>find<\/code> command, or exclude folders such as <code>node_modules<\/code>, to keep Script A fast.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Branching, rebasing and pull-request flows are covered in the <a href=\"https:\/\/www.devopsschool.com\/blog\/git-workflow-master-tutorial\/\">Git Workflow Master Tutorial<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How do I commit and push all Git repositories in a folder at once?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Loop over every <code>.git<\/code> entry found by <code>find &lt;folder&gt; -name .git -prune -print0<\/code>. For each one, run <code>git -C &lt;repo&gt; add -A<\/code>, <code>commit<\/code> and <code>push<\/code>, as Script A does. This handles spaces in paths and skips repositories that have no changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I make a separate commit for each folder in one repository?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For each folder, run <code>git add -A -- &lt;folder&gt;<\/code> and then <code>git commit -m &quot;...&quot; -- &lt;folder&gt;<\/code>. Run <code>git push<\/code> once at the end. Adding the path after <code>--<\/code> limits each commit to that folder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my bulk push script fail with &#039;no upstream branch&#039;?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A branch that has never been pushed has no remote tracking branch, so a plain <code>git push<\/code> does not know where to send it. Push once with <code>git push -u origin &lt;branch&gt;<\/code>, or let the script detect the missing upstream and do it for you.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I run this script from cron?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, as long as Git can authenticate without a prompt and the commit identity is configured. That means an SSH key with no passphrase or a running agent, or an HTTPS credential helper. Use absolute paths, and send output to a log file so you can check failures later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does git add -A include deleted files?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. <code>git add -A<\/code> stages new, modified and deleted files. Adding a path after <code>--<\/code> limits that to the given folder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/git-workflow-master-tutorial\/\">Git Workflow Master Tutorial<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-git-and-use-cases-of-git\/\">What is Git and use cases of Git?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-interview-question-and-answers-of-git\/\">Top 50 Interview question And answers of Git<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/devops\/git\/\">More Git tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>This page shows how to use a Bash script to stage, commit and push changes one directory at a time. That can mean a folder full of&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[38],"tags":[137,5020],"class_list":["post-32725","post","type-post","status-publish","format-standard","hentry","category-git","tag-bash","tag-git-commit"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=32725"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32725\/revisions"}],"predecessor-version":[{"id":80419,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/32725\/revisions\/80419"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=32725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=32725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=32725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}