git 如何将特定提交推送到远程而不是以前的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3230074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I push a specific commit to a remote, and not previous commits?
提问by Robert23
I have made several commits on different files, but so far I would like to push to my remote repository only a specific commit.
我已经对不同的文件进行了多次提交,但到目前为止,我只想将特定的提交推送到我的远程存储库。
Is that possible?
那可能吗?
回答by Geoff Reedy
To push up througha given commit, you can write:
为了推动通过了一个指定的提交,你可以这样写:
git push <remotename> <commit SHA>:<remotebranchname>
provided <remotebranchname>
already exists on the remote. (If it doesn't, you can use git push <remotename> <commit SHA>:refs/heads/<remotebranchname>
to autocreate it.)
前提<remotebranchname>
是遥控器上已经存在。(如果没有,您可以使用git push <remotename> <commit SHA>:refs/heads/<remotebranchname>
自动创建它。)
If you want to push a commit withoutpushing previous commits, you should first use git rebase -i
to re-order the commits.
如果你想推送一个提交而不推送以前的提交,你应该首先使用git rebase -i
重新排序提交。
回答by Samuel
The other answers are lacking on the reordering descriptions.
重新排序描述中缺少其他答案。
git push <remotename> <commit SHA>:<remotebranchname>
will push a single commit, but that commit has to be the OLDEST of your local, non-pushed, commits, not to be confused with the top, first, or tip commit, which are all ambiguous descriptions in my opinion. The commit needs to the oldest of your commits, i.e. the furthest from your most recent commit. If it's not the oldest commit then all commits from your oldest, local, non-pushed SHA to the SHA specified will be pushed. To reorder the commits use:
将推送单个提交,但该提交必须是本地非推送提交中最旧的提交,不要与顶部提交、第一个提交或提示提交混淆,在我看来,这些都是模棱两可的描述。提交需要最旧的提交,即离最近提交最远的提交。如果它不是最旧的提交,那么所有从最旧的、本地的、未推送的 SHA 到指定的 SHA 的提交都将被推送。要重新排序提交,请使用:
git rebase -i HEAD~xxx
After reordering the commit you can safely push it to the remote repository.
重新排序提交后,您可以安全地将其推送到远程存储库。
To summarize, I used
总而言之,我用
git rebase -i HEAD~<number of commits to SHA>
git push origin <post-rebase SHA>:master
to push a single commit to my remote master branch.
将单个提交推送到我的远程主分支。
References:
参考:
- http://blog.dennisrobinson.name/push-only-one-commit-with-git/
- http://blog.dennisrobinson.name/reorder-commits-with-git/
- http://blog.dennisrobinson.name/push-only-one-commit-with-git/
- http://blog.dennisrobinson.name/reorder-commits-with-git/
See also:
也可以看看:
回答by Walter Mundt
I'd suggest using git rebase -i
; move the commit you want to push to the top of the commits you've made. Then use git log
to get the SHA of the rebased commit, check it out, and push it. The rebase will have ensures that all your other commits are now children of the one you pushed, so future pushes will work fine too.
我建议使用git rebase -i
; 将您要推送的提交移至您所做的提交的顶部。然后用于git log
获取重新提交的 SHA,检查它并推送它。变基将确保您所有其他提交现在都是您推送的提交的子项,因此未来的推送也将正常工作。
回答by David
Cherry-pick works best compared to all other methods while pushing a specific commit.
在推送特定提交时,与所有其他方法相比,Cherry-pick 效果最好。
The way to do that is:
这样做的方法是:
Create a new branch -
创建一个新分支 -
git branch <new-branch>
Update your new-branch with your origin branch -
使用您的原始分支更新您的新分支 -
git fetch
git rebase
These actions will make sure that you exactly have the same stuff as your origin has.
这些操作将确保您拥有与原点完全相同的东西。
Cherry-pick the sha id
that you want to do push -
Cherry-picksha id
你想要推送的那个 -
git cherry-pick <sha id of the commit>
You can get the sha id
by running
你可以sha id
通过运行获得
git log
Push it to your origin -
把它推到你的原点——
git push
Run gitk
to see that everything looks the same way you wanted.
运行gitk
以查看一切看起来是否与您想要的一样。
回答by Josh K
I believe you would have to "git revert" back to that commit and then push it. Or you could cherry-pick
a commit into a new branch, and push that to the branch on the remote repository. Something like:
我相信你必须“git revert”回到那个提交,然后推送它。或者您可以cherry-pick
提交到一个新分支,然后将其推送到远程存储库上的分支。就像是:
git branch onecommit
git checkout onecommit
git cherry-pick 7300a6130d9447e18a931e898b64eefedea19544 # From the other branch
git push origin {branch}
回答by NunoSempere
You could also, in another directory:
您也可以在另一个目录中:
- git clone [your repository]
- Overwrite the .git directory in your original repository with the .git directory of the repository you just cloned right now.
- git add and git commit your original
- git clone [你的仓库]
- 用您现在刚刚克隆的存储库的 .git 目录覆盖原始存储库中的 .git 目录。
- git add 和 git commit 你的原始文件