List git repositories with unpushed changes
Asked Answered
E

4

7

I'm reinstalling my OS. I have thousands of repos on my computer. I have a feeling that I've made a few changes in some repos while hacking around that are not committed and/or pushed.

From a bash shell on my Debian PC, what's the best way to find a list of changes that have not yet been committed/pushed to their remotes, and decide whether or not I want to keep the changes?

Euphroe answered 29/3, 2014 at 19:16 Comment(2)
What about uncommitted changes?Rachellerachis
Sorry for the silly comment, but hi Greg. First time I realize 2 people can have the same SO name.Euphroe
M
5

You may like the project multi-git-status from Ferry Boender.

https://github.com/fboender/multi-git-status.
"Show uncommited, untracked and unpushed changes in multiple Git repositories."

enter image description here

Membranous answered 22/11, 2017 at 15:0 Comment(0)
G
2

I'm assuming finding each repo and running git status in each repo is sufficient for your needs. In which case the following might be a starting point for you:

find / -name "*.git" -type d -print0 | xargs -0 -L 1 -i% bash -c "cd %/..; pwd; git status -s -uno"

find traverses your filesystem, starting at / (you may want to restrict that to perhaps ~), searching for .git diretories. The output (null-delimited to handle funny filenames) is piped to xargs, which cds to each repo, prints the pwd, and short-form git-status, without listing untracked files.

Gettogether answered 29/3, 2014 at 20:41 Comment(0)
C
2

The previous answer finds uncommitted, but not unpushed commits.

This will find both:

find / -name "*.git" -type d -print0 | xargs -0 -L 1 -i% bash -c "cd %/..; pwd; git status -s -uno; git cherry -v 2>/dev/null"

git cherry shows the SHA1 of unpushed changes; -v adds the commit comment; and error redirection suppresses noisy output for repos that lack an upstream.

Cannonry answered 22/11, 2017 at 14:49 Comment(1)
on Mac (using zsh) a lowercase -i throws "xargs: illegal option -- i"; using capital I works: xargs -0 -L 1 -I% bash -cDanadanae
D
1

Be sure you check all branches

Unpushed changes can exist in branches other than the checked-out branch. There can also be more than one upstream per branch.

Zero in on relevant info

This approach takes its lead from other answers, which are very helpful starting points, and subs out a few details.

find / -name "*.git" -type d -print0 | xargs -0 -L 1 -I% sh -c "cd %/..; pwd; git status -suno; git branch -vvv 2>/dev/null" > local_repos.txt

The above one-liner writes to a file called local_repos.txt, which is the easiest way to read the output. It

  • lists all branches, marking the checked-out branch with an asterisk
  • clarifies when repo HEAD is detached
  • shows tracked files with uncommitted changes
  • distinguishes which branches have upstreams and which do not
  • counts the commits of each branch that is ahead/behind its upstreams
  • gives the most recent sha & commit message on each branch

Like the other solutions, it also lists the path to each repo. Unlike the other solutions, it does not break out a list of references to unpushed commits on the checked-out branch.

Example output

/dir-with-repo-1
    main  d96c7fcd [origin/main: behind 3] update abcd
  * staging f938e4aa [origin/staging: ahead 2, behind 6] tweak workspace layout
/dir-with-repo-2
  * dev   8b9cec2b [origin/dev] Merge branch 'feature/35-featurename' into 'dev'
    main  b96d6fcd [origin/main: behind 4] update efgh
/dir-with-repo-3
  * hotfix  239f95dc [origin/hotfix] update ijkl
    main    4c92eaa7 [origin/main: behind 7] update klmn
/dir-with-repo-4
  M composer.json
  M composer.lock
  * main 0d2050c add opqr
/dir-with-repo-5
  * (HEAD detached at d3286d57) d3286d57 fix(FunctionComment): Add support for hijk
    1.2.3                       d3286d57 [origin/1.2.3] Add support for hijk
Danadanae answered 13/3, 2022 at 19:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.