你如何只推送一些本地 git 提交? [英] how do you push only some of your local git commits?

查看:35
本文介绍了你如何只推送一些本地 git 提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有 5 个本地提交.我只想将其中的 2 个推送到集中式存储库(使用 SVN 风格的工作流).我该怎么做呢?

Suppose I have 5 local commits. I want to push only 2 of them to a centralized repo (using an SVN-style workflow). How do I do this?

这不起作用:

git checkout HEAD~3  #set head to three commits ago
git push #attempt push from that head

这最终会推送所有 5 个本地提交.

That ends up pushing all 5 local commits.

我想我可以执行 git reset 来实际撤消我的提交,然后执行 git stash 和 git push —— 但我已经编写了提交消息并组织了文件,我不想重做它们.

I suppose I could do git reset to actually undo my commits, followed by git stash and then git push -- but I've already got commit messages written and files organized and I don't want to redo them.

我的感觉是传递给 push 或 reset 的某些标志会起作用.

My feeling is that some flag passed to push or reset would work.

如果有帮助,这是我的 git 配置

If it helps, here's my git config

[ramanujan:~/myrepo/.git]$cat config 
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = ssh://server/git/myrepo.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

推荐答案

假设您的提交在 master 分支上,并且您想将它们推送到远程 master 分支:

Assuming your commits are on the master branch and you want to push them to the remote master branch:

$ git push origin master~3:master

如果您使用的是 git-svn:

If you were using git-svn:

$ git svn dcommit master~3

在 git-svn 的情况下,你也可以使用 HEAD~3,因为它期待提交.在直接 git 的情况下,您需要使用分支名称,因为在 refspec 中未正确评估 HEAD.

In the case of git-svn, you could also use HEAD~3, since it is expecting a commit. In the case of straight git, you need to use the branch name because HEAD isn't evaluated properly in the refspec.

您还可以采取更长时间的方法:

You could also take a longer approach of:

$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master

如果您正在养成这种工作流程的习惯,您应该考虑在一个单独的分支中完成您的工作.然后你可以做这样的事情:

If you are making a habit of this type of work flow, you should consider doing your work in a separate branch. Then you could do something like:

$ git checkout master
$ git merge working~3
$ git push origin master:master

请注意,origin master:master"部分对于您的设置可能是可选的.

Note that the "origin master:master" part is probably optional for your setup.

这篇关于你如何只推送一些本地 git 提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