¿Cómo mover cambios comprometidos pero no aplicados a otra rama?

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?

preguntado el 27 de agosto de 11 a las 22:08

Not a dupe. It's a very different problem with changes that are already committed, not to mention have gone through an incorrect rebase. -

2 Respuestas

Puedes hacerlo así:

  1. git checkout branch to checkout the feature branch.
  2. 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.
  3. git checkout master para revisar master.
  4. git reset --hard origin/master reiniciar 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.

Respondido 28 ago 11, 02:08

"you lose all commits that are in the branch..." I don't follow this part. My goal is to keep my unpushed commits by moving them to the feature branch. "this assumes you didn't have any unpushed changes to master". Again, unpushed commits are the whole reason why I'm asking my question. - BrianHoltz

I see that wat I want to do is described in the git-reset man page under "Undo a commit, making it a topic branch". - BrianHoltz

The git man page mentioned by BrianHoltz is here: git-scm.com/docs/git-reset/2.0.0#_examples - Berend de Boer

Small note: for my case I wanted to move the commits to a Un nuevo branch after starting work on master and accidentally committing there. For this you can use git checkout -b new-branch as step 1 (adding the -b bandera a git checkout creates a new branch) and skip step 2 (since you are already on the same commit as master). - Luca_Scorpion

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/mastersobrefeature:

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, hacer:

git checkout feature
git merge temp
git branch -d temp

It will be a fast-forward merge since you just rebased.

Respondido 28 ago 11, 09:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.