作者CrBoy (上大學好忙喔)
看板Linux
標題Re: [問題] git 分支使用疑問
時間Fri Feb 3 00:54:09 2012
※ 引述《shihyuyao (shihyuyao)》之銘言:
: 分支的切換是只能local端嗎?
: git branch -r
: octopress/HEAD -> octopress/master
: octopress/configuration
: octopress/edge
: octopress/gh-pages
: octopress/master
: octopress/move_rakefile_configs
: octopress/rake_minify_js
: octopress/refactor_code_highlight
: octopress/refactor_deployment
: octopress/refactor_js
: octopress/rubygemcli
: octopress/site
: octopress/site-deploy-test
: octopress/subdir
: octopress/thor
: 假設我要切到 octopress/configuration 分支
: git checkout octopress/configuration
: 出現下面錯誤?
: error: You have local changes to 'Rakefile'; cannot switch branches.
: 還有 用 git clone 下載是預設下載遠端的 master的檔案嗎?
: 要如何下載分支的檔案?
: 謝謝
你的問題其實不在於是不是local端 而是因為你Rakefile有改過(local changes)
而你checkout其他branch的話 可能造成你這個修改不見
為了安全起見 git不允許你切換branch
想解決這個問題 你可以先commit local changes 這樣你的working copy就是clean的
不過通常不要為了暫時的切換亂commit比較好 有個好作法是利用 git stash
他會暫時把你現在的working copy存下來 然後讓working copy變成跟HEAD一樣
也就是clean的 你可以stash很多很多working copy 像stack一樣一個個疊上去
等你暫時要做的事情結束後 切回原來branch 再用 git stash pop把剛剛暫存的東西
復原回來就好了~
要注意你如果在HEAD=A的時候stash 然後在HEAD=B的時候pop
有可能造成conflict (就像merge的conflict一樣) 有點討厭
另外 切換到remote branch是可以的 你的用法沒有錯
不過要特別注意不能在remote branch上commit
一定要用local branch去merge他之後 才能commit
最後透過push才能把revision放到remote branch上
--
抱歉中英文夾雜的很嚴重...主要是git裡面的名詞我會用英文這樣....orz
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.165.96.114
※ 編輯: CrBoy 來自: 218.165.96.114 (02/03 00:55)
推 fjm31714:(worship) 02/03 10:34
→ legnaleurc:注意如果 stash pop 之後有 confilct 的話,該 stash 02/03 16:54
→ legnaleurc:不會自動被 drop, 解決 conflict 之後要自已手動 drop 02/03 16:54
推 shihyuyao:git clone 後用 git branch 只顯示master 是這預設? 02/06 02:58
→ shihyuyao:用git branch -a 會顯示出origin/aa,origin/bb ...etc 02/06 02:59
→ shihyuyao:這些origin/aa, bb, etc 分支是存在本端倉庫?還是遠端? 02/06 03:02
→ shihyuyao:因為git clone 不是會把所有東西拉回local? 02/06 03:02
推 shihyuyao:還是clone 預設只有拉回master分支到本端? 02/06 03:08
→ shihyuyao:其餘git branch -r 顯示的origin 都在server需透過網路? 02/06 03:09
→ shihyuyao:用git checkout -t 從server拉回到本端? 02/06 03:11
→ shihyuyao:還是這些git branch -r 遠端分支隱藏在local 倉庫裡面? 02/06 03:12
→ tsaiminghan:不知耶,抱歉了,我只會一般的checkin out切branch 04/07 17:43
→ tsaiminghan:回錯 04/07 17:44