How do I move uncommitted code to another branch?
So I have the following situation:
I've done some work locally without pushing to the remote repository. I want to move this local code to a different branch because if I pull there will be changes that will ruin all the work I have pushed locally.
This is the result of "git status" on the old branch:
"On a branch, your branch is ahead of" origin / "by 1 commit. (Use" git push "to push local commits) do nothing, working directory is clean"
And this is the result of "git status" on the newly created branch:
"Do nothing on the branch, the working directory is clean"
source to share
If you made a branch after you committed it, it should contain the commit you want to move. You can check this with git log
, you should see that your commit is the first one in the log.
On a branch, you don't want the commit anymore git reset --hard HEAD~
. This will remove the commit from the branch and reset the branch so you can now pull without any problem. (Make sure your commit is on a different branch, as your commit will disappear after that).
If the commit is not on your other branch, you can either delete the branch or re-create it from the original branch with git checkout -b <branch name>
, or you can push it into your branch with a help git cherry-pick <SHA of your commit>
that will make a copy of the commit in your branch. Then you can reset the original branch with the steps above.
source to share
You have options:
- Disconnect your current branch to create a new one, with all commits already in place, and then reset the old one back to the original commits
- Cherries are pushing commits to a new branch
- Merge the current branch into a new branch with fast forwarding enabled
- Rebuild a new branch on top of an old branch
I'm sure there are many more options. It depends on your particular case, which is best.
source to share
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?
Currently, the ref for branch A points to a commit B SHA. We need it to point to commit A. How do we do this?
- First go to section A with:
git checkout branch-A
- Once you're on branch A, we want to force-rewrite the commit that the reflector Branch A points to - we want to do a reset, but we still want to save the files before we push them - so we can push them to the new branch. To do this, we have to use git reset hard with a mixed flag. HEAD ~ 1 means you are resetting to a commit, which is exactly one commit where you are right now.
git reset HEAD~1 --mixed
- Now that you are here, just create a new branch:
git checkout -b new-branch-name
- And after that immediately execute these changes:
git commit -am ‘fix ABC bug’
Voila! Pretty simple!
The best decision
An alternative is to do --hard reset on branch A to HEAD ~ 1 and just checkout the new branch from commit b - the latest commit. We are not adding unnecessary commits in this scenario.
source to share