Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Push specific commits in git

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)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading