将更改推送到新分支

时间:2019-11-03 22:09:11

标签: git github intellij-idea branch git-branch

我仍然对git分支感到困惑。

我在local master分支上编码了所有更改,然后意识到我想将这些更改推送到新的分支上,而不是remote master分支上。因此,现在我在github上的远程仓库上创建了一个名为dev的新分支,并更新了IntelliJ中的branchs-list。看起来像这样:

git branches on intellij

如何确保所做的更改(我认为目前在local master上进行了更改,但是我尚未提交/推送它们)在remote dev分支上进行了推送?

3 个答案:

答案 0 :(得分:1)

我最终使用了IntelliJ的界面(带有Ctrl+K)来git add我想要的文件,并为我的git commit设置了消息。提交完成后,发生在我当前的“ local master”分支上,我只使用了git push origin HEAD:dev,一切都按预期进行了。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果您尚未提交任何文件,则在为您的新创建的远程分支机构checkout(特别是如果这两个分支机构相同)执行push之后,它们应该保持不变。这将创建您的本地分支机构,您可以在其中进行任意数量的提交,但只能是 local 。仅当您执行UString时,最新的本地提交/更改才会进入远程分支。

在进行任何提交之前,我总是非常谨慎地更改分支,因为您可能会松开所做的更改,因此要当心。