考慮下面的 「故事」:工作目錄如何在「git checkout」上更新?
$ 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
爲什麼hello.txt
對分支主兩行呢? (我認爲git checkout
將恢復工作目錄到以前的狀態,即hello.txt
將只有一條線。)
幕後實際發生的工作目錄上git checkout
什麼?它如何更新?
顯然,git默認將當前工作目錄合併到新分支(_master_在我的情況下):http://www.gitguys.com/topics/switching-branches-without-committing –
如果文件被添加到該索引但未提交。 –
我認爲分支只存儲提交,而不是工作改變。當您切換到分支並執行某些工作時,您需要在切換到其他分支之前提交這些更改,否則這些更改將不會與該分支相關聯。 處理分支的另一種方法是將您的存儲庫克隆到新目錄中。然後,您可以在兩個不同目錄中的兩個分支上工作,而無需提交,直到準備就緒。這使得更容易避免錯誤。 –