重新设置单个 Git 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14635672/
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
Rebase a single Git commit
提问by Kevin Meyer
Is there a way to rebase a single commit from a branch onto another branch?
有没有办法将一个分支的单个提交重新绑定到另一个分支?
I have this branch structure:
我有这个分支结构:
-- -- -- -- -- (Master)
\
-- -- -- -- -- XX (Feature-branch)
All I want to do is to rebase the last commit of Feature-branch
onto master and rollback Feature-branch
one commit.
我想要做的就是将最后一次提交变基Feature-branch
到 master 并回滚Feature-branch
一次提交。
-- -- -- -- -- XX (Master)
\
-- -- -- -- -- (Feature-branch)
How do I do that?
我怎么做?
回答by tewe
You can cherry-pick XX to master.
你可以挑选XX来掌握。
git checkout master
git cherry-pick <commit ID of XX>
And remove the last commit from the feature branch with git reset.
并使用 git reset 从功能分支中删除最后一次提交。
git checkout Feature-branch
git reset --hard HEAD^
回答by CharlesB
git rebase --onto master branch~1 branch
This says "rebase the range of commits between last-before-branch and branch (that is, XX commit) on the tip of master branch"
这表示“在 master 分支的尖端重新确定 last-before-branch 和分支(即 XX 提交)之间的提交范围”
After this operation branch
tip is moved on commit XX
, so you want to set it back with
在branch
提交时移动此操作提示后XX
,因此您希望将其设置回
git checkout branch
git reset --hard branch@{1}^
Which says "reset the branch tip to the commit before its previous state"
其中说“将分支提示重置为之前状态之前的提交”
So a cherry pick is a simpler solution...
所以樱桃挑选是一个更简单的解决方案......
回答by hrdwdmrbl
It's pretty simple to do actually. The solution is to do an interactive rebase and "drop" all of the commits you don't want to include in the rebase.
其实做起来很简单。解决方案是进行交互式 rebase 并“删除”您不想包含在 rebase 中的所有提交。
git rebase -i <target_branch>
where target_branch
is the branch you want to rebase on to
git rebase -i <target_branch>
target_branch
您要重新定位的分支在哪里
Then you will edit the file that is opened and pick
the commits you do want and drop
(or d
for short) all the commits you don't want to bring along.
然后您将编辑打开的文件和pick
您想要的提交以及drop
(或d
简称)您不想带来的所有提交。
回答by albfan
@Charles response is correct. Anyway I ended up using this so many times, most of all to rebase specific config on a project
@Charles 的回答是正确的。无论如何,我最终使用了很多次,最重要的是在项目上重新设置特定配置
* a8f9182 (HEAD -> production) production configuration | * daa18b7 (pre) preproduction configuration |/ | * d365f5f (local) local configuration |/ * 27d2835 (dev) amazing new feature that will save the world * | 56d2467 (master) boring state of the art for project |/
that I create a new command for it:
我为它创建了一个新命令:
$ cat ~/bin/git-rebaseshot COMMIT= DEST=${2:-HEAD} git rebase ${COMMIT}^ ${COMMIT} --onto $DEST
normally you want to autocomplete branch names for that command, so add it sourcing this function (adding to .bashrc or .profile):
通常你想为那个命令自动完成分支名称,所以添加它来获取这个函数(添加到 .bashrc 或 .profile):
_git_rebaseshot () { __gitcomp_nl "$(__git_refs)" }
git autocomplete will search for it
git autocomplete 会搜索它
you can use this command like this:
你可以像这样使用这个命令:
# rebase config on prepro on actual HEAD
$ git rebaseshot prepro
# rebase config on local onto dev
$ git rebaseshot local dev
# rebase production config on master
$ git rebaseshot pro master
When you divide features correctly, possibities are endless.
当您正确划分特征时,可能性是无穷无尽的。
* a8f9182 (HEAD -> postgres) BBDD config * a8f9182 (local) local config * a8f9182 (debug) log level config * a8f9182 (dev) new feature |
I guess this is what quiltpeople like to do.
我想这就是被子人喜欢做的事情。
this command will work anyway with whatever sha/ref you provide:
无论如何,此命令将适用于您提供的任何 sha/ref:
$ git rebaseshot <Feature branch> master
$ git rebaseshot <commit of XX> master
回答by cdmo
Here's another option:
这是另一种选择:
- Make sure you have a remote with a copy of the feature branch
- Delete the local feature branch
- Create and checkout a new branch with the same name as the old feature branch you just deleted off of master
- cherry-pick the one commit from the remote copy of the feature branch you want.
- 确保您有一个带有功能分支副本的遥控器
- 删除本地功能分支
- 创建并签出一个与您刚刚从 master 中删除的旧功能分支同名的新分支
- 从您想要的功能分支的远程副本中挑选一个提交。
Commands look like:
命令看起来像:
git checkout Feature-branch
git push -u origin HEAD
git checkout master
git branch -D Feature-branch
git checkout -b Feature-branch
git cherry-pick HASH-OF-XX
It's not a rebase command no, but it's a rebase in spirit.
这不是一个 rebase 命令,但它在精神上是一个 rebase。