How to use GIT to list the changed files in the branch

Asked

Viewed 398 times

2

How to view the list of all the changed files in one branch without having to inform the commit X until commit Y.

I was using the following command below to list the commit’s and get the Hash of commit’s:

    git log origin/master..HEAD

The return is something like

commit 234567893fcc38567c634e810aa904c7497d7fdc (HEAD -> feature/task/123, origin/feature/task/123)
Author: Nome <[email protected]>
Date:   Wed Aug 19 12:37:46 2020 -0300

    Alterada a função x

commit 022e45f07f259b4e8f49679e6a0531c404179cb9
Author: Nome <[email protected]>
Date:   Wed Aug 19 12:35:47 2020 -0300

    Alterada a variável y

commit 75094ffa564f1b4a3d19ffaebf2afc358585fc81
Author: Nome <[email protected]>
Date:   Wed Aug 19 12:25:58 2020 -0300

    Adicionado regra

commit 12345678298af92e713b90b9335bcb813eda85fd
Author: Nome <[email protected]>
Date:   Wed Aug 19 12:21:47 2020 -0300

    Correção de bug

And the command below to list the files of commit A up to the commit C taking the first 8 commit hash characters.

   git show --pretty="format:" --name-only  12345678..23456789 | sort | unique

The return is the list below:

    arquivo1.js
    arquivo2.css
    arquivo3.html

My question is how do I return in a simpler way all the changed files on branch without having to inform the Hash of commit initial and final?

I also took a look at the documentation: Commit Hystory

2 answers

1

You can use the command git diff with the option --name-only to list only the name of the files. See documentation.

This command can be used with one or two parameters. With one it will compare the commit pointed to the current state of your directory. Another option is to pass two parameters and it will compare the two directly.

For example, suppose you’re in the branch you’re developing, you can use:

git diff --name-only master

Keep in mind that if after creating the branch commits have been made in Master, these will also be listed as a difference. If this is not a problem (e.g. you know that there have been no commits in master since then) this simpler command is enough.

To get only the ones from the strictly speaking branch you can use the most complete command and use the first commit from the branch as a reference:

git diff --name-only <BranchDevelop> <CommitOriginalDaBranch>

To get the first commit from the branch you can use Command merge-based who finds the best common ancestor between two commits:

git merge-base BranchDevelop Master
> 5e3299f35dc05c8ab69cb0e62c3fbecb161afb87
git diff --name-only BranchDevelop 5e3299f35dc05c8ab69cb0e62c3fbecb161afb87

Or, if you prefer you can use the functions of your shell to directly catch the return of the first parameter:

git diff --name-only BranchDevelop $(git merge-base BranchDevelop Master)

Note: I believe this syntax of $() may not be accepted by all bash/shell, in which case the equivalent with back-tick `git merge-base BranchDevelop Master`

0

To list the files without having to inform the hash use:

  git log feature/FI/85683 --not master --name-status --pretty=format:"%x09"

To display the list without repeating the name of the files use | sort | unique :

  git log feature/FI/85683 --not master --name-status --pretty=format:"%x09" | sort | unique

Browser other questions tagged

You are not signed in. Login or sign up in order to post.