我在本地存储库中提交了两次或更多次代码,使用
git commit -m "message 1"
git commit -m "message 2"
git commit -m "message 3"现在我用下面的SHA提交了三次
commit-1 SHA1
commit-2 SHA2
commit-3 SHA3但我只想使用git推送在远程存储库中推送commit-2。
如果我运行git push,那么它将推送所有提交。
我还尝试了以下命令:
git push SHA2但这也推动了所有的提交。
如何在远程仓库中推送这个唯一的commit-2?
发布于 2016-01-18 15:30:41
您需要首先提交您的分支,以便使commit2成为origin/yourBranch之后的第一个提交。
x--x--C1--C2--C3 (B)
|
(origin/B)
git rebase -i C1~
x--x--C2'--C1'--C3' (B)
|
(origin/B)然后,您可以推送该提交。
参见"git - pushing specific commit":
git push <remotename> <commit SHA>:<remotebranchname>
# Example
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master它确实会将所有提交推送到您选择的提交。
但由于您的提交是第一个提交,所以它只推送该提交。
我不建议挑剔,因为它改变了推送的提交的SHA1 :当您最终推送您的完整分支时,您将以重复提交而告终。
发布于 2016-01-18 16:19:50
您也可以使用cherry-pick来执行此操作,但是包含未推送提交的本地分支将从您的远程分支转移。

该怎么做呢?
# cd into your project folder
# create new branch based on the origin branch (latest code in remote)
git checkout -b <new branch> origin/master
# "grab" the commit you wish to use
git cherry-pick <SHA-1>
# now your branch contains the desired commit.
# push it back to your remote.
############################################################################
### Note: you might not be able to push if you try to push it back to ###
### master. To fix it you might need to rename the original master ###
### and your then rename the new barch to master ###
############################################################################https://stackoverflow.com/questions/34849144
复制相似问题