I am learning to use git from command line, I have been using it from Pycharm GUI.
I am working on my_project
directory with pycharm, on branch my_branch
.
Now, I want to bring some new feature to my code, and I want to do it in another branch, so that in case I spoil something I can abort the new feature by simply deleting the new branch.
To create and switch to the new branch I have run git checkout -b my_branch_2
, started to do some changes (committed none), then I want to go back to my_branch
, so I run git checkout my_branch
.
At this point, as Pycharm GUI usually does when one tryes to checkout from a branch with uncommitted changes to another branch, I expected git to ask me if I want to commit the changes or to checkout and discard the changes to my_branch_2
and to switch to my_branch
code (Pycharm executes this latter option when the user select the button "force checkout").
Instead, it switches back to my_branch
without asking anything and carrying the changes I made to my_branch_2
, so that eventually wrong code is brought to my stable branch !
Why does it happen?
Am I using wrong the command git checkout
?
Which git commands do I have to run to replicate the behavior of Pycharm when I choose the "Force checkout" option from its GUI?
CodePudding user response:
That's the expected behaviour of git checkout
Local modifications to the files in the working tree are kept, so that they can be committed
If you don't want those to carry over, either reset, stash or clean them.
So, the equivalent of "Pycharm's forced checkout" on command line (which will delete all uncommitted local changes and switch from the current branch to my_branch
) is
git checkout . && git checkout my_branch
and this has to be run in root directory.