Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move committed-but-unpushed changes to another branch?

Tags:

git

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?

like image 544
BrianHoltz Avatar asked Aug 27 '11 22:08

BrianHoltz


2 Answers

You can do it like this:

  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 to checkout master.
  4. 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.
like image 174
svick Avatar answered Oct 15 '22 04:10

svick


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

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.

like image 4
Karl Bielefeldt Avatar answered Oct 15 '22 02:10

Karl Bielefeldt