How to get the list of files that will be pushed in git push?

98 Views Asked by At

I need some generic command (without specyfing any branches name), that will give me list of files that will be pushed in git push. There might be few commits on branch that will be pushed. So basically I'd like to get all files from local commits. I guess I'd need to compare to last commit that is on remote, but I don't know how to do it

I need it because I want to add pre-push git hook that will lint files that are being pushed.

I tried git diff @{upstream} --name-only and it does what I need, but it only works if I have upstream of branch set. Otherwise I'll get error fatal: no upstream configured for branch <branch-name>

Is there any way to do it in generic way even for branches without upstream set?

2

There are 2 best solutions below

9
M G On BEST ANSWER

I found answer to my question:

git log --format="%H" HEAD --not --remotes

This will give me hashes of all commmits that are on my local branch, but on on remote.

I just need diff between most recent commit and parent of the first commit:

git diff --name-only <first_commit>~ <last_commit> 

And this will output list of files that will be pushed to the remote

26
Dmitry On

you simply want to compare your current branch with origin:

git diff --name-only mybranch origin/mybranch

when you don't have upstream set, somehow you must have the remote branch available locally one way or the other. Otherwise what are you trying to compare ?

You need to understand how git works. Git is a local copy of the remote repository. All operations are done locally and then pushed to remote repository. Compare is the same in that sense, you must have a local copy of the remove branch to be compared with something else.