Switching a branch after aborting current changes in git

前端 未结 9 1387
情书的邮戳
情书的邮戳 2021-01-30 17:00

I cloned a git repo and then started playing around in its master branch. After a while, I want to ignore the changes I just made (without committing them), and switch to a diff

相关标签:
9条回答
  • 2021-01-30 17:29

    To ignore but NOT remove the changes

    OP asks to ignore changes - but does not ask to remove them. Sounds like OP want changes to still be around. So this answer is for those folks in a similar situation who don't want to lose their changes.

    Given the limitations encountered already, plus given you want to keep those changes, one option is to put them in another branch

    Regardless of where you have either uncommitted or staged changes, you can always make a new branch with

    git checkout -b new_branch
    

    and then git add and git commit as necessary. and now you can switch to other branches at will, knowing these changes have been saved.

    Like the OP I don't like to use stash and pop (and cherry pick) but isolating these changes by putting them into a new branch I can then merge and rebase to and from works well for me. Using branches for the solution is easy for me

    0 讨论(0)
  • 2021-01-30 17:32

    Just for the sake of completeness, and for those who landed here by searching: Although the OP asks specifically for a solution without stashing, it is worth mentioning that stash is indeed a very nice option:

    The command saves your local modifications away and reverts the working directory to match the HEAD commit.

    So you can simply

    git stash
    

    This is like resetting to HEAD. When being absolutely positively certain that indeed those uncommitted changes are worthless, simply

    git stash drop
    

    You can even have multiple stashes etc. as mentioned in the documentation link above.

    I would recommend this practice since it puts one in the habit to double-think before resetting without a significant cost.

    0 讨论(0)
  • 2021-01-30 17:33

    If you're really sure that you want to throw away your uncommitted changes (i.e. those that are staged as well as those in your working tree) you can do:

    git reset --hard
    

    In general, stashing is often safer

    0 讨论(0)
提交回复
热议问题