Reputation: 5229
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:
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 ?
Upvotes: 3
Views: 9391
Reputation: 203
I was in a similar situation, where I accidentally started making some changes in my local Master branch without committing them and wanted to move them to a new branch without having to manually making those changes again.
In Intellij Idea all I had to do was create a new branch using the Git button at the bottom right corner and all my changes were automatically there, then I just committed the changes and pushed to remote and it successfully created a new remote branch with my changes.
I am not sure if this will work if you already committed your changes to the Master branch, but in that case you can just create a branch from the Master branch and then just reset your local Master branch later perhaps.
Upvotes: 2
Reputation: 5229
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!
Upvotes: 3
Reputation: 2645
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.
Upvotes: 0
Reputation: 30868
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
Upvotes: 2