Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

git: push a single commit

Say I made several commits and wish to cherry pick which ones I push to the remote repository. How can I do that (in ascii: C1->C2->C3->C4 and I want to push C2 and C4). Will reordering with rebase, resetting, pushing and then resetting work? (C1->C2->C3->C4 => C2->C4->C1->C3 => reset C4 => push => reset C3). Is there a nicer way?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may be looking for:

git push origin $commit:$branch

Credit: http://blog.dennisrobinson.name/push-only-one-commit-with-git/

Explanatory notes:

  • Pushing a commit pushes all commits before it (as Amber said). The key is to reorder your commits first (git rebase -i), so they are in the order you want to push them.
  • $commit doesn't have to be a sha1. For example, "HEAD~N" would push everything before the last N commits.
  • $branch (typically "master" or "main") is the branch you push to – the remote branch. It does not have to be the same as a local branch.
  • The suggested branch + cherry-pick method (suggested by midtiby) works too, but for reordering purposes (such as getting the prework in first), this avoids creating throwaway branches.

If the branch does not exist remotely, and you want to create it, it must be prefixed with refs/heads/ (to disambiguate branch from tag):

git push origin $commit:refs/heads/$new_branch

Pro tip: git-revise is a similar but better tool than git rebase (for local commits specifically). For my purposes, as a daily git user, I think it should be obligatory if you use git revise -i a lot (which is of course quite necessary to produce high quality commits).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...