How to use 'Git diff' to search changes regardless of folder depth
Platform Notice: Cloud and Data Center - This article applies equally to both cloud and data center platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Summary
This article explains how to utilize git diff
to search for changes in files regardless of their folder depth.
Cause
By default, the git diff
command requires specifying the exact folder depth when searching for changes in files.
For example, if you want to search for changes in the c
folder located within two nested folders (e.g., a/b/c
), you need to run the command git diff HEAD^ */*/c
. This behavior can be inconvenient when dealing with files nested in folders of varying depths.
Output
1
2
3
4
5
6
7
8
9
10
11
$ git diff HEAD^ -- **/c
$ git diff HEAD^ -- */*/c
diff --git a/a/b/c/one b/a/b/c/one
index 5626abf..4846250 100644
--- a/a/b/c/one
+++ b/a/b/c/one
@@ -1 +1,2 @@
one
+1
asdf
Solution
Use git diff
to search for changes in files regardless of their folder depth. Enable the globstar
shell option. Use the following steps:
Open the
.bashrc
file in your home directory (e.g.,~/.bashrc
).Add the following line to the file:
shopt -s globstar
Save the file and restart your terminal session.
Alternatively, you can enable the globstar
option at the beginning of a Pipelines script by adding the command shopt -s globstar
.
With the globstar
option enabled, you can use the ** wildcard to search for changes in any folder depth. For example, to search for changes in any c
folder regardless of its depth, run the command git diff HEAD^ **/c
.
Output
1
2
3
4
5
6
7
8
9
10
$ git diff HEAD^ -- **/c
diff --git a/a/b/c/one b/a/b/c/one
index 5626abf..4846250 100644
--- a/a/b/c/one
+++ b/a/b/c/one
@@ -1 +1,2 @@
one
+1
asdf
This will allow you to find changes in files located in folders of varying depths with a single command, making tracking changes in your repository more convenient and efficient.
Was this helpful?