Quick git tip: Push only some commits to your upstream repo

git status says:

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.

You want to git push only one of those commits to the public repo. Here’s how:

First use git log to get the commit hash of the commit you want to push. Then:

$ git push origin <thelonghash>:master

(Thanks to ajax).


You can’t push arbitrary commits this way. See Stefan’s comment below.


Filed under Uncategorized

5 responses to “Quick git tip: Push only some commits to your upstream repo

  1. Great tip! I love learning something(s) new every day — one of the best things about being involved with you genius types in Fedora, if you ask me.

  2. Stefan Näwe

    One should say that only the commits _up to_ the given one
    are pushed to the other repo.
    It’s not like pushing only one commit, e.g.
    if you have commits A->B->C->D and do ‘git push otherrepo C:master’ the commits A,B, and C will be pushed to otherrepo
    (if they aren’t already there).


  3. rich

    Stefan, this is a very good point.

    The solution would be to use git rebase -i to
    rearrange the commits so that ‘C’ is earliest.

  4. Max

    Exactly what I needed right now! Thanks!

  5. I have commits(newest —> oldest) A<– B<– C<— D<– E<– F . Commits D, E and F were already pushed in the previous push I did, but they still show up when I try pushing A, B and C. This happens every time I'm doing a push.
    I usually do 'cherry-pick' after doing a PR, but I'm tired of seeing already merged commits showing up every time. Can someone help me here

    Plus, I don't know how to remove those old commits (D, E and F) before doing a PR.

    So, I want to see only UNMERGED and UNPUSHED commits anytime I push.
    Thanks for your help.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.