简体   繁体   中英

i need to check for every commit made in the branch, need list the changed, added & deleted files in Git

I need to list all the changed, deleted & added files from a branch in one line. later put added and modified in one file called added_or_modified.txt and dleted into deleted.txt. is there any way to find changed, added & deleted files in one line?

(Since you're not mentionning between which states you want to diff, I've assumed the simplest context, diffing against HEAD. If you try to see diff between other given states, feel free to add it to your question, I'll adapt my answer)

I'd do something along the lines of

git diff --name-status

You'll have letters symbolizing file status before file names :

A added
C changed
D deleted
M modified
R renamed
T changed
U unmerged
X unknown
B broken pairing

See here for details about these states.


And for a deleted files list in a file (same principle for other states), you can do

git diff --diff-filter=D > deleted.txt

Your question title says you need to check each commit. In that case perhaps you mean something like git log . On the other hand, your description of the output sounds like you just want one list of files (or, later, two). That suggests git diff instead.

git log --name-only --format="" <upstream_branch_name>..<branch_name>

or

git diff --name-only <upstream_branch_name>..<branch_name>

For example, if you're checking a branch named my_feature and want to know what's changed in each commit since since it diverged from a develop branch, you could say

git log --name-only --format="" develop..my_feature

Here I've used --format="" so that you just get a list of filenames; if you're generating a list per commit, you might want to do something different with the format option. I mention it because, as written, a file could appear multiple times - even showing up once because commit A created it, and again because commit B deleted it. If that's not desirable, and you just want the net change associated with the branch, use diff instead of log .

Down the line you could change that to

git log --name-status --format="" develop..my_feature

or

git diff --name-status develop..my_feature

and process the results to separate the deletes to their own file. Be aware that with --name-status , git will by default apply rename detection. So you might get some lines like

D    file1
A    file2
M    file3

which is what you would want to see. The one starting with D was deleted, so put it in the delete list; the others were added and modified. But you also might get some lines like

R99  fileX   fileY

meaning that git thinks you've renamed fileX to fileY and made minor modifications in the same commit. If you'd rather git just give you

D    fileX
A    fileY

so that you see simply which paths appear and disappear individually, then add the --no-renames option, as

git diff --name-status --no-renames develop..my_feature

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM