Is there a way to see what files have changed in a branch?
An alternative to the answer by @Marco Ponti, and avoiding the checkout:
git diff --name-only <notMainDev> $(git merge-base <notMainDev> <mainDev>)
If your particular shell doesn't understand the $() construct, use back-ticks instead.
git diff --name-only <some-other-branch>
will show you what files are different between your current branch and <some-other-branch>
. So it's essentially the same command, but note that you can use this to find the files that are different between any two branches, even if they're not remotely related. Whether that comparison is useful or not depends on the topology of your branches... Also, note <some-other-branch>
really could be any commit at all, or anything that resolves to one (tags, etc.). –
Hanky <notMainDev>
and <MY_CURRENT_CO_BRANCH>
most recently had a common ancestor, and compare <notMainDev>
to that ancestor. You'll have to provide your current branch name, though, as git merge-base
expects two arguments - there's not a shortcut, at least in the current version. –
Hanky git branch | grep '\*' | awk '{print $2}'
that will get the commit for the branch between <notMainDev> and my current branch. I can then do git diff --name-only <notMainDev> $(git merge-base <notMainDev> git branch | grep '\*' | awk '{print $2}'
) –
Improvisator etat [alias] diffbranch = "!sh -c 'git diff --name-only $1
git merge-base $1 $(git name-rev HEAD 2> /dev/null | cut -f2)`' -" !!! –
Improvisator $()
syntax working by using a git bash session - any tips for how I might get it to work in a standard command window (so I can put it in a batch file)? –
Armoured git diff --name-only origin/branchX $(git merge-base origin/branchX origin/master)
vs git diff --name-only branchX $(git merge-base branchX master)
which gives fatal: Not a valid object name branchX. fatal: ambiguous argument 'branchX': unknown revision or path not in the working tree.
–
Smash git diff --merge-base <notMainDev> --name-only <mainDev>
. –
Obstinate git merge-base
doesn't give the original forking point of the branch, but the closest common ancestor with the other (presumed main) branch. In many cases, both would be identical, but this solution won't work if the branch contains a commit merging main branch into it, in which case it will only list files that have been modified after the last merge, but any files modified in the branch before that merge commit (although not merged into main branch yet) won't be listed. –
Attribute All you have to do is the following:
git checkout <notMainDev>
git diff --name-only <mainDev>
This will show you only the filenames that are different between the two branches.
<mainDev>
since the branches diverged, though. You might want to use git diff --name-only <sha-of-branch-point>
instead, or see the alternate answer I posted that avoids the checkout. –
Hanky notMainDev
would be kept up to date with the mainDev commits...I usually find it useful to see those differences as well though. –
Contagion <sha-of-branch-point>
with git rev-parse <branch-name>
–
Illuminative amazed this has not been said so far!
git diff main...branch
So see the changes only on branch
To check the current branch use
git diff main...
Thanks to jqr
This is short hand for
git diff $(git merge-base main branch) branch
so the merge base (the most recent common commit between the branches) and the branch tip
Also using origin/main
instead of just master will help in case your local main is dated
git diff --name-only master..
if you just want the names of files that are different between the two branches. –
Goosander master
branch and a feature
branch. The master
branch was way ahead by a bunch of commits. The feature
branch was behind, but also had master
merged into it a few times to pull updates. using git diff --name-status master...feature
gave me exactly what Upsource was showing in the branch review. –
Fremantle git diff --name-only main...
or, if you have diffstat
, you could also pipe the result through diffstat
: git diff main... | diffstat
. –
Grenade I can't believe there are so many ways to do this. I use whatchanged as someone posted before, just with the following arguments:
git whatchanged --name-only --pretty="" origin..HEAD
This just lists the filenames, and only the ones that changed on the current branch.
New users are encouraged to use git-log instead. The whatchanged command is essentially the same as git-log but defaults to show the raw format diff output and to skip merges.
–
Biffin sort -u
–
Smash Update Nov 2020:
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...
If you want to include uncommitted changes as well, remove the
...
:git diff --name-only master
If you use different main branch name (eg: "main"), substitute it:
git diff --name-only main...
If your want to output to stdout (so its copyable):
git diff --name-only master... | cat
If your want filenames to be clickable in VSCode terminal no matter what folder you are running this command from, add
--relative
:git diff --name-only --relative master... | cat
per really nice detailed explanation of different options https://blog.jpalardy.com/posts/git-how-to-find-modified-files-on-a-branch/
I really liked @twalberg's answer but I didn't want to have to type the current branch name all the time. So I'm using this:
git diff --name-only $(git merge-base master HEAD)
git diff master... --name-only
when run on the target branch and get the same result. Could you be so kind as to provide any feedback to what's good vs bad between your answer and the command I've provided? –
Karmenkarna git diff master.. --name-only
(note there is only 2 dots instead of 3). To understand what the dots mean, see this answer –
Waldrop git diff --name-only master...branch-name
to which we want to compare.
git whatchanged
seems to be a good alternative.
New users are encouraged to use git-log instead. The whatchanged command is essentially the same as git-log but defaults to show the raw format diff output and to skip merges.
–
Biffin What if it could be as easy as this?
git changed
If you're willing to assume that the main branch is called "master", and that you create your other branches from master, then you can add this alias to your ~/.gitconfig
file to make it that easy:
cbranch = !"git branch | grep '*' | cut -f2 -d' '"
changed = !"git diff --name-only $(git cbranch) $(git merge-base $(git cbranch) master)"
Those assumptions will work for most people in most situations, but you must be aware that you're making them.
Also, you must use a shell that supports $()
. It's very likely that your shell supports this.
For some reason no one mentioned git-tree
. See https://mcmap.net/q/14241/-how-do-i-list-all-the-files-in-a-commit
git-tree
is preferred because it's a plumbing command; meant to be programmatic (and, presumably, faster)
(assuming base branch is master
)
git diff-tree --no-commit-id --name-only -r master..branch-name
However this will show you all files which were affected in the branch, if you want to see explicitly modified files only, you can use --diff-filter
:
git diff-tree --no-commit-id --name-only -r master..branch-name --diff-filter=M
Also one can use --name-status
instead of --name-only
to see the status of the files (A
/M
/D
and so on)
rubocop --fail-level error $(git diff-tree --no-commit-id --name-only -r origin/master..HEAD --diff-filter=M)
–
Superficial Considering you're on a feature branch and you want to check which files have changed compared to master... just this:
git diff --name-only master
git show --stat origin/branch_name
This will give you a list of the files that have been added or modified under this branch.
The accepted answer - git diff --name-only <notMainDev> $(git merge-base <notMainDev> <mainDev>)
- is very close, but I noticed that it got the status wrong for deletions. I added a file in a branch, and yet this command (using --name-status
) gave the file I deleted "A" status and the file I added "D" status.
I had to use this command instead:
git diff --name-only $(git merge-base <notMainDev> <mainDev>)
I use grep so I only get the lines with diff --git which are the files path:
git diff branchA branchB | grep 'diff --git'
// OUTPUTS ALL FILES WITH CHANGES, SIMPLE HA :)
diff --git a/package-lock.json b/package-lock.json
Expanding off of what @twalberg and @iconoclast had, if you're using cmd for whatever reason, you can use:
FOR /F "usebackq" %x IN (`"git branch | grep '*' | cut -f2 -d' '"`) DO FOR /F "usebackq" %y IN (`"git merge-base %x master"`) DO git diff --name-only %x %y
The following batch file is based on twalberg's answer but will work in Windows:
@ECHO OFF
C: :: <== OR USE A DIFFERENT DRIVE
CD \path\to\where\git\files\are :: <== CHANGE TO THE ACTUAL PATH
SET /p b="Enter full path of an ALREADY MERGED branch to compare with origin/master: "
bash --login -i -c "git diff --name-only %b% $(git merge-base %b1% origin/drop2/master)"
PAUSE
The above assumes that the main branch is origin/master and that git bash was included when Git was installed (and its location is in the path environment). I actually needed to show the actual differences using a configured diff tool (kdiff3) so substituted the following bash command above:
bash --login -i -c "git difftool --dir-diff %b% $(git merge-base %b1% origin/drop2/master)"
© 2022 - 2024 — McMap. All rights reserved.