2010-08-20 52 views
0

我是新來的混帳分支和我有點通過對具有結構轉換的分支行爲混淆。執行以下步驟:爲什麼當我切換分支時git自動合併更改?

git init 
Initialized empty Git repository in /Users/mads/Desktop/testing/.git/ 

echo 'hello a' > a.txt 
echo 'hello b' > b.txt 
git add *.txt 
git commit -m "Initial commit" 
[master (root-commit) 1ab870f] Initial commit 
2 files changed, 2 insertions(+), 0 deletions(-) 
create mode 100644 a.txt 
create mode 100644 b.txt 

git status 
# On branch master 
nothing to commit (working directory clean) 

git branch new_feature 
git checkout new_feature 
Switched to branch 'new_feature' 

ls 
a.txt b.txt 

echo "hello c" > c.txt 
git add c.txt 

echo "hello a, new version" > a.txt 

git status 
# On branch new_feature 
# Changes to be committed: 
# (use "git reset HEAD <file>..." to unstage) 
# 
# new file: c.txt 
# 
# Changed but not updated: 
# (use "git add <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# 
# modified: a.txt 
# 

git commit -m "Added new feature" 

[new_feature 1ccda31] Added new feature 
1 files changed, 1 insertions(+), 0 deletions(-) 
create mode 100644 c.txt 

git checkout master 
M a.txt 
Switched to branch 'master' 

cat a.txt 
hello a, new version 

爲什麼a.txt上的更改會在結帳時自動拉到主服務器上?這不是「合併」的目的嗎?我以爲工作的一個分支,然後切換到另一個人會被孤立......

回答

6

你從來沒有在修改之後跑git add a.txt。 Git不會自動跟蹤您對文件所做的更改,甚至不會自動跟蹤您已添加到存儲庫的文件。 (這是從SVN不同,例如)你必須明確地git add每次你改變它,如果你想進行版本的變化時每個文件。

或者,您可以使用-a選項git commit,該選項將自動添加並提交對git已在跟蹤的文件所做的任何更改。所以最接近svn commit的將是git commit -a

3

您沒有將a.txt添加到您的提交中,所以它仍然是修改的工作文件。當您結帳主人時,它不會覆蓋您的更改,因此在工作目錄中仍然存在對a.txt的更改。

2

您遇到的問題是更改爲未提交。

這就是M a.txt意味着當您再次簽出主人時。

如果你這樣做commit -a而不只是提交它會提交所有更改的文件。或者,您必須添加您想要提交的所有更改的文件。

如果一切都妥善承諾,你會看到,分支機構是很好的孤立。

相關問題