I worked on master and forgot that I did not change the branch, tried to push, but because of the limitations I could not (I am happy), I changed to develop, but the commit remained on master, how can I transfer it to develop?
3 Answers
Just to give the full version of the cherry-pick solution to this situation (which also deletes the commit from master afterwards)
git checkout develop
git cherry-pick master
git checkout master
git reset --hard HEAD^

- 19,645
- 3
- 36
- 61
By using git reset
. See also here.
By using reset you can take the changes back into your working tree.
Then you switch to the branch you want to commit to, and then commit the changes.
git checkout master
git reset --soft HEAD~1
git checkout develop
git commit
Where HEAD~1
refers to two commits ago. You can also use the commit-ish of the commit prior to the erroneous commit.
--soft
means take the changes out of the commit (but leave them in to your workspace) and also keep them in the 'staging' area.
Without --soft
git defaults to --mixed
which would reset the staging area as well, which isn't a big deal if you have no other local changes. You would just have to re-add them via git add xyz.txt
or whatever.

- 4,901
- 40
- 61
-
it just takes my commit back – Самир Шахмурадлы Feb 18 '19 at 17:59
-
@Nick I liked your solution, but did you not forget to re-add files to the index before the commit? Since mixed reset clears the index, changes are not staged after it. Unless I'm missing something? Or even better, you could do a `reset --soft` instead of a reset `--mixed` and then not bother with re-adding? – Romain Valeri Feb 18 '19 at 18:28
I understood how to do this, I need to go to the branch to which I want to transfer my changes and use the git cherry-pick commit identifier

- 775
- 2
- 11
- 23
-
This does *not* delete the commit from master. If you don't have the faulty commit on `master` any more, I guess you tried Nick's solution beforehand and *that* got rid of it on `master`. And since the commit still existed (though not on `master` dangling), the cherry-pick worked and got it on your `develop` branch. – Romain Valeri Feb 18 '19 at 18:29
-
For convenience, you can feed a branch ref to `cherry-pick` and it will use its tip (last) commit, it's sometimes slightly easier than first searching for your `commit identifier` (SHA-1) – Romain Valeri Feb 18 '19 at 18:39