[git] How can I see what I am about to push with git?

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).

This question is related to git

The answer is


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?.


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.


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


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.


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/


  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

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)


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.


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.


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^^

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.


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


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


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.