是否有可能将变更从一个分支提交到另一个分支。
假设我在BRANCH1中提交了更改,并希望将它们推到BRANCH2。
从BRANCH1中,这样做是否有效:
git push origin **BRANCH2**
然后重置BRANCH1?
是否有可能将变更从一个分支提交到另一个分支。
假设我在BRANCH1中提交了更改,并希望将它们推到BRANCH2。
从BRANCH1中,这样做是否有效:
git push origin **BRANCH2**
然后重置BRANCH1?
当然,尽管它只在BRANCH2的快进或强制时才会工作。这样做的正确语法是
git push <remote> <source branch>:<dest branch>
查看git push手册页上关于“refspec”的描述,了解更多关于它如何工作的详细信息。还要注意,强制推送和重置都是“重写历史”的操作,心脏不好的人不应该尝试,除非你绝对确定你对任何远程存储库和其他拥有同一项目的分叉/克隆的人知道你在做什么。
差不多可以了。
当推入一个非默认的分支时,你需要指定源ref和目标ref:
git push origin branch1:branch2
Or
git push <remote> <branch with new changes>:<branch you are pushing to>
在我的例子中,我有一个本地提交,它没有推送到origin\master,而是提交到我的本地主分支。这个本地提交现在应该推到另一个分支。
使用Git扩展,你可以做这样的事情:
(Create if not existing and) checkout new branch, where you want to push your commit. Select the commit from the history, which should get commited & pushed to this branch. Right click and select Cherry pick commit. Press Cherry pick button afterwards. The selected commit get's applied to your checked out branch. Now commit and push it. Check out your old branch, with the faulty commit. Hard reset this branch to the second last commit, where everything was ok (be aware what are you doing here!). You can do that via right click on the second last commit and select Reset current branch to here. Confirm the opperation, if you know what you are doing.
您也可以在GIT命令行上执行此操作。例子摘自大卫·克里斯滕森:
I think you'll find git cherry-pick + git reset to be a much quicker workflow: Using your same scenario, with "feature" being the branch with the top-most commit being incorrect, it'd be much easier to do this: git checkout master git cherry-pick feature git checkout feature git reset --hard HEAD^ Saves quite a bit of work, and is the scenario that git cherry-pick was designed to handle. I'll also note that this will work as well if it's not the topmost commit; you just need a commitish for the argument to cherry-pick, via: git checkout master git cherry-pick $sha1 git checkout feature git rebase -i ... # whack the specific commit from the history
我用git push origin branch1:branch2命令得到了一个糟糕的结果:
在我的例子中,branch2被删除了,branch1被更新了一些新的变化。
因此,如果你只想把从branch1推到branch2上的更改,试试下面的步骤:
在branch1上:git添加。 git在branch1上提交-m 'comments' 在branch1上:git push origin branch1 在branch2上:git拉动起源branch1 在branch1上:恢复到上一次提交。
您已经提交到BRANCH1,并希望在不丢失更改的情况下摆脱此提交? Git重置是你所需要的。 做的事:
git branch BRANCH2
如果你想让BRANCH2成为一个新的分支。如果你愿意,你也可以在最后将它与另一个分支合并。如果BRANCH2已经存在,则省略此步骤。
然后做:
git reset --hard HEAD~3
如果你想重置你已经提交的分支上的提交。这将使用最后三次提交的更改。
然后执行以下操作,将重置后的提交提交到BRANCH2
git checkout BRANCH2
这个来源很有帮助: https://git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch
这很简单。假设您已经对本地和远程驻留的分支A进行了更改,但是您想将这些更改推到不存在的分支B。
步骤01:创建并切换到新的分支B
git checkout -b
步骤02:在新的本地分支中添加更改
Git添加。//或指定文件
步骤03:提交更改
Git commit -m commit_message
步骤04:将更改推送到新的分支B。下面的命令也将远程创建一个新的分支B
git push origin B
现在,你可以从bitbucket中验证分支B将比分支A多提交一次,当你签出分支A时,这些更改将不会存在,因为这些更改已被推入分支B。
注意:如果您已经将更改提交到分支A中,然后您想将这些更改转移到新的分支B中,那么您必须首先重置这些更改。# HappyLearning
这很容易做到
git status
git add .
git commit -m "any commit"
git pull origin master
git push origin master:development # assuming 'development' is the target branch name.
git init #git远程删除源文件 Git远程添加origin <http://...git> echo "This is for demo" >> README.md git添加README.md git commit -m "初始提交" Git checkout -b branch1 Git分支——list * * * * * * *添加文件 git添加-A git状态 git commit -m "Initial - branch1" Git push——set-upstream origin branch1 #git push origin——delete branch1 #git分支——unset-upstream
当您将代码推到另一个分支时,只需遵循下面的git命令。记住demo是我的另一个分支名,你可以用你的分支名替换。
git push origin master:demo
在BRANCH1上提交更改。 打开终端,输入命令-> "git push <remote_name> <branch1_name>:<branch2_name>"
完成了。
对于这个问题: 命令为
git push origin BRANCH1:BRANCH2
编辑:现在我意识到,GitHub桌面有很好的UI将你的变化从一个分支到另一个。当然,这不是一个比用手敲击终端命令更好的选择。
@SLaks的答案是小道具,这对我来说基本适用。但是在分支有不同头的情况下。一个方便的方法是使用樱桃。
git登录Branch1与您的更改 复制提交SHA您的更改 Git checkout branch2 -在这里应用您的更改 git选择SHA_OF_COMMIT_FROM_STEP_2