4

I am working on a local branch (let us call it bugFix123), I normally use the following command sequences to push changes,

1. git add to add files
2. git commit -m 'description' (there could be multiple sequence of 1 and 2, suppose I have multiple commits for multiple changes, and using one single push in the final)
3. git push origin bugFix123

here is a new question, suppose I commit multiple times for multiple local changes, before I execute command git push origin bugFix123, how to see what are the final files to push? I tried to use command git commit, it seems git commit does not display the right file list to push (if there are multiple commits, before push). The purposeI want to see the final file list to push in multiple commit scenario is because I do not want to push unnecessary files and further fix them (it will send incorrect push list file notifications to others).

Edit 1,

I find git push -n not always work,

git push -n
To git@github.com:foo/goo.git
 ! [rejected]        master -> master (non-fast-forward)
 ! [rejected]        update12_1 -> update12_1 (fetch first)
error: failed to push some refs to 'git@github.com:foo/goo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

regards, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • This question is very similar to http://stackoverflow.com/questions/21101572/git-diff-between-file-in-local-repo-and-origin/21101689#21101689 but probably not an exact duplicate. Adding the link here for future reference. – Code-Apprentice Nov 18 '16 at 20:44

3 Answers3

4

You can use below command to list names of all changes made since last push, however it doesn't show non commit changes

git diff --name-only origin/master..HEAD

--list all commits since last push
git log origin/master..HEAD
abhirathore2006
  • 3,317
  • 1
  • 25
  • 29
  • Thanks abhirathore2006, vote up for your reply. Want to confirm my understanding is correct from your answer. Suppose I am working on local `master` branch and have multiple commits, and then I use `git fetch` to fetch from `remote master` to `origin/master`, then should I execute `git diff --name-only master origin/master`, other than `git diff --name-only origin/master..HEAD`? – Lin Ma Oct 20 '16 at 00:30
  • 1
    @LinMa i don't use git fetch so have not so much idea about it, however check this, http://stackoverflow.com/a/30733500/1785635 this will give you a bit of understanding – abhirathore2006 Oct 20 '16 at 05:49
  • 1
    @LinMa `git fetch` followed by `git diff --name-only master origin/master` will only sure the changes other people have made to `master` on the central repository compared to your personal copy. It will not show any changes you have made on your feature branch. – Code-Apprentice Oct 22 '16 at 11:48
  • @Code-Apprentice, thanks and I tried `git diff --name-only origin/master..HEAD` works good to me. If you have a better solution, let us know – Lin Ma Oct 22 '16 at 18:37
  • @LinMa That is the final solution I came up with that I want to edit in my answer. I will also explain some variations. – Code-Apprentice Oct 23 '16 at 07:33
2
git diff --name-only master

This will list all files which have changed since master. Of course, you can provide any commit-ish instead of master, such as another branch or a SHA1 key.

If you have local changes which are added to the index but not committed, then specify a commit range with

git diff --name-only master bugFix123

Alternatively, you can perform a dry run with

git push -n

