我如何将特定提交推送到远程,而无需提交以前的提交?

我在本地存储库中提交了2次或更多次使用代码

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 push仅在远程仓库中推送commit-2
如果我运行git push,那么它会推送所有提交。
我也尝试了以下命令:

git push SHA2

但是这也推动了所有提交。

如何在远程仓库中推送这个唯一的commit-2


你需要首先对你的分支进行git rebase -i ,以使commit2在origin/yourBranch之后第一次提交。

 x--x--C1--C2--C3 (B)
    |
  (origin/B)

git rebase -i C1~

 x--x--C2'--C1'--C3' (B)
    |
  (origin/B)

然后你可以推送该提交。

请参阅“git - 推送特定提交”:

git push <remotename> <commit SHA>:<remotebranchname>
# Example
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master

它确实推动所有提交,包括您选择的提交。
但是由于你的提交是第一个提交,它只会推送该提交。


我不推荐挑选樱桃,因为它会改变推送提交的SHA1:当您最终推送完整分支时,最终会发生重复提交。


您也可以使用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                   ###
############################################################################
链接地址: http://www.djcxy.com/p/49805.html

上一篇: How can I push specific commit to a remote, without the previous commits?

下一篇: When should I use git pull