Switching a branch after aborting current changes in git

我是研究僧i 提交于 2019-12-02 16:11:33

Option 1

git checkout -f gh-pages

Option 2

git reset --hard     # beware: don't make that a habit
git checkout gh-pages

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.

You can ignore all uncommitted changes.

git reset --hard HEAD

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

If you have unstaged file, try:

git checkout -- .

Or

git checkout -- filename

git add -A git stash

this will clear all the changes you made (Only those that are not commited)

You can discard changes you made in a specific file:

git checkout somefile.txt

And then jump smoothly to the branch:

git checkout gh-pages
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!