作者:亞里士朱德
原文地址:http://yalishizhude.github.io/2015/09/16/git/
從svn到git
兩者都是優(yōu)秀的版本管理工具(giter請(qǐng)不要鄙視svner)
- svn上手非常容易握童,操作簡(jiǎn)單姆怪。
- git功能強(qiáng)大,但是要熟練使用分支不算容易澡绩。
git因?yàn)槭欠植际降幕遥云浜诵木褪欠种Вㄖ挥衜aster分支情況下和svn差不多?)肥卡,分支的意義在于溪掀,可以將項(xiàng)目代碼按照功能、模塊拆分成不同的分支步鉴。比如這個(gè)產(chǎn)品要加一個(gè)支付功能和一個(gè)登陸功能膨桥,可以創(chuàng)建兩個(gè)分支,交給不同的開發(fā)人員并行開發(fā)唠叛。登陸功能先開發(fā)完,測(cè)試無誤后合并改分支到master分支沮稚,master分支部署上線艺沼。支付功能雖然沒有開發(fā)完成,但是在另一條分支上蕴掏,所以產(chǎn)品上線和功能開發(fā)完全不受影響障般。這才是分布式開發(fā)的高效模式。
被git坑了一個(gè)星期之后決心把官方文檔看一下盛杰,結(jié)合實(shí)踐經(jīng)驗(yàn)進(jìn)行了整理挽荡。
新手常見問題
1.如何查看有哪些分支?
git branch -a
2.如何強(qiáng)制檢出/切換分支或分支指定文件即供?
git checkout <branch> [file] [-f]
強(qiáng)制更新定拟,以branch版本的代碼為主。
3.提交代碼出現(xiàn)沖突沖突怎么辦逗嫡?
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
先切換分支青自,然后拉取分支上最新的代碼覆蓋到本地;
git pull
添加或者提交代碼驱证,解決沖突之后延窜。
git push
4.如何新建分支
git checkout -b <branch_name>
本地建立 branch 並立即切換到新分支;
git branch -m <new_name>
修改分支名稱抹锄。
5.從遠(yuǎn)程倉庫拉取代碼到本地倉庫逆瑞,并建立跟蹤關(guān)系
git clone http://xxx.git
或者
get clone git@xxx.git
然后
git checkout -b <本地新分支名> <對(duì)應(yīng)的遠(yuǎn)程分支名>
6.遠(yuǎn)程倉庫新建了一個(gè)分支荠藤,如何更新遠(yuǎn)程分支信息
git fetch <remote base>
7.如何在遠(yuǎn)程倉庫新建一個(gè)分支
git branch <branch name>
新建一個(gè)本地分支,按照正常流程提交完代碼后获高,推送到遠(yuǎn)程
git push <remote base> <local branch>:<remote branch>
實(shí)用指令
reset
git reset [file]
取消暫存
remote
查看遠(yuǎn)程倉庫名
git remote -v
查看遠(yuǎn)程倉庫url
git remote add <basename> <url>
新增遠(yuǎn)程倉庫
git remote show <basename>
查看遠(yuǎn)程倉庫詳細(xì)信息
git remote rename <old basename> <new basename>
重命名遠(yuǎn)程倉庫
pull
相當(dāng)于fetch和merge
push
git push [remote_branch] [local_branch]
推送本地倉庫代碼到遠(yuǎn)程倉庫哈肖,相當(dāng)于svn的commit
git push <remote base> [tag name]
推送本地標(biāo)簽到遠(yuǎn)程倉庫
git push <remote base> <remote branch>:<local branch>
將本地分支推送到指定的遠(yuǎn)程分支
git push <remote base> --delete <remote branch>
刪除遠(yuǎn)程分支
tag
查看標(biāo)簽(用來標(biāo)記標(biāo)志性的穩(wěn)定版本信息)
git tag -l '[expression]'
查看那符合正則表達(dá)式的
git tag -a <tag name> -m <comment>
添加帶注釋的標(biāo)簽
git tag -a <tag name> <md5>
對(duì)某個(gè)版本打標(biāo)簽
git tag [tag name]
如果沒有標(biāo)簽名,則為查看所有標(biāo)簽谋减。帶標(biāo)簽名則為新建標(biāo)簽
merge
git merge <branch name>
將其他分支合并到本分支
commit
git commit -a -m 'xx'
暫存并提交
branch
git branch
查看本地倉庫分支
git branch -v
查看本地倉庫分支最后一次提交情況
git branch -vv
查看分支跟蹤情況
git branch <branch name>
新建分支
git branch -d <branch name>
刪除分支
git branch [--merged | --no-merged]
查看已合并|未合并的本地倉庫分支
git branch -u <remote base>/<remote branch>
修改當(dāng)前跟蹤分支
commit
git commit -a -m 'xx'
提交并且暫存暫存的方法
checkout
git checkout -- [file]
恢復(fù)文件
git checkout -b [branchname] [tagname]
在特定的版本上創(chuàng)建一個(gè)新的分支并切換到此分支
git checkout -b [local branch] [remote base]/[remote branch]
將遠(yuǎn)程分支檢出到本地分支
git checkout --track <remote base>/<remote branch>
讓當(dāng)前分支跟蹤遠(yuǎn)程分支
git checkout --track <remote base>/<remote branch>
git checkout -b <local branch> <remote base>/<remote branch>
讓當(dāng)前分支跟蹤到遠(yuǎn)程分支牡彻。兩條命令作用基本一致,不同的是第二條命令可以重命名檢出的分支出爹。
rebase
git rebase [basebranch]
變基是將一系列提交按照原有次序依次應(yīng)用到另一分支上庄吼,而合并是把最終結(jié)果合在一起。
小技巧
查看配置
git config -1
設(shè)置git push 默認(rèn)
git config --global push.default current
設(shè)置別名
git config --global alias.<name> <commend>
我的設(shè)置:
git config --global alias.st status
git config --global alias.cm "commit -m"
git config --global alias.ph "push origin <local_repository>:<remote_repository>"
保存用戶名和密碼
對(duì)于http(s)協(xié)議严就,可以用下面命令臨時(shí)緩存
git config --global credential.helper cache
開啟linux緩存
git config --global credential.helper wincred
開啟windows緩存
對(duì)于ssh協(xié)議总寻,可以用ssh key,具體教程網(wǎng)上很多
忽略文件
默認(rèn)方法是在當(dāng)前項(xiàng)目目錄下創(chuàng)建一個(gè).gitignore
文件梢为,如果需要忽略的文件已經(jīng)添加到版本庫中渐行,請(qǐng)先移除git rm --cached [file]
不刪除文件,只移除追蹤铸董。