705

Is there a way to see what would be pushed if I did a git push command?

What I'm picturing is something like the "Files Changed" tab of Github's "pull request" feature. When I issue a pull request, I can look and see what will be pulled in if they accept my pull request: github example of aggregate changes

Command line is OK, but I'd prefer some sort of GUI (like the screenshot above).

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
cmcculloh
  • 47,596
  • 40
  • 105
  • 130
  • Related: http://stackoverflow.com/questions/3080509/git-list-commits-not-pushed-to-the-origin-yet – nos Nov 06 '12 at 21:49
  • 1
    Many related questions/answers; here's another: http://stackoverflow.com/questions/2176278/preview-a-git-push/13732520#13732520 – michael Jan 17 '13 at 21:49

14 Answers14

795

For a list of files to be pushed, run:

git diff --stat --cached [remote/branch]

example:

git diff --stat --cached origin/master

For the code diff of the files to be pushed, run:

git diff [remote repo/branch]

To see full file paths of the files that will change, run:

git diff --numstat [remote repo/branch]

If you want to see these diffs in a GUI, you will need to configure git for that. See How do I view 'git diff' output with a visual diff program?.

Community
  • 1
  • 1
Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • 9
    Both variations of git diff --cached without a commitish given will only display the differences w/regards to HEAD. I think you meant git diff [--stat] --cached origin/master, assuming the origin's main branch is master – mfontani Sep 03 '10 at 15:18
  • @mfontani, yes you're right. Now I realize that what I actually answered to, was a way to see what will be committed, not pushed. So, yes, a path should be given. – Ionuț G. Stan Sep 03 '10 at 15:27
  • 61
    This isn't exactly what you want. You should diff `HEAD`, not the index, against origin (`git diff origin/master HEAD`). Diffing the index will give the same results if and only if you have no changes staged for commit. If you have changes staged, they'll be included in the diff, but clearly won't be pushed since they haven't been committed yet. – Cascabel Sep 03 '10 at 19:03
  • 8
    This answer needs to be edited to include the information in the comment from @Jefromi `git diff --stat origin/master HEAD` is the correct answer. – Tony Topper Jul 02 '13 at 17:00
  • 2
    @mfontani has the right answer for me. I want to check what will get pushed from what I've already committed, not what will get committed if I commit everything I've changed. `git diff --cached [remote/branch]` does the trick :) – poshaughnessy Nov 25 '13 at 18:53
  • @thekashyap I think you've missed the `--stat` option in `git diff`. – Ionuț G. Stan Apr 19 '14 at 14:41
  • @stan, `git diff --stat origin/master` shows diff. If you do `git push` it is NOT guaranteed that list of files produced by earlier `git diff` is what will be `push`ed. Answer completely ignores the concept of commit. – Kashyap Apr 21 '14 at 14:59
  • @thekashyap ah, you mean what Jefromi has already said with diffing against HEAD a few comments above. Yes, that's true. – Ionuț G. Stan Apr 22 '14 at 07:22
  • i test all variations of `diff` mentioned here, and none produced any output. but `push --dry-run` do show `06b7b81..ffa79a8 master -> master` – gcb Aug 18 '14 at 19:15
  • How can this answer be accepted? It does not even do what its description says, nor does it do what was asked. `git diff` without a _range_ always diffs your current _working directory_ to something, with _cached_ it diffs at least what will be committed next time, but never what has to be pushed. `push --dry-run` from Brian is the only one usable answer so far, another would be `git diff --stat HEAD origin/master` – Daniel Alder Sep 07 '14 at 11:07
  • git diff --cached HEAD (all changes file by file) and git diff --stat HEAD (more abbreviated - what it looks like when you do git pull with the green pluses and red minuses and the short summary at the bottom) worked the best for me to view what would be committed. (Also, `git diff --cached` I believe implies `HEAD` at the end - at least it works for me. You can also do `git diff --cached file`. `git diff --stat`, however, does **not** seem to work.) – dylnmc Oct 27 '14 at 14:21
  • 2
    instead of typing origin/master, you can use @{u} eg git diff @{u} or git diff HEAD @{u} Note that this only works if you have a upstream tracking branch already created and linked. I find it useful when I've got lots of branches and lots of remotes so that I don't have to think about what is linked where. – Damien Sawyer Dec 18 '14 at 10:38
  • It feels like idiot savant to remember that command when it should be `git viewfiles' or something that you can remember – Niklas Rosencrantz Jun 23 '16 at 14:41
  • You can use `git status` to determine the value of `[remote/branch]` – mvermand Jun 15 '17 at 09:53
  • This approach does not connect to the remote repo (simply compares with `origin/branch`), so it may not reflect all commits that have been pushed to remote. Always run `git pull origin` first to make sure `origin/branch` is up-to-date. (For the sake of explanation, if the same commits were pushed to remote from another branch but not pulled down to your working repo, it would still show them, but they wouldn't need to be pushed. Or worse, if there are conflicting changes in the remote repo, this wouldn't give you any warning about them.) – Sean the Bean Oct 24 '17 at 14:20
  • **The `origin/` part is important!** `git diff --stat --cached main` does not work; you have to write `git diff --stat --cached origin/main`. – Julia Dec 10 '22 at 10:51
218

There is always dry-run:

git push --dry-run

It will do everything except for the actually sending of the data.

If you want a more graphical view you have a bunch of options.

Tig and the gitk script that come with git both display the current branch of your local copy and the branch of the remote or origin.

alt text

So any commits you make that are after the origin are the commits that will be pushed.

Open gitk from shell while in the branch you want to push by typing gitk&, then to see the difference between what is on the remote and what you are about to push to the remote, select your local unpushed commit and right-click on the remote and choose "Diff this -> selected": alt text

cmcculloh
  • 47,596
  • 40
  • 105
  • 130
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77
  • 23
    git push --dry-run doesn't show me anything like what I'm hoping to see. gitk is closer, but it doesn't show me the aggregate total of all changes that will be pushed. So if I'm pushing 6 commits to remote, I want to see the sum total of what will be pushed. I don't care what each commit has individually because one commit could be completely negated by the next. – cmcculloh Sep 03 '10 at 15:45
  • 1
    If you need a diff of what will be pushed, do `git diff --stat HEAD origin/master` or `git diff HEAD origin/master`. Important is the _HEAD_, otherwise you include uncommited changes in your diff – Daniel Alder Sep 07 '14 at 11:09
  • Is there some reason that git doesn't automatically do a dry-run before it tries to push data to a remote? If you are pushing a large amount of data, it uploads all of the data and then shows any errors after. You've just wasted time and bandwidth uploading data that you now have to re-upload. This seems like strange behavior. – zeusstl Jan 19 '15 at 07:04
  • 1
    The problem with `git push --dry-run` is that it still requires write permission at the remote. So if you have a clone without permission to push upstream, but you'd like to see what your local unpushed changes are, `--dry-run` won't do it. – Ed Avis Apr 22 '15 at 15:07
  • This is a good solution, but it should be noted that it does require connecting to the remote (including entering the SSH key if applicable). If you just want to see the difference between `branch` and `origin/branch`, I would go with Ionuț G. Stan's answer, since it doesn't actually require connecting to the remote repo. This can be good or bad, depending what you need. – Sean the Bean Oct 24 '17 at 14:10
  • Use `$ git push -v --dry-run`, if you're here wondering to check WHERE changes are being pushed to without modify anything. It is useful when 2 or more remote repositories are configured (i.e. origin and origin2), which you can check with `$ git remote -v`. Also `$ git branch -vv` shows current branch upstreams. – Mauricio Jul 08 '21 at 15:32
205

To simply list the commits waiting to be pushed: (this is the one you will remember)

git cherry -v

Show the commit subjects next to the SHA1s.

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
  • 5
    One interesting thing is that you can specify a remote to compare like: `git cherry -v heroku/remote` in case you keep your code in more than one remote. – fagiani Mar 30 '16 at 15:32
  • What's the best complementary command that allows us to actually correlate the differences these commits contained? – Hassan Baig Nov 24 '18 at 13:08
  • This command is nice, but I'd like more detail, like the names of the files to be changed during the push. – Mr. Lance E Sloan Aug 07 '19 at 13:22
  • I found this command `git diff --stat --cached origin/branchname` nicely lists the changed files. – UweBaemayr Sep 02 '20 at 18:42
  • 1
    Interesting, why when I use `git cherry -v` the command just ends and I get the Terminal again as if nothing has happened? Any clues? – Beauregard Lionett Aug 10 '21 at 14:38
22

You probably want to run git difftool origin/master.... that should show the unified diff of what is on your current branch that is not on the origin/master branch yet and display it in the graphical diff tool of your choice. To be most up-to-date, run git fetch first.

Scott Chacon
  • 2,756
  • 19
  • 14
  • 2
    `git difftool -d origin/master`, I have my difftool set to meld so it works great, thx! – Aquarius Power Aug 23 '14 at 06:49
  • `git difftool -d` is way better, thanks Aquarius Power. The only problem is that it doesn't detect moved files and shows them as deleted and added instead. – EM0 Aug 19 '16 at 14:09
11

Try git diff origin/master..master (assuming that origin/master is your upstream). Unlike git push --dry-run, this still works even if you don't have write permission to the upstream.

Ed Avis
  • 1,350
  • 17
  • 36
10
  1. If you have write permissions on remote
git push --dry-run
  1. If you do not have write permissions on remote
git diff --stat HEAD remote/branch
IliassA
  • 95
  • 2
  • 8
vkg
  • 351
  • 4
  • 10
9

One way to compare your local version before pushing it on the remote repo (kind of push in dry-run):

Use TortoiseGit:
Right click on the root folder project > TortoiseGit > Diff with previous version >
for Version 2 choose refs/remotes/origin/master

Nakilon
  • 34,866
  • 14
  • 107
  • 142
user1921207
  • 99
  • 1
  • 1
9

You can list the commits by:

git cherry -v

And then compare with the following command where the number of ^ equals to the number of commits (in the example its 2 commits):

git diff HEAD^^
DavidS
  • 2,160
  • 1
  • 19
  • 22
  • It is simply good one to see the list of latest commits. This will not give name of the file(s). Hence as some one answered using dry run options and patch options is better. git diff --stat --patch origin master git push --dry-run – Sree Rama Dec 28 '17 at 10:51
6

To see which files are changed and view the actual code changes compared to the master branch you could use:

git diff --stat --patch origin master

NOTE: If you happen to use any of the Intellij IDEs then you can right-click your top-level project, select Git > Compare with branch > and pick the origin you want e.g. origin/master. In the file tree that will appear you can double-click the files to see a visual diff. Unlike the command-line option above you can edit your local versions from the diff window.

ccpizza
  • 28,968
  • 18
  • 162
  • 169
6

Use git gui, there you can see a list of what changed in your actual commit. You can also use gitk wich provides an easy interface for reflogs. Just compare between remotes/... and master to see, what will be pushed. It provides an interface similar to your screenshot.

Both programs are included in git.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • 1
    I don't see any place in git gui that shows everything that has been committed but not pushed. – cmcculloh Sep 03 '10 at 16:11
  • In git gui you can see, what's modified but not commited. In gik (Via `gitk --all` you get a complete list of all comments. Now you can compare the actual state of your dev-branch with your remote to push. – fuz Sep 04 '10 at 06:01
3

After git commit -m "{your commit message}", you will get a commit hash before the push. So you can see what you are about to push with git by running the following command:

git diff origin/{your_branch_name} commit hash

e.g: git diff origin/master c0e06d2

mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
Chandra
  • 31
  • 1
  • Thanks for answering but I think this effort was not required as the question is pretty old and highly voted. You are better off spending efforts on different questions. – Pbd Apr 24 '20 at 11:23
1

Just to add my two cents... I wanted to implement this when running jobs in a gitlab pipeline on a gitlab runner. The best way to do this was to use this script:

git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA

Also in my case I wanted to filter files by extension, to achieve this I used:

git diff --name-only $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA '*.py'

After that you can for example forward this list somewhere else, a linter perhaps ;)

Hope this will help someone.

1

If you are using Mac OS X, I would recommend you get Tower, it's a wonderful program that has made dealing with Git a pleasure for me. I now longer have to remember terminal commands and it offers a great GUI to view, track and solve differences in files.

And no, I'm not affiliated with them, I just use their software and really like it.

http://www.git-tower.com/

tuomassalo
  • 8,717
  • 6
  • 48
  • 50
kakubei
  • 5,321
  • 4
  • 44
  • 66
0

Just wanted to add for PyCharm users: You can right-click on the file, -> Git -> Compare with branch

And then you can choose master (or any other)