将更改推送到新分支
Push changes to new branch
我对 git 的分支仍然有些困惑。
我在 local master
分支上编写了所有更改,然后意识到我想将这些更改推送到新分支而不是 remote master
分支。所以现在我在 github 上的远程仓库上创建了一个名为 dev
的新分支,并更新了 IntelliJ 中的分支列表。它看起来像这样:
我如何确保更改(我认为目前正在 local master
上完成,但我没有 committed/pushed 它们)被推送到 remote dev
分支?
git checkout dev
git add <the_changed_files_you_want_to_add>
git commit
git push origin dev
在理想情况下,上述命令应该足够了。
git checkout dev
可能会因为冲突而失败,这会报错
error: Your local changes to the following files would be overwritten by checkout:
....
Please commit your changes or stash them before you switch branches.
如果是这样,你可以试试:
# commit the changes on "master"
git add <the_changed_files_you_want_to_add>
git commit
# if there are any changed files left
git stash
# if "git stash" is not run here, DON'T run "git stash pop" in the end
# create the local dev and apply the new commit
git checkout dev
git cherry-pick master
# if there are any conflicts, open and edit the conflicted files and resolve them and then
git add <conflicted_files>
git cherry-pick --continue
# push the local dev to the server
git push origin dev
# restore and clean the local master
git checkout master
# discard the commit whose changes you want to be on "dev"
git reset HEAD^ --hard
# only if "git stash" was run
git stash pop
如果您还没有提交任何文件,在您为 您新创建的远程分支 执行 checkout
之后,它们应该保持不变(尤其是如果这两个分支相同).这将创建您的本地分支,您可以在其中提交任意数量的内容,但它只是本地。只有当你做 push
.
时,最新的本地 commits/changes 才会去远程分支
在完成任何提交之前,我总是小心更改分支,因为您可能会丢失所做的更改,因此请小心。
我最终使用 IntelliJ 的界面(使用 Ctrl+K
)来 git add
我想要的文件并为我的 git commit
设置消息。在我当前的“local master
”分支上发生的提交之后,我只使用了git push origin HEAD:dev
并且一切都按预期工作。
编辑:
将近一年后,这里是 GUI 解决方案的更新答案!
提交后,您可以使用 Ctrl+Shift+K
打开 Push
对话框。下面是 add-failure-TE-dataImpl-ftr
本地分支上的单个提交(名为 whatever
)的示例,并朝向我们知道将在您的 origin
上创建的远程分支 newBranch
远程,因为 New
小框出现在其名称的右侧。
在达到该状态之前,您的正常上游分支会显示在 origin:
的右侧,您可以通过简单地左键单击它并输入您要创建的新分支的名称来更改它!
我对 git 的分支仍然有些困惑。
我在 local master
分支上编写了所有更改,然后意识到我想将这些更改推送到新分支而不是 remote master
分支。所以现在我在 github 上的远程仓库上创建了一个名为 dev
的新分支,并更新了 IntelliJ 中的分支列表。它看起来像这样:
我如何确保更改(我认为目前正在 local master
上完成,但我没有 committed/pushed 它们)被推送到 remote dev
分支?
git checkout dev
git add <the_changed_files_you_want_to_add>
git commit
git push origin dev
在理想情况下,上述命令应该足够了。
git checkout dev
可能会因为冲突而失败,这会报错
error: Your local changes to the following files would be overwritten by checkout:
....
Please commit your changes or stash them before you switch branches.
如果是这样,你可以试试:
# commit the changes on "master"
git add <the_changed_files_you_want_to_add>
git commit
# if there are any changed files left
git stash
# if "git stash" is not run here, DON'T run "git stash pop" in the end
# create the local dev and apply the new commit
git checkout dev
git cherry-pick master
# if there are any conflicts, open and edit the conflicted files and resolve them and then
git add <conflicted_files>
git cherry-pick --continue
# push the local dev to the server
git push origin dev
# restore and clean the local master
git checkout master
# discard the commit whose changes you want to be on "dev"
git reset HEAD^ --hard
# only if "git stash" was run
git stash pop
如果您还没有提交任何文件,在您为 您新创建的远程分支 执行 checkout
之后,它们应该保持不变(尤其是如果这两个分支相同).这将创建您的本地分支,您可以在其中提交任意数量的内容,但它只是本地。只有当你做 push
.
在完成任何提交之前,我总是小心更改分支,因为您可能会丢失所做的更改,因此请小心。
我最终使用 IntelliJ 的界面(使用 Ctrl+K
)来 git add
我想要的文件并为我的 git commit
设置消息。在我当前的“local master
”分支上发生的提交之后,我只使用了git push origin HEAD:dev
并且一切都按预期工作。
编辑:
将近一年后,这里是 GUI 解决方案的更新答案!
提交后,您可以使用 Ctrl+Shift+K
打开 Push
对话框。下面是 add-failure-TE-dataImpl-ftr
本地分支上的单个提交(名为 whatever
)的示例,并朝向我们知道将在您的 origin
上创建的远程分支 newBranch
远程,因为 New
小框出现在其名称的右侧。
在达到该状态之前,您的正常上游分支会显示在 origin:
的右侧,您可以通过简单地左键单击它并输入您要创建的新分支的名称来更改它!