'Switch between git branches [duplicate]

I have a repository having two branches master ( keeps final error free code ) and develop ( developing branch ).The default branch is master, I cloned and worked on the project, but unfortunately I forgot to checkout to the develop branch before I start coding.So all the changes that I made will be on the master branch. Is there any way to switch the branch after developing? so that I can commit and push the code only to the develop branch without loosing my code.



Solution 1:[1]

If you don't have any work saved on your develop branch, just delete it with git branch -d develop and rename master to develop with git branch -m master develop.

If you have something already on your develop, just merge the changes into develop with git checkout develop then git merge master.

Solution 2:[2]

git fetch origin master
git checkout main

this command will switch your branch

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 walgarch
Solution 2 Hu Zhi Xing