Git 小技巧

获取第二章的sample代码

git checkout v0.2

从0.1切换到0.2时,出现如下告警

git checkout v0.2
error: Your local changes to the following files would be overwritten by checkout:
        app/routes.py
Please commit your changes or stash them before you switch branches.
Aborting

我选择放弃本地修改,切换到其他章节的代码,还有几种备选方案,可以看参考。

git reset --hard
HEAD is now at 23b3fc8 Chapter 1: Hello, World! (v0.1)
(venv) XXXXXX microblog % git checkout v0.2
Previous HEAD position was 23b3fc8 Chapter 1: Hello, World! (v0.1)
HEAD is now at 5d45592 Chapter 2: Templates (v0.2)

参考

you have three options:

Commit the change using
git commit -m "My message"
Stash it.
Stashing acts as a stack, where you can push changes, and you pop them in reverse order.

To stash, type

git stash
Do the merge, and then pull the stash:

git stash pop
Discard the local changes
using git reset --hard
or git checkout -t -f remote/branch

Or: Discard local changes for a specific file
using git checkout filename

Leave a Reply

Your email address will not be published. Required fields are marked *