See also git: diff between file in local repo and origin.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thanks Code-Apprentice, but your command does not tell what are the files to be pushed. It only tell what are changed. – Lin Ma Oct 18 '16 at 03:00
  • 1
    @LinMa I do not understand. What is the difference between changed files and files to be pushed? Git only pushes files which are changed. – Code-Apprentice Oct 18 '16 at 03:03
  • Hi Code-Apprentice, let me clarify, I think git will push files which are changed, and also in commit list. Your command find all changed files, even if they are not in commit list. I want to find all files which are in commit. I ask this since I have so many commits locally and I want to find a way to get an aggregated changed file list in the many commits which are **really** going to pushed. If I read your advice wrong, please feel free to correct me. – Lin Ma Oct 18 '16 at 03:07
  • 1
    @LinMa Do you have any local changes which have not been committed? – Code-Apprentice Oct 18 '16 at 03:10
  • @LinMa If you provide only one commit to `git diff`, it compares the given commit with the index. Files which are changed locally, but not added, are effectively ignored. – Code-Apprentice Oct 18 '16 at 03:17
  • “Do you have any local changes which have not been committed", yes, this is why I ask the question here. If everything is in commit, it is simple. :) – Lin Ma Oct 18 '16 at 04:34
  • "If you provide only one commit to git diff" -- but my scenario is there are multiple commit (I change some files, commit, then change some other files, then commit again. The whole sequence of commit has 10+ commits and it is among a few days), if compare commit one by one, it is a bit less efficient to me. Is there a way to compare final commit (aggregated effect of all commits) and see what files are going to be pushed? – Lin Ma Oct 18 '16 at 04:36
  • 1
    By "provide one commit", I mean the earliest one in your list of commits. The rest will be included. – Code-Apprentice Oct 18 '16 at 09:41
  • 1
    @LinMa The important thing is that you can do this with `git diff`. Obviously I am not getting the details exactly right for your situation. You need to read [the documentation](https://git-scm.com/docs/git-diff) yourself. – Code-Apprentice Oct 18 '16 at 09:50
  • Thanks Code-Apprentice, apologize if I have not made myself understood. Let us show an example to make my question clear. Suppose you commit 3 times locally for different steps of changes, then you want to push once, how do you know exact all changes in 3 commits which are going to push, is there a git command (I know git diff, but I do not think it resolves my issues)? – Lin Ma Oct 19 '16 at 05:05
  • 1
    @LinMa yes, `git diff` will do what you want – Code-Apprentice Oct 19 '16 at 05:06
  • Thanks Code-Apprentice, `git diff` will compare all local changes (including un-commit local changes) or changes in all commits I have? Could you show an example command? – Lin Ma Oct 19 '16 at 05:11
  • 1
    @LinMa `git diff` can show the changes between any two commits in your entire history. I already gave an example in my answer. If you need more details, read the docs. – Code-Apprentice Oct 19 '16 at 05:17
  • Thanks Code-Apprentice, take time today to read your example and document, want to confirm my understanding is correct. Suppose I am working on local `master` branch and have multiple commits, and then I use `git fetch` to fetch from `remote master` to `origin/master`, then if I execute `git diff --name-only master origin/master`, it will compare all commits' aggregated results on **local** `master`, to **local** `origin/master`, correct? – Lin Ma Oct 20 '16 at 00:28
  • 1
    @LinMa My original answer was incorrect. It compares the local writing copy to `master`. However, with the right arguments, `git diff` will do what you want. When I have time, I will write a more thorough and complete answer. – Code-Apprentice Oct 22 '16 at 11:52
  • nice! Appreciate for all the help. – Lin Ma Oct 22 '16 at 18:36
2

If you've already pushed something to bugFix123 and you want to compare your local branch bugFix123 with its remote version (for instance, origin) you can do the following:

git diff --numstat origin/bugFix123 bugFix123 
ghashi
  • 1,497
  • 1
  • 13
  • 17
  • Thanks ghashi, I have not pushed anything to `bugFix123` yet. I have multiple commit locally and just want to know what will be the final list to be pushed from the multiple commits, any ideas how to get such information? – Lin Ma Oct 18 '16 at 03:02
  • 1
    Try `git diff --numstat origin/master bugFix123 ` or `git diff --numstat develop bugFix123 ` (if your base branch is `develop`). Is this what you're looking for? – ghashi Oct 18 '16 at 04:02
  • Tried command `git diff --numstat origin/master bugFix123`, it shows a long diff list, it is obvious not showing files already in commit file lists (far more than committed file numbers). – Lin Ma Oct 18 '16 at 04:40
  • My scenario is there are multiple commit (I change some files, commit, then change some other files, then commit again. The whole sequence of commit has 10+ commits and it is among a few days), if compare commit one by one, it is a bit less efficient to me. Is there a way to compare final commit (aggregated effect of all commits) and see what files are going to be pushed? – Lin Ma Oct 18 '16 at 04:41
  • @LinMa Has anyone else pushed to `origin/master`? – Code-Apprentice Oct 18 '16 at 09:47
  • 1
    @LinMa alternatively, you can do `git push -n`. – Code-Apprentice Oct 18 '16 at 09:54
  • 2
    If you have the hash of your first commit you can do this: `git diff --numstat a5b22e6 bugFix123`, where `a5b22e6` should be replaced with the hash of the first commit – ghashi Oct 19 '16 at 01:19
  • @ghashi, apologize if I have not made myself understood. Let us show an example to make my question clear. Suppose you commit 3 times locally for different steps of changes, then you want to push once, how do you know exact all changes in 3 commits which are going to push, is there a git command (I think my question should have nothing to do with first commit, correct)? – Lin Ma Oct 19 '16 at 05:07
  • Thanks Code-Apprentice for the advice, (1) no one has pushed to `origin/master`, I just want to make sure I push the right thing from multiple commits, since I think when push, it will push multiple commits' aggregated results, correct? (2) `git push -n` will actually do the push, I just want to know what will be pushed (I want to check file to be push list, so that I do not push the wrong stuff), I do not need a real push. – Lin Ma Oct 19 '16 at 05:10
  • 1
    @LinMa From the docs: "-n Do everything except actually send the updates." – Code-Apprentice Oct 19 '16 at 05:25
  • 1
    Hummm, I think the next answer may not be exactly what you want but maybe it will help. Try this: `git log --pretty=format:%C(yellow)%h%Cred%d\%Creset%s%Cblue\[%cn] --decorate --numstat`. It will print nicely your last commits and for each commit it will show the modified files. If I understood, you want all of the changes together (not by commit). In this case, the others answers should work. I did some search and found this: http://stackoverflow.com/questions/2016901/viewing-unpushed-git-commits . Check if it helps – ghashi Oct 19 '16 at 05:33
  • @Code-Apprentice, I find `git push -n` is not always ideal and I post error message in edit 1 section of my origin post, if you have any good ideas, it will be great. – Lin Ma Oct 20 '16 at 00:36
  • @ghashi, your command has issues, when I execute `git log --pretty=format:%C(yellow)%h%Cred%d\%Creset%s%Cblue\[%cn] --decorate --numstat`, there is error message like `-bash: syntax error near unexpected token `('`. Any thoughts? – Lin Ma Oct 20 '16 at 00:39
  • 1
    What if you try just `git log --decorate --numstat` or `git log --numstat`? – ghashi Oct 20 '16 at 13:09
  • 1
    @ghashi You should edit your question to include the information in your previous comment. – Code-Apprentice Oct 20 '16 at 15:54