How do I move unpushed committed code to another branch?

Git

Git Problem Overview


So I have the following situation:

I committed some work locally, without pushing to the remote repository. I want to move this local code to another branch, because if I pull, there will be modifications that will ruin all the work I put locally.

This is the output of git status on the old branch:

On branch <branch_name>
Your branch is ahead of 'origin/<branch_name>' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

And this is the output of git status on the newly created branch:

On branch <branch_name>
nothing to commit, working directory clean

Git Solutions


Solution 1 - Git

If it's just one commit, you can simply do

git reset HEAD~1
git stash
git checkout anotherbranch
git stash pop

And if you want to put it in a fresh new branch, another way is

git branch newbranch
git reset --hard HEAD~1

Solution 2 - Git

If you made the branch after you committed it should contain the commit that you are wanting to move. You can verify this with git log, you should see your commit as the first on in the log.

On the branch that you no longer want the commit to be do git reset --hard HEAD~. This removes the commit from the branch and reset the branch so that you can now pull without any issue. (Be sure that your commit is on the other branch as after doing this your commit will be gone).

If the commit is not on your other branch, you can either delete the branch and create it again from the original branch with git checkout -b <branch name> or you can cherry-pick it into your branch with git cherry-pick <SHA of your commit> which will make a copy of the commit on your branch. Then you can reset the original branch with the steps above.

Solution 3 - Git

In my case the answer was:

  1. Create a new branch that has the current state:
    git checkout -b new-branch
    
  2. Go back to the branch you want to remove the unpushed commits from
    git checkout -
    
  3. Remove the unpushed commits
    git reset --hard origin
    

Solution 4 - Git

You have options:

  1. Branch off of your current branch to create a new one with all the commits already in place, then reset the old one back to the original commits
  2. Cherry pick the commits to a new branch
  3. Merge current branch into new branch with fast-forward enabled
  4. Rebase new branch on top of old branch

I'm sure there are many more options. It depends on your specific case which one is best.

Solution 5 - Git

The Current Situation:

(If I understand correctly):

This is what you want:


Branch A -> |<---Commit A--->| 
                        \  
Branch B                  ->|<---Commit B--->|   


But This is what you currently have:


Branch A -> |<---  Commit A --->|  ->   |<---  Commit B --->|   

How to get there?

Right now the ref for branch A is pointing to the commit B SHA. We need it to point to commit A. How do we do this?

  1. First get on Branch A with:

git checkout branch-A

  1. Once you are on Branch A, we want to forcibly rewrite the commit that the Branch A ref points to – we want to do a reset – but we still want to retain the files prior to them being staged – so we can transfer them to a new branch. In order to do this, we must use a git reset hard with a -– mixed flag. HEAD~1 means that you are resetting to a commit that is exactly one commit behind where you are right now.

git reset HEAD~1 --mixed

  1. Now that you are here, simply create a new branch:

git checkout -b new-branch-name

  1. And after that, immediately commit those changes:

git commit -am ‘fix ABC bug’

Voila! Quite simple really!

A better solution

An alternative is to do a --hard reset on branch A to HEAD~1, and to simply checkout out a new branch from commit b - the latest commit. We are not adding any unnecessary commits in this scenario.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNarcis NeacsuView Question on Stackoverflow
Solution 1 - Gitj4nwView Answer on Stackoverflow
Solution 2 - GitSchleisView Answer on Stackoverflow
Solution 3 - GitIddan AaronsohnView Answer on Stackoverflow
Solution 4 - GitJoe PhillipsView Answer on Stackoverflow
Solution 5 - GitBenKoshyView Answer on Stackoverflow