I've got some unpushed git commits in my child of our master branch. I did a rebase to a feature branch, and successfully merged the few conflicts that resulted, but I didn't realize that rebase wouldn't leave me on the feature branch. If I now git checkout the feature branch, it will presumably overwrite my committed changes in my working tree, leaving them in some kind of limbo. I could "git checkout -m", but that apparently is only for merging uncommitted local changes. How can I get onto the feature branch and keep my unpushed-but-committed changes that I currently have under the master branch (without pushing them to the master branch)? Should I have used git rebase -onto? If so, what should I do now that I've rebased without -onto?
You can do it like this:
git checkout branch
to checkout the feature branch.git reset --hard master
to move the branch to be the same commit as master
right now. By doing this, you lose all commits that are in the branch. Because of your rebase, all those commits should have copies on master
, so you shouldn't actually lose anything.git checkout master
to checkout master
.git reset --hard origin/master
to reset master
to the state that is on the origin
repo. This assumes you didn't have any unpushed changes to master
. If you do, replace origin/master
with the commit id you want to reset to.First to restate your situation to make sure I understood correctly. You made a few commits on master
and wanted to move them to feature
, so you ran git rebase feature
? First, you want to undo that. Assuming you still have master
checked out, and haven't done any rebases since, run git reset --hard ORIG_HEAD
to undo the rebase.
Now, for the proper way to move your commits over to the feature
branch. Make and checkout a temporary branch so you don't mess up master
:
git checkout -b temp
Now rebase all the changes made since the last commit on origin/master
ontofeature
:
git rebase --onto feature origin/master
That moves the temp
branch as if it was branched from feature
. Now to merge the changes onto feature
, do:
git checkout feature
git merge temp
git branch -d temp
It will be a fast-forward merge since you just rebased.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With