Push specific commits in git

Advertisements

I have a branch that is composed of several commits. In my particular case, some fixes to the build system, some useful but not important features, and some important features I am developing.

* extra commit 5 <wip>
* some commit 4 <useful>
* some commit 3
* feature commit 2 <feature>
* feature commit 1
* main <main>

In a branch like this, where I have 5 commits from main, with 3 actual branches (wip, useful and feature), is there any way to specify I want to push to a remote only commit 3 and commit 4 (the subbranch from feature to useful)?

>Solution :

You can push a specific refspec (e.g., a branchname) to the remote repository by specifying its name:

git push origin useful

However, you can’t push a specific commit without all the commits before it in the branch (which I think you may have eluded to). If you want to do something like that, you’ll have to create a new branch containing only these commits. E.g.:

git checkout main # go back to main
git checkout -b newbranch # start a new branch
git cherry-pick <commit 3 hash>..<commit 4 hash>
git push origin newbranch

Leave a ReplyCancel reply