I've done a fair bit of work ("Your branch is ahead of 'origin/master' by 37 commits.") which really should have gone into its own branch rather than into master
. These commits only exist on my local machine and have not been pushed to origin
, but the situation is complicated somewhat in that other devs have been pushing to origin/master
and I've pulled those changes.
How do I retroactively move my 37 local commits onto a new branch? Based on the docs, it appears that git rebase --onto my-new-branch master
or ...origin/master
should do this, but both just give me the error "fatal: Needed a single revision". man git-rebase
says nothing about providing a revision to rebase
and its examples do not do so, so I have no idea how to resolve this error.
(Note that this is not a duplicate of Move existing, uncommited work to a new branch in Git or How to merge my local uncommitted changes into another Git branch? as those questions deal with uncommitted changes in the local working tree, not changes which have been committed locally.)
This question is related to
git
One more way assume branch1 - is branch with committed changes branch2 - is desirable branch
git fetch && git checkout branch1
git log
select commit ids that you need to move
git fetch && git checkout branch2
git cherry-pick commit_id_first..commit_id_last
git push
Now revert unpushed commits from initial branch
git fetch && git checkout branch1
git reset --soft HEAD~1
Checkout fresh copy of you sources
git clone ........
Make branch from desired position
git checkout {position}
git checkout -b {branch-name}
Add remote repository
git remote add shared ../{original sources location}.git
Get remote sources
git fetch shared
Checkout desired branch
git checkout {branch-name}
Merge sources
git merge shared/{original branch from shared repository}
I stuck with the same issue. I have found easiest solution which I like to share.
1) Create new branch with your changes.
git checkout -b mybranch
2) (Optional) Push new branch code on remote server.
git push origin mybranch
3) Checkout back to master branch.
git checkout master
4) Reset master branch code with remote server and remove local commit.
git reset --hard origin/master
Alternatively, right after you commit to the wrong branch, perform these steps:
git log
git diff {previous to last commit} {latest commit} > your_changes.patch
git reset --hard origin/{your current branch}
git checkout -b {new branch}
git apply your_changes.patch
I can imagine that there is a simpler approach for steps one and two.
For me this was the best way:
git fetch
git branch my-changes
and push to remotegit master -u upstream-branch remotes/origin/my-changes
git branch master --set-upstream-to remotes/origin/master
What about:
git reset
back to the last commit before you started making changes.git pull
to re-pull just the remote changes you threw away with the reset.Or will that explode when you try to re-merge the branch?
If you have a low # of commits and you don't care if these are combined into one mega-commit, this works well and isn't as scary as doing git rebase
:
unstage the files (replace 1 with # of commits)
git reset --soft HEAD~1
create a new branch
git checkout -b NewBranchName
add the changes
git add -A
make a commit
git commit -m "Whatever"
A simpler approach, which I have been using (assuming you want to move 4 commits):
git format-patch HEAD~4
(Look in the directory from which you executed the last command for the 4 .patch
files)
git reset HEAD~4 --hard
git checkout -b tmp/my-new-branch
Then:
git apply /path/to/patch.patch
In whatever order you wanted.
Here is a much simpler way:
Create a new branch
On your new branch do a git merge master
- this will merge your committed (not pushed) changes to your new branch
Delete you local master branch git branch -D master
Use -D
instead of -d
because you want to force delete the branch.
Just do a git fetch
on your master branch and do a git pull
on your master branch to ensure you have your teams latest code.
Source: Stackoverflow.com