Switch from one git branch to another without loosing the newly added code

Viewed 387

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.

2 Answers

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.

git fetch origin master
git checkout main

this command will switch your branch

Related