現(xiàn)在公司仍然用的是SVN,究其原因可能是領(lǐng)導(dǎo)層認(rèn)為SVN比較“安全”,因?yàn)椋o你一個(gè)內(nèi)網(wǎng)IP你在除了公司外的地方無(wú)法對(duì)項(xiàng)目作出人和修改。但是現(xiàn)在看好多公司都在用git,覺(jué)得這個(gè)git一定有它的過(guò)人之處怯疤,所以就來(lái)學(xué)習(xí)一下吧!
一催束、 安裝
我們用的都是Mac,所以可以直接通過(guò)homebrew安裝Git集峦,具體方法請(qǐng)參考homebrew的文檔:http://brew.sh/
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
然后再檢查
$ git version // 判斷是否安裝成功,查看版本號(hào)
然后抠刺,設(shè)置你的個(gè)人信息
$ git config --global user.name "YourName"
$ git config --global user.email "YourEmail"
- 注意'git config'命令的'--global'參數(shù)塔淤,用了這個(gè)參數(shù),表示你這臺(tái)機(jī)器上所有的Git倉(cāng)庫(kù)都會(huì)使用這個(gè)配置速妖,當(dāng)然也可以對(duì)某個(gè)倉(cāng)庫(kù)指定不同的用戶名和Email地址凯沪。
二、基本使用
1买优、可以新建一個(gè)文件夾妨马,mkdir testGit
, 然后cd testGit
$ git init // 創(chuàng)建倉(cāng)庫(kù)
Initialized empty Git repository in /Users/testGit/.git/
2挺举、增加你需要改變的東西,放到testGit中去烘跺,添加到倉(cāng)庫(kù)中去
$ git add . // 注意 . 是增加所有的
3湘纵、提交到倉(cāng)庫(kù)中去 -m “這里面是需要注釋的內(nèi)容”
$ git commit -m "first init "
4、接下來(lái)就是看滤淳,你的遠(yuǎn)程倉(cāng)庫(kù)建在什么地方啦梧喷,我在github 和 coding 上都有,個(gè)人認(rèn)為剛開(kāi)始在coding 上使用還是不錯(cuò)的
$ git remote add origin git@coding.com....
假如此時(shí)遇到這個(gè)問(wèn)題脖咐,fatal: Authentication failed for铺敌,那么有可能是沒(méi)有添加ssh key導(dǎo)致的,具體可以參考這文章
Git SSH Key 生成步驟
5屁擅、把內(nèi)容推送到遠(yuǎn)程庫(kù)上
$ git push -u origin master //由于遠(yuǎn)程庫(kù)是空的偿凭,我們第一次推送master分支時(shí),加上了-u參數(shù)派歌,Git不但會(huì)把本地的master分支內(nèi)容推送的遠(yuǎn)程新的master分支弯囊,還會(huì)把本地的master分支和遠(yuǎn)程的master分支關(guān)聯(lián)起來(lái),在以后的推送或者拉取時(shí)就可以簡(jiǎn)化命令
$ git push origin master // 后期胶果,當(dāng)然在分支的情況匾嘱,另論
6、然后每次用之前
$ git pull origin master // 每次使用之前最好刷新一下
實(shí)際上現(xiàn)在到目前為止就可以開(kāi)始用起來(lái)啦早抠,當(dāng)然分支管理這個(gè)大部分我們后期在討論霎烙。
常用的命令
$ git status // 查看每次變化的
$ git log // 查看每次記錄的
$ git log --pretty=oneline // 查看記錄更清晰
$ git clone git@github.com:****.git // 從遠(yuǎn)程倉(cāng)庫(kù)克隆,常用
7蕊连、建立分支并切換
$ git branch 分支名字 // 建立分支
$ git checkout 分支名字 // 切換到分支
$ git branch // 查看分支
8吼过、合并分支
//先切換主支
$ git checkout master
$ git merge --no-ff 分支名字
三、理解
工作區(qū)-----倉(cāng)庫(kù)----遠(yuǎn)程倉(cāng)庫(kù)(git add; git commit ; git push )
四咪奖、常見(jiàn)問(wèn)題(陸續(xù)增加中···)
1(問(wèn))、合并的時(shí)候遇到的沖突
error: There was a problem with the editor 'vi'.
Not committing merge; use 'git commit' to complete the merge.
E325: ATTENTION
Found a swap file by the name ".git/.MERGE_MSG.swp"
owned by: eladb dated: Tue Aug 20 10:52:03 2013
file name: ~eladb/MyWorkspace/Client/.git/MERGE_MSG
modified: no
user name: eladb host name: Elads-MacBook-Pro-2.local
process ID: 29959 (still running)
While opening file ".git/MERGE_MSG"
dated: Tue Aug 20 10:53:11 2013
NEWER than swap file!
(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r .git/MERGE_MSG"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".git/.MERGE_MSG.swp"
to avoid this message.
Swap file ".git/.MERGE_MSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)born:
1(答)解決方法:
找到".git/.MERGE_MSG.swp"酱床,之后刪除即可羊赵,然后重新git add .;
git commit -m "";git push master 之后,再執(zhí)行 git merge branchName 就好啦
2 (問(wèn)) git push 出現(xiàn)的警告問(wèn)題
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior,use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
(答)解決:
git config --global push.default matching或者git config --global push.default simple命令扇谣,以后再push就不會(huì)有警告了昧捷。
下面說(shuō)一下push.default matching和push.default simple的區(qū)別:
push.default設(shè)置maching的意思是:git push 會(huì)把你本地所有分支push到名稱相對(duì)應(yīng)的遠(yuǎn)程主機(jī)上。這意味著可能你會(huì)在不經(jīng)意間push一些你原本沒(méi)打算push的分支罐寨。
push.default設(shè)置成simple的意思是:git push僅僅把當(dāng)前所在分支push到從當(dāng)初git pull pull下來(lái)的那個(gè)對(duì)應(yīng)分支上靡挥,另外,這個(gè)過(guò)程也會(huì)同時(shí)檢查各個(gè)分支的名稱是否相對(duì)應(yīng)鸯绿。
平常最好用 $ git push origin BRANCH #BRANCH是你遠(yuǎn)程分支的名字
相應(yīng)的 git pull
git pull origin BRANCH #BRANCH是你遠(yuǎn)程分支的名字
3 (問(wèn)) git merge 合并時(shí)遇到的沖突, Automatic merge failed; fix conflicts and then commit the result
$ git merge secondDev
Auto-merging myTest.txt
CONFLICT (content): Merge conflict in myTest.txt
Automatic merge failed; fix conflicts and then commit the result.
自動(dòng)合并失敗跋破。由于在同一行進(jìn)行了修改簸淀,所以產(chǎn)生了沖突。
在沖突處
add 把變更錄入到索引中
commit 記錄索引的狀態(tài)
pull 取得遠(yuǎn)端數(shù)據(jù)庫(kù)的內(nèi)容
然后重新提交
$ git add myTest.txt
$ git commit -m "合并secondDev分支"
# On branch master
nothing to commit (working directory clean)
五毒返、一張很好用git 的圖
參考鏈接(安裝中配置和使用的基本)
1租幕、基本的安裝及配置
2、詳細(xì)的講解和說(shuō)明??
3拧簸、git的使用規(guī)范
4劲绪、git的簡(jiǎn)單使用