Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - How to find all "unpushed" commits for all projects in a directory?

Tags:

git

bash

I'm finally getting used to Git and, after the initial steep learning curve, I must say it's quite good (I just miss the single file externals, but that's another story). I have, however, an issue that I can't solve: I'm currently working on a dozen projects at the same time. They are all interconnected and I must jump from one to the other, making changes here and there.

All good so far, I can "juggle" them quite easily, I commit changes regularly and so on. The issue is that, after several hours, I don't remember which projects I pushed to the remote repository. Of course, I can "cd" into each project's directory and issue the push command, but it's tedious. I was wondering if there would be a way, using bash, to run something like a git find all unpushed commits in all projects in this directory. Such command would be run from a directory which is not a Git repository, but which contains a tree of all the projects.

The logic is simple, but the implementation seems quite complicated, also because, although the root directory contains all the projects, the actual Git repositories can be found at any level from the starting directory. For example:

  • projects directory
    • customer X
    • project1 (Git repo)
    • customer U
    • project2 (Git repo)
    • project3
      • somelibrary (Git repo)
      • theme (Git repo)

In this example, only directories in bold are Git repositories, therefore the check should be run inside them. I'm aware that the structure looks a bit messy, but it's actually quite easy to handle.

Any help is welcome, thanks.

like image 359
Diego Avatar asked Sep 19 '12 16:09

Diego


1 Answers

You can use the following one-liner, which you can use to search from a directory which is not a Git repository, as you requested. This will list the absolute paths to the git repositories which have unpushed commits:

find . -type d -iname '.git' -exec sh -c 'cd "${0}/../" && git status | grep -q "is ahead of" && pwd' "{}" \;

This works by recursively searching for .git directories, and when found cd to the parent directory, which is the git project root. Then run git status and grep for the "is ahead of" phrase. If this is true the command after && is executed, in this case the 'pwd' command. It is here where you can insert you own commands (for example to show the unpushed commits).

Note: only works for unpushed commits on the active branch

like image 75
Qetesh Avatar answered Nov 15 '22 20:11

Qetesh