Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

How do you push certain commits?

(If it doesn't, you can use git push <remotename> <commit SHA>:refs/heads/<remotebranchname> to autocreate it.) If you want to push a commit without pushing previous commits, you should first use git rebase -i to re-order the commits.

How do I pull only certain commits?

Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want. "Cherry pick" the commits you want into this branch. Run this command: git cherry-pick super-long-hash-here . That will pull just this commit into your current branch.

Does git push push all local commits?

No, git push only pushes commits from current local branch to remote branch that you specified in command.


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.


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.


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.


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