git: How to move locally committed (but not pushed) changes to a new branch 2
Recently, we’ve been working on a certain branch, we did some changes and performed a couple of commits that were not pushed on the remote system.
There was a complication and it was decided that the local changes should not be pushed to the branch that we were working on.
Rather, they changes should go to a new branch which eventually will be merged.
As mentioned above, we already had done some changes and we already had performed the commits.
git status
would give us the following:
$ git status; On branch scanner_pdu_parser_master Your branch is ahead of 'origin/scanner_pdu_parser_master' by 2 commits. (use "git push" to publish your local commits)
So, we needed to change the branch for those local commits.
Solution – Move the local commits to a new branch
First we got the name of the current branch using the command:
git branch;
Then, we switched to a new local branch
git checkout -b banana_peeler;
And, we pushed the local branch to the remote system:
git push --set-upstream origin banana_peeler;
Afterwards, we switched back to the previous branch
git checkout apple_peeler;
And reset it back to its original form, removing our local commits from it:
git reset --hard origin/apple_peeler;
Please note that the last command will delete all changes that are not committed as well.
In other words, any file you modified and did not commit or push, they will be reverted back to the original code as well.