如何在"git checkout"上更新工作目录?

Mis*_*hko 14 git git-checkout git-branch

考虑以下"故事":

$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in /home/misha/misha/my_project/.git/

$ echo "first line" > hello.txt
$ git add hello.txt
$ git commit -m "first commit"
[master (root-commit) 9c913a1] first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 hello.txt

$ git branch new_feature
$ git checkout new_feature
Switched to branch 'new_feature'
$ echo "second line" >> hello.txt
$ cat hello.txt
first line
second line

$ git checkout master
M    hello.txt
Switched to branch 'master'
$ cat hello.txt
first line
second line
Run Code Online (Sandbox Code Playgroud)

为什么hello.txt分支主管上有两行?(我认为这git checkout会将工作目录恢复到之前的状态,即hello.txt只有一行.)

幕后实际发生在工作目录上的是git checkout什么?它是如何更新的?

scu*_*ube 17

您的git checkout to master可以防止您丢失未提交的更改.这就是为什么你的文件仍然有你的第二行hello.txt.如果您确实想要丢失未提交的更改,则必须使用该-f参数.

最后,您的结帐将如下所示:

git checkout -f master
Run Code Online (Sandbox Code Playgroud)

  • 显然,git默认将当前工作目录合并到新分支中(在我的情况下为_master_):http://www.gitguys.com/topics/switching-branches-without-committing (2认同)