简体   繁体   中英

Push changes to new branch

I'm still slightly confused about branches with git.

I coded all my changes on the local master branch, and then realized I'd want to push those changes on a new branch instead of the remote master branch. So now I've created a new branch called dev on my remote repo on github and updated the branches-list in IntelliJ. It looks like this:

intellij 上的 git 分支

How do I ensure the changes (which I think are currently being done on local master , but I haven't committed/pushed them) are pushed on the remote dev branch?

I ended up using IntelliJ's interface (with Ctrl+K ) to git add the files I wanted and to set the message for my git commit . After the commit, which happened on my current " local master " branch, I just used git push origin HEAD:dev and everything worked as expected.

EDIT :

Almost a year later, here is an updated answer with the GUI solution!

After you've committed, you can use Ctrl+Shift+K to open up the Push dialog. Below is an example of a single commit (named whatever ) on the add-failure-TE-dataImpl-ftr local branch, and headed toward the remote branch newBranch which we know will be created on your origin remote because there is the little New box appearing just to the right of its name.

在此处输入图像描述

Before reaching that state, you normal upstream branch would be showed to the right of origin: , you can change that by simply left-clicking it and typing in the name of the new branch you want to create!

git checkout dev
git add <the_changed_files_you_want_to_add>
git commit
git push origin dev

In an ideal situation, the above commands should be enough.

git checkout dev may fail due to conflicts, which would complain

error: Your local changes to the following files would be overwritten by checkout:
    ....
Please commit your changes or stash them before you switch branches.

If so, you could try:

# 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

If you haven't committed any files they should stay the same after you do checkout for your newly created remote branch (especially if those 2 branches are identical). This will create your local branch where you can commit as much as you want, but it will be only locally . The latest local commits/changes will go the remote branch only when you do push .

I am always careful with changing the branches before I have done any commits as it might happen that you could loose the changes that you made, thus take care.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM