How do I move unpushed committed code to another branch?
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
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
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.
In my case the answer was:
- Create a new branch that has the current state:
git checkout -b new-branch
- Go back to the branch you want to remove the unpushed commits from
git checkout -
- Remove the unpushed commits
git reset --hard origin
You have options:
- 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
- Cherry pick the commits to a new branch
- Merge current branch into new branch with fast-forward enabled
- 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.
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?
- First get on Branch A with:
git checkout branch-A
- 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
- Now that you are here, simply create a new branch:
git checkout -b new-branch-name
- 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.