git:放弃对当前签出版本的更改

git: abandon changes to current checked out version

我检查了我的项目的早期版本,使用:

$ git checkout fd4a4a1

回复是:

Note: checking out 'fd4a4a1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at fd4a4a1... <original commit message>

我复制了一大段代码,但没有做任何改动。我现在想 return 到当前分支。然而,当我 运行...

git checkout myCurrentBranch

...git 抱怨:

error: Your local changes to the following files would be overwritten by checkout:
    app/.meteor/packages
    app/meteo4j.js
Please, commit your changes or stash them before you can switch branches.
Aborting

我不关心这些变化。如果它们是制造出来的,那是无意中发生的。是否可以强制 git 忽略这些更改并检查我当前的分支,而不以任何方式更改我的历史记录?

Is it possible to force git to ignore these changes and to checkout my current branch, without changing my history in any way?

是的,使用 git checkout -f <branchname> 来 "force" 进行签出,无论它是否会破坏任何未提交的更改。