47

I have merged a branch in to master and now I can see that in my git log

Some time has passed and now I want to know whether I previously also pushed master (with that commit) to the remote. How can I tell if it has been pushed?

I can think of a few workaround such as recloning the repository elsewhere, or resetting and checking and then re-merging but I feel that there's probably a somewhat simpler answer.

fyi this is different from How can I know in git if a branch has been already merged into master? as I know it has been merged, just don't know about the remote push.

Community
  • 1
  • 1
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

6 Answers6

52

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.

Klas Mellbourn
  • 42,571
  • 24
  • 140
  • 158
  • 2
    It doesn't always work. E.g. it works for me in random github repository. However when I use `git status` in our local project, the line simply absent, i.e. after `On branch master` immediately follows the `Untracked files` line. – Hi-Angel Jul 17 '18 at 07:41
  • 1
    @Hi-Angel is this a completely local project that has not been pushed to a remote? That is, a project with no corresponding remote repository? In such a project `git status` would never mention any `origin`. – Klas Mellbourn Jul 17 '18 at 10:48
  • No, I mean "local" in the sense it's not a public project. It does have a "remote origin". – Hi-Angel Jul 17 '18 at 11:13
  • 3
    So, occasionally, I found the answer on IRC: I was missing the `git branch --set-upstream-to origin/master master` command. When using `git clone`, git usually sets it automatically, but in my case it was locally created code and then added "origin", so apparently I had to call it manually. Now I see the difference with remote too. – Hi-Angel Aug 20 '18 at 09:51
  • This is not true if you have never pushed your branch to a remote: it will still say `nothing to commit, working directory clean`. – Connor Low Feb 17 '22 at 18:24
  • @ConnorLow but that is not what the question was about. OP was trying to remember if he "previously also pushed master", this confusion would hardly occur if he _never_ pushed to a remote at all. – Klas Mellbourn Feb 19 '22 at 20:00
25

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.

somesh
  • 2,509
  • 3
  • 16
  • 24
  • 2
    I found this more reliable that the selected answer. For some reason on my system, ```git status``` does not always report if local branch is ahead. – Obromios Aug 16 '19 at 06:38
19

Programmatically (for example, in a script):

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)"
VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
  • 1
    Good one – perfect for my use (+1) with a minor adjustment: Note that if you have no remote named "upstream", `@{u}` will fail. In such a case, instead use `git rev-parse refs/remotes//` – e.g. `git rev-parse refs/remotes/origin/master` if your remote is named "origin" and you want to check its "master" branch. – Izzy Jun 07 '18 at 12:38
  • Ad your last example - how is that gonna make your script exit? You're making the exit 1 command in a subshell. Instead, you can use `{}` grouping, which uses current shell: `test your_condition || { echo "ERROR: you have local changes"; exit 1; }` -- the whitespace and semicolons inside the `{}` are significant. – dwelle Jul 10 '19 at 11:57
  • @dwelle I'm always using `-euET` flags of bash. – VasiliNovikov Jul 11 '19 at 09:44
  • 1
    The comparison of `@{u}` and `HEAD` only tells you if your HEAD happens to be the same as the HEAD at origin. What if origin's HEAD is now 10 commits ahead of your HEAD? Then your test would fail, even though your HEAD has been pushed already. – tanager Oct 31 '19 at 21:28
  • @tanager I think you're right. I changed the code the check if the commit is ancestor instead of equality. Thanks for the remark! – VasiliNovikov Nov 04 '19 at 16:46
  • 1
    I think you need to swap your arguments around. `--is-ancestor A B` returns 0 (success) if a is an ancestor of B. This will pretty much always be true when A is the upstream branch and B is your local branch. – Brecht Machiels Dec 09 '21 at 13:21
  • @BrechtMachiels I think you're right, thanks for writing! Edited the answer. – VasiliNovikov Dec 11 '21 at 07:42
6

you can use git log --graph --all --decorate, it will show where each ref is located (HEAD, master, origin/master etc.)

Vlad Nikitin
  • 1,929
  • 15
  • 17
  • This is my favorite answer because it gives the information EXPLICITLY: "commit XYZ (origin/master)". The commands in the other answers merely imply it by showing no information about unpushed commits. Thanks! – Travis Wilson Feb 02 '18 at 19:32
6

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.

asd
  • 61
  • 1
  • 1
3

Try this. git branch -r --contains <sha1>

For a commit in my repository I can see it exists on the remote develop branch

git branch  -r --contains 7914e54ea7e30c7f446e791df66bd3a5805c978a
origin/develop
crea1
  • 11,077
  • 3
  • 36
  • 46