ashisrai_
ashisrai_

Reputation: 6568

git command - filtering commits while pushing

I have made multiple commits but don't want all commits to be pushed for now. Is there a way to push only those commits which I want?

Thanks

Upvotes: 4

Views: 162

Answers (2)

sehe
sehe

Reputation: 393934

You have two options,

rebase

Say, you want to include 3 out of five most recent commits:

git rebase -i HEAD~5           # reorder the lines in the text editor, 
                               # leave the 'private' commits at the end
git push origin HEAD~2:master  # push all but the last two

cherry pick

This involves a temporary branch and is a lot more work

git checkout -b temp HEAD~5
git cherrypick <hash1>
git cherrypick <hash2>
git cherrypick <hash3>
git push origin master

Recommendations

  • I recommend against cherrypicking since it is more errorprone and easily makes identical commits seem 'unidentical'
  • use the --cherry-pick or --cherry-mark options with git log to see the actual differences between refs (git log --cherry-pick --oneline master...origin/master)

Upvotes: 5

jamapag
jamapag

Reputation: 9318

This command can help:

$ git push origin <thelonghash>:master

But if you have commits A->B->C->D and do git push origin C:master the commits A,B, and C will be pushed to origin. So if you need push only C you need to use git rebase -i to rearrange the commits so that C is earliest.

Upvotes: 1

Related Questions