ram*_*jan 140 git version-control
假设我有5个本地提交.我想只将其中的2个推送到集中式仓库(使用SVN风格的工作流程).我该怎么做呢?
这不起作用:
git checkout HEAD~3 #set head to three commits ago
git push #attempt push from that head
Run Code Online (Sandbox Code Playgroud)
最终推动所有5个本地提交.
我想我可以执行git reset来实际撤消我的提交,然后是git stash然后是git push - 但我已经编写了提交消息并组织了文件,我不想重做它们.
我的感觉是,推送或重置的一些标志会起作用.
如果它有帮助,这是我的git配置
[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
Run Code Online (Sandbox Code Playgroud)
Rya*_*ham 177
假设您的提交位于主分支上,并且您希望将它们推送到远程主分支:
$ git push origin master~3:master
Run Code Online (Sandbox Code Playgroud)
如果您使用的是git-svn:
$ git svn dcommit master~3
Run Code Online (Sandbox Code Playgroud)
在git-svn的情况下,你也可以使用HEAD~3,因为它期望提交.在直接git的情况下,您需要使用分支名称,因为在refspec中未正确评估HEAD.
您还可以采取更长的方法:
$ git checkout -b tocommit HEAD~3
$ git push origin tocommit:master
Run Code Online (Sandbox Code Playgroud)
如果你养成这种工作流程的习惯,你应该考虑在一个单独的分支中完成你的工作.然后你可以这样做:
$ git checkout master
$ git merge working~3
$ git push origin master:master
Run Code Online (Sandbox Code Playgroud)
请注意,"origin master:master"部分可能是您的设置的可选项.
Tho*_*ard 14
默认情况下,git-push会推送所有分支.当你这样做:
git checkout HEAD~3 #set head to three commits ago
git push #attempt push from that head
Run Code Online (Sandbox Code Playgroud)
您移动到分离的HEAD(您不在任何分支上),然后将所有分支(包括本地主服务器(它仍然在原处))推送到远程主服务器.
手动解决方案是:
git push origin HEAD:master
Run Code Online (Sandbox Code Playgroud)
如果您发现推送所有分支的默认行为令人困惑(并且危险!),请将其添加到〜/ .gitconfig:
[remote.origin]
push = HEAD
Run Code Online (Sandbox Code Playgroud)
然后只推动你所在的分支.在您的示例(一个分离的头)中,您将收到此错误消息,而不是意外地推送错误的提交:
error: unable to push to unqualified destination: HEAD
Run Code Online (Sandbox Code Playgroud)
git push <latest commit SHA1 until you want commits to be pushed>
例子:
git push fc47b2
git push HEAD~2
提交链接在一起作为具有父/子机制的链.因此,推送提交实际上也会将所有父提交推送到此远程未知的提交.git push当前提交时隐式执行此操作:所有先前的提交也被推送,因为此命令等效于git push HEAD.
因此,问题可能会重写为如何推送特定提交,例如,此特定提交可能是HEAD~2.
如果您要推送的提交是非连续的,只需git rebase -i在特定推送之前使用a重新排序.
1) 如果需要,请使用“git rebase”重新排序您的提交。
git rebase -i
Run Code Online (Sandbox Code Playgroud)
此命令将在您的编辑器中显示类似内容(我正在使用 vim)
pick 4791291 commitA
pick a2bdfbd commitB
pick c3d4961 commitC
pick aa1cefc commitD
pick 9781434 commitE
# Rebase ..............
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
^G Get Help ^O WriteOut ^R Read File ^Y Prev Page ^K Cut Text ^C Cur Pos
^X Exit ^J Justify ^W Where Is ^V Next Page ^U UnCut Text ^T To Spell
Run Code Online (Sandbox Code Playgroud)
2)根据您的选择通过简单的剪切粘贴重新排序您的提交。假设新订单是
选择 9781434 commitE
选择 c3d4961 commitC
选择 4791291 commitA
选择 aa1cefc commitD
选择 a2bdfbd commitB
在编辑器中进行这些更改并按 ctrl+ O (writeOut)
或者你也可以使用
git rebase -i HEAD~<commitNumber>
Run Code Online (Sandbox Code Playgroud)
您可以检查新序列
git log
Run Code Online (Sandbox Code Playgroud)
3)现在使用
git push <remoteName> <commit SHA>:<remoteBranchName>
Run Code Online (Sandbox Code Playgroud)
如果只有一个分支在远程(原点)和一个在本地(主),只需使用
git push <commit SHA>
git push aa1cefc
Run Code Online (Sandbox Code Playgroud)
这将推动 commitB 和 commitD。