How to know if latest commit to master has been pushed to the remote?

I suggest you run this:

$ git fetch --all
Fetching origin
Fetching upstream

This will fetch the latest data from all remotes.

Then you run:

$ git branch -v
  master       ef762af [ahead 3] added attach methods
* testing      4634e21 added -p flag
  upstream     1234567 [ahead 1, behind 7] updated README.md

This will show which branches you're ahead or behind on.

I posted this because none of the other answers mention fetching the remote data, that step is crucial.


If you have made multiple commits and not sure which one of them have been pushed to remote, try this:

git log origin/<remote-branch>..<local-branch>

Example:

git log origin/master..master

This would list out all commits in your local branch that have not been pushed to the remote branch mentioned.


Do

> git status

If the output is

# On branch master
nothing to commit, working directory clean

Then you have pushed the current commit.

If the output instead begins with

# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)

Then you have a local commit that has not yet been pushed. You see this because the remote branch, origin/master, points to the commit that was last pushed to origin. However, your branch is ahead of 'origin/master', meaning that you have a local commit that has been created after the last pushed commit.

If the commit you are interested in is not the latest, then you can do

> git log --decorate --oneline

to find out if the commit in question is before or after the commit pointed to by origin/master.
If the commit is after (higher up in the log than) origin/master, then it has not been pushed.


A solution to do it programmatically:

git merge-base --is-ancestor HEAD @{u}

You may also want to check if your local directory is clean, e.g. there are no uncommitted file changes:

test -z "$(git status --porcelain)"