git 推送提交到另一个分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13897717/
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
Push commits to another branch
提问by jviotti
Is it possible to commit and push changes from one branch to another.
是否可以将更改从一个分支提交并推送到另一个分支。
Assume I commited changes in BRANCH1and want to push them to BRANCH2.
假设我在BRANCH1 中提交了更改并希望将它们推送到BRANCH2。
From BRANCH1, is it valid to do:
从BRANCH1,这样做是否有效:
git push origin **BRANCH2**
And then reset BRANCH1?
然后重置BRANCH1?
回答by SLaks
That will almost work.
这几乎会奏效。
When pushing to a non-default branch, you need to specify the source ref and the target ref:
推送到非默认分支时,需要指定源引用和目标引用:
git push origin branch1:branch2
Or
或者
git push <remote> <branch with new changes>:<branch you are pushing to>
回答by Ryan Stewart
Certainly, though it will only work if it's a fast forward of BRANCH2 or if you force it. The correct syntax to do such a thing is
当然,尽管它仅在 BRANCH2 快进或强制它时才有效。做这样的事情的正确语法是
git push <remote> <source branch>:<dest branch>
See the description of a "refspec" on the git push man pagefor more detail on how it works. Also note that both a force push and a reset are operations that "rewrite history", and shouldn't be attempted by the faint of heart unless you're absolutely sure you know what you're doing with respect to any remote repositories and other people who have forks/clones of the same project.
有关其工作原理的更多详细信息,请参阅git push 手册页上对“refspec”的描述。另请注意,强制推送和重置都是“重写历史记录”的操作,胆小的人不应尝试,除非您绝对确定您知道对任何远程存储库和其他拥有同一项目的分叉/克隆的人。
回答by Bilal Ahmed Yaseen
It's very simple. Suppose that you have made changes to your Branch Awhich resides on both place locally and remotely but you want to push these changes to Branch Bwhich doesn't exist anywhere.
这很简单。假设您已对驻留在本地和远程位置的分支A进行了更改,但您想将这些更改推送到任何地方都不存在的分支B。
Step-01:create and switch to the new branch B
Step-01:创建并切换到新的分支B
git checkout -b B
git checkout -b B
Step-02:Add changes in the new local branch
步骤 02:在新的本地分支中添加更改
git add . //or specific file(s)
git 添加。//或特定文件
Step-03:Commit the changes
步骤 03:提交更改
git commit -m "commit_message"
git commit -m "commit_message"
Step-04:Push changes to the new branch B. The below command will create a new branch Bas well remotely
步骤 04:将更改推送到新分支B。下面的命令将远程创建一个新的分支B
git push origin B
git push 原点 B
Now, you can verify from bitbucket that the branch Bwill have one more commit than branch A. And when you will checkout the branch Athese changes won't be there as these have been pushed into the branch B.
现在,您可以从 bitbucket 验证分支B将比分支A多提交一次。当您检出分支A 时,这些更改将不存在,因为它们已被推送到分支B 中。
Note:If you have commited your changes into the branch Aand after that you want to shift those changes into the new branch Bthen you will have to reset those changes first. #HappyLearning
注意:如果您已将更改提交到分支A,然后您想将这些更改转移到新的分支B 中,那么您必须先重置这些更改。#快乐学习
回答by testing
In my case I had one local commit, which wasn't pushed to origin\master
, but commited to my local master
branch. This local commit should be now pushed to another branch.
就我而言,我有一个本地提交,它没有推送到origin\master
,而是提交到我的本地master
分支。这个本地提交现在应该被推送到另一个分支。
With Git Extensionsyou can do something like this:
使用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 pickbutton 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.
- (如果不存在则创建并)签出要推送提交的新分支。
- 从历史记录中选择提交,它应该被提交并推送到这个分支。
- 右键单击并选择Cherry pick commit。
- 之后按樱桃挑选按钮。
- 选定的提交获取应用于您的检出分支。现在提交并推送它。
- 用错误的提交检查你的旧分支。
- 将此分支硬重置为最后一次提交,一切正常(注意你在这里做什么!)。您可以通过右键单击倒数第二个提交并选择Reset current branch to here 来做到这一点。如果您知道自己在做什么,请确认操作。
You could also do that on the GIT command line. Example copied from David Christensen:
您也可以在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 cherry-pick
+git reset
是一个更快的工作流程:使用相同的场景,“功能”是最高提交不正确的分支,这样做会容易得多:
git checkout master
git cherry-pick feature
git checkout feature
git reset --hard HEAD^
节省了相当多的工作,并且是
git cherry-pick
旨在处理的场景。我还会注意到,如果它不是最顶层的提交,这也会起作用;你只需要提交一个关于cherry-pick的论点,通过:
git checkout master
git cherry-pick $sha1
git checkout feature
git rebase -i ... # whack the specific commit from the history
回答by Benyamin Jafari
I get a bad result with git push origin branch1:branch2
command:
我用git push origin branch1:branch2
命令得到了不好的结果:
In my case, branch2
was deleted and branch1
was updated with the new changes.
就我而言,branch2
已删除并branch1
使用新更改进行更新。
Thus, if you want the changes only push on the branch2
from the branch1
, try these procedures:
因此,如果你想改变只推就branch2
从branch1
,请尝试以下方法:
- On
branch1
:git add .
- On
branch1
:git commit -m 'comments'
On
branch1
:git push origin branch1
On
branch2
:git pull origin branch1
On
branch1
: revert to the previous commit.
- 在
branch1
:git add .
- 在
branch1
:git commit -m 'comments'
在
branch1
:git push origin branch1
在
branch2
:git pull origin branch1
On
branch1
:恢复到之前的提交。
回答by Ramiz Khan
you can do this easily
你可以很容易地做到这一点
git status
git add .
git commit -m "any commit"
git pull origin (branch name, master in my case)
git push origin current branch(master):branch 2(development)(in which you want to push changes)
回答by Srinu Mareti
git init
#git remote remove origin
git remote add origin <http://...git>
echo "This is for demo" >> README.md
git add README.md
git commit -m "Initail Commit"
git checkout -b branch1
git branch --list
****add files***
git add -A
git status
git commit -m "Initial - branch1"
git push --set-upstream origin branch1
#git push origin --delete branch1
#git branch --unset-upstream
回答by Sasa
You have committed to BRANCH1 and want to get rid of this commit without losing the changes? git resetis what you need. Do:
您已提交 BRANCH1 并希望在不丢失更改的情况下摆脱此提交? git reset正是你所需要的。做:
git branch BRANCH2
if you want BRANCH2 to be a new branch. You can also merge this at the end with another branch if you want. If BRANCH2 already exists, then leave this step out.
如果您希望 BRANCH2 成为一个新分支。如果需要,您也可以在最后将其与另一个分支合并。如果 BRANCH2 已存在,则无需执行此步骤。
Then do:
然后做:
git reset --hard HEAD~3
if you want to reset the commit on the branch you have committed. This takes the changes of the last three commits.
如果你想在你提交的分支上重置提交。这需要最后三个提交的更改。
Then do the following to bring the resetted commits to BRANCH2
然后执行以下操作将重置的提交带到 BRANCH2
git checkout BRANCH2
This source was helpful: https://git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch
这个来源很有帮助:https: //git-scm.com/docs/git-reset#git-reset-Undoacommitmakingitatopicbranch