I am working on a project using git hub for version control. I have a branch called main and I created a branch called tests with the command git checkout -b pruebas
, up to that point it brings me the code that I have in the main branch, I verify with the command git branch
if I am in the tests branch and I start to make the respective changes, but at Switching to the main branch shows me the code that I have written in the tests branch without having made a commit or a git merge
. I am using visual studio code and the commands are executed from the git bash console. But, I don't know how to solve that problem or if there is any step that I am not doing. Thanks for your help.
There is no problem. What you're seeing is how git is expected to work. As long as those changes are floating in your working tree (that is, not being committed), if you ask git to take you to another branch, those changes will follow you. So when you go to
main
, you see the changes are there... and if you go back to the other branch, they go with you again.When you checkout uncommitted changes, git does a check to see if checkout is possible. The files that are modified with respect to the current branch must be the same in the branch you want to go to (that is, those files must be the same between
HEAD
and the destination branch). If that's not the case, then git throws a warning and the checkout is aborted...unless you docheckout -f
and in that case the changes you made are lost .If you want to go to another branch without taking these changes and without losing them, you must use "the cove" (that is, the stash ):