Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I list all folder names that have files changed in Git?

Tags:

git

bash

shell

git diff --name-only or git diff --name-status will list all files that have been changed, however there is no command to list all folder names that contain files changed.

For example, with this directory tree:

test/
|
|__B/
|
|____b1.txt
|
|__C/
|
|____c1.txt

If b1.txt and c1.txt have changed, I'd like to get B and C as the output.

like image 328
Bun Avatar asked Mar 17 '18 14:03

Bun


People also ask

How do I get a list of files changed in a branch?

To get the list of files modified (and committed!) in the current branch you can use the shortest console command using standard git: git diff --name-only master... If your local "master" branch is outdated (behind the remote), add a remote name (assuming it is "origin"): git diff --name-only origin/master...

Can git track folders?

Git will only track Files and the changes in them. So folders are tracked as part of the file changes in them. In order to create an empty folder and commit it, there have to be some files inside it.


2 Answers

Here's a way:

git diff --name-only | xargs -L1 dirname | uniq

Explanation

  • git diff --name-only: list all changed files
  • xargs -L1 dirname: remove filenames to keep only directories
  • uniq: remove duplicates

You can create an alias so you don't have to type the whole command each time you need it:

alias git-diff-folders="git diff --name-only | xargs -L1 dirname | uniq"
like image 197
Ronan Boiteau Avatar answered Oct 08 '22 20:10

Ronan Boiteau


This command would give results of top level directories. git status | cut -d'/' -f1

For a more in depth solution I am currently working with ${PWD##*/} in bash to find a more correct solution to your problem.

Trying to write a solution for you in git that is simular to this npm solution:

get the path where npm gulp called from

im no longer searching for the answer on this one,

git diff --name-only | xargs -L1 dirname | uniq

was the correct answer provided by Pᴇʜ

it works on my mac computer after his edit's

like image 2
Michael Dimmitt Avatar answered Oct 08 '22 20:10

Michael Dimmitt