How do I move unpushed committed code to another branch?

Viewed 31961

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
6 Answers

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
    

That for me

Revert the commit

git reset HEAD~1

Create new Branch and commits changes

git checkout -b feature/15421
git add .
git push origin feature/15421
Related