Reputation: 1193
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
Upvotes: 91
Views: 51404
Reputation: 1018
branch-B
):<branch-A> git checkout branch-B
branch-A
into branch-B
:<branch-B> git rebase branch-A
Rebasing is the process of combining or moving a sequence of commits on top of a new base commit.
branch-B
has the commit(s) you wanted to move:<branch-B> git log
branch-A
:<branch-B> git checkout branch-A
<branch-A> git reset --hard
Upvotes: 0
Reputation: 667
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
Upvotes: -3
Reputation: 43690
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.
Upvotes: 66
Reputation: 1210
In my case the answer was:
git checkout -b new-branch
git checkout -
git reset --hard origin
Upvotes: 53
Reputation: 35575
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?
git checkout branch-A
git reset HEAD~1 --mixed
git checkout -b new-branch-name
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.
Upvotes: 1
Reputation: 51100
You have options:
I'm sure there are many more options. It depends on your specific case which one is best.
Upvotes: 4
Reputation: 2405
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
Upvotes: 88