how do you push only some of your local git commits?

GitVersion Control

Git Problem Overview


Suppose I have 5 local commits. I want to push only 2 of them to a centralized repo (using an SVN-style workflow). How do I do this?

This did not work:

git checkout HEAD~3  #set head to three commits ago
git push #attempt push from that head

That ends up pushing all 5 local commits.

I suppose I could do git reset to actually undo my commits, followed by git stash and then git push -- but I've already got commit messages written and files organized and I don't want to redo them.

My feeling is that some flag passed to push or reset would work.

If it helps, here's my git config

[ramanujan:~/myrepo/.git]$cat config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://server/git/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

Git Solutions


Solution 1 - Git

Assuming your commits are on the master branch and you want to push them to the remote master branch:

$ git push origin master~3:master

If you were using git-svn:

$ git svn dcommit master~3

In the case of git-svn, you could also use HEAD~3, since it is expecting a commit. In the case of straight git, you need to use the branch name because HEAD isn't evaluated properly in the refspec.

You could also take a longer approach of:

$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master

If you are making a habit of this type of work flow, you should consider doing your work in a separate branch. Then you could do something like:

$ git checkout master
$ git merge working~3
$ git push origin master:master

Note that the "origin master:master" part is probably optional for your setup.

Solution 2 - Git

Short answer:

git push <latest commit SHA1 until you want commits to be pushed>

Examples:

git push origin fc47b2:master

git push origin HEAD~2:main

Long answer:

Commits are linked together as a chain with a parent/child mechanism. Thus, pushing a commit actually also pushes all parent commits to this commit that where not known to the remote. This is implicitly done when you git push the current commit: all the previous commits are also pushed because this command is equivalent to git push HEAD.

So the question might be rewritten into How to push a specific commit and this specific commit might be HEAD~2, for example.

If the commits you want to push are non-consecutive, simply re-order them with a git rebase -i before the specific push.

Solution 3 - Git

What I do is work on a local branch called "work". This branch contains all the temporary commits (like workarounds or private build options or whatever) that I don't intend to push to the upstream repository. I work away on that branch, then when I want to commit I switch to the master branch, cherry-pick the appropriate commits that I do want to commit, then push master.

After pulling changes from the upstream into my master branch, I git checkout work and git rebase master. That rewrites all my local changes to be at the end of the history.

I'm actually using git svn with this workflow, so my "push" operation involves git svn dcommit. I also use tig which is a nice text mode gui repository viewer, to cherry-pick the appropriate commits to master.

Solution 4 - Git

By default, git-push pushes all branches. When you do this:

 git checkout HEAD~3  #set head to three commits ago
 git push #attempt push from that head

You move to a detached HEAD (you're not on any branch) and then you push all the branches, including the local master (which is still where it was) to the remote master.

The manual solution is:

 git push origin HEAD:master

If you find the default behaviour of pushing all branches confusing (and dangerous!), add this to your ~/.gitconfig:

 [remote.origin]
 	push = HEAD

Then only the branch you're on is pushed. In your example (a detached head), you would have got this error message, rather than accidentally pushing the wrong commits:

 error: unable to push to unqualified destination: HEAD

Solution 5 - Git

  1. Use "git rebase" to reorder your commits, if you want to.

    git rebase -i

This command will display something like this in your editor ( I am using vim )

pick 4791291 commitA
pick a2bdfbd commitB
pick c3d4961 commitC
pick aa1cefc commitD
pick 9781434 commitE

# Rebase ..............
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out



                                               
^G Get Help         ^O WriteOut         ^R Read File        ^Y Prev Page                ^K Cut Text         ^C Cur Pos
^X Exit             ^J Justify          ^W Where Is         ^V Next Page            ^U UnCut Text       ^T To Spell

2) Reorder your your commits according to your choice by simple cut paste. Suppose the new order is

pick 9781434 commitE

pick c3d4961 commitC

pick 4791291 commitA

pick aa1cefc commitD

pick a2bdfbd commitB

Make these changes in your editor and press ctrl+ O (writeOut)

Or you can also use

git rebase -i HEAD~<commitNumber>

You can check the new sequence with

git log

3) Now use

git push <remoteName> <commit SHA>:<remoteBranchName>

If only one branch at remote(origin) and one at local(master), just use

git push <commit SHA>
git push aa1cefc

This will push commitB and commitD.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionramanujanView Question on Stackoverflow
Solution 1 - GitRyan GrahamView Answer on Stackoverflow
Solution 2 - GitTimView Answer on Stackoverflow
Solution 3 - GitGreg HewgillView Answer on Stackoverflow
Solution 4 - GitThomas LeonardView Answer on Stackoverflow
Solution 5 - GitYogesh YadavView Answer on Stackoverflow