開始:
1) 遠(yuǎn)程倉庫相關(guān)命令
檢出倉庫:$ git clone git://github.com/jquery/jquery.git
查看遠(yuǎn)程倉庫:$ git remote -v
添加遠(yuǎn)程倉庫:$ git remote add [name] [url]
刪除遠(yuǎn)程倉庫:$ git remote rm [name]
修改遠(yuǎn)程倉庫:$ git remote set-url --push[name][newUrl]
拉取遠(yuǎn)程倉庫:$ git pull [remoteName] [localBranchName]
推送遠(yuǎn)程倉庫:$ git push [remoteName] [localBranchName]
2)分支(branch)操作相關(guān)命令
查看本地分支:$ git branch
查看遠(yuǎn)程分支:$ git branch -r
創(chuàng)建本地分支:$ git branch [name] ----注意新分支創(chuàng)建后不會(huì)自動(dòng)切換為當(dāng)前分支
切換分支:$ git checkout [name]
創(chuàng)建新分支并立即切換到新分支:$ git checkout -b [name]
刪除分支:$ git branch -d [name] ---- -d選項(xiàng)只能刪除已經(jīng)參與了合并的分支兔朦,對(duì)于未有合并的分支是無法刪除的枣察。如果想強(qiáng)制刪除一個(gè)分支隔显,可以使用-D選項(xiàng)
合并分支:$ git merge [name] ----將名稱為[name]的分支與當(dāng)前分支合并
創(chuàng)建遠(yuǎn)程分支(本地分支push到遠(yuǎn)程):$ git push origin [name]
刪除遠(yuǎn)程分支:$ git push origin :heads/[name]
我從master分支創(chuàng)建了一個(gè)issue5560分支,做了一些修改后拔恰,使用git push origin master提交抹沪,但是顯示的結(jié)果卻是'Everything up-to-date',發(fā)生問題的原因是git push origin master 在沒有track遠(yuǎn)程分支的本地分支中默認(rèn)提交的master分支,因?yàn)閙aster分支默認(rèn)指向了origin master 分支颊郎,這里要使用git push origin issue5560:master 就可以把issue5560推送到遠(yuǎn)程的master分支了。
如果想把本地的某個(gè)分支test提交到遠(yuǎn)程倉庫亭枷,并作為遠(yuǎn)程倉庫的master分支袭艺,或者作為另外一個(gè)名叫test的分支,那么可以這么做叨粘。
git push origin test:master? ? ? ? // 提交本地test分支作為遠(yuǎn)程的master分支 //好像只寫這一句猾编,遠(yuǎn)程的github就會(huì)自動(dòng)創(chuàng)建一個(gè)test分支
git push origin test:test? ? ? ? ? ? ? // 提交本地test分支作為遠(yuǎn)程的test分支
如果想刪除遠(yuǎn)程的分支呢?類似于上面升敲,如果:左邊的分支為空答倡,那么將刪除:右邊的遠(yuǎn)程的分支。
git push origin :test? ? ? ? ? ? ? // 剛提交到遠(yuǎn)程的test將被刪除驴党,但是本地還會(huì)保存的瘪撇,不用擔(dān)心
3)版本(tag)操作相關(guān)命令
查看版本:$ git tag
創(chuàng)建版本:$ git tag [name]
刪除版本:$ git tag -d [name]
查看遠(yuǎn)程版本:$ git tag -r
創(chuàng)建遠(yuǎn)程版本(本地版本push到遠(yuǎn)程):$ git push origin [name]
刪除遠(yuǎn)程版本:$ git push origin :refs/tags/[name]
4) 子模塊(submodule)相關(guān)操作命令
添加子模塊:$ git submodule add [url] [path]
如:$ git submodule add git://github.com/soberh/ui-libs.git src/main/webapp/ui-libs
初始化子模塊:$ git submodule init ----只在首次檢出倉庫時(shí)運(yùn)行一次就行
更新子模塊:$ git submodule update ----每次更新或切換分支后都需要運(yùn)行一下
刪除子模塊:(分4步走哦)
1)$ git rm --cached [path]
2) 編輯“.gitmodules”文件,將子模塊的相關(guān)配置節(jié)點(diǎn)刪除掉
3) 編輯“.git/config”文件港庄,將子模塊的相關(guān)配置節(jié)點(diǎn)刪除掉
4) 手動(dòng)刪除子模塊殘留的目錄
5)忽略一些文件倔既、文件夾不提交
在倉庫根目錄下創(chuàng)建名稱為“.gitignore”的文件,寫入不需要的文件夾名或文件鹏氧,每個(gè)元素占一行即可渤涌,如
target
bin
*.db
? ? Git配置
1 git config --global user.name "huaqianlee"?
2 git config --global user.email "huaqianlee@gmail.com"
3 git config --global color.ui true
4 git config --global alias.co checkout # 配置別名,co 配為checkout 別名把还, 不過我沒用
5 git config --global alias.ci commit
6 git config --global alias.st status
7 git config --global alias.br branch
8 git config --global core.editor "mate -w"? ? # 設(shè)置Editor使用textmate
9 git config -l? # 列舉所有配置
? ? 用戶的git配置文件~/.gitconfig
--------------------------------------------------------------------------------------------------------
? ? Git常用及進(jìn)階命令
--------------------------------------------------------------------------------------------------------
? ? 常用命令
1 git help? #查看幫助实蓬,命令忘記了就靠它
2 git init? ? #初始化話目錄為git倉庫
3 git clean -fd? # 刪除無用目錄及文件
4 git clean -fX? # 刪除無用文件
5 git clean # 刪除所有untracked文件
? ? 添加
1 git add file? #添加文件到暫存區(qū)
2 git add .? ? ? ? #將所有修改或者新加文件添加到暫存區(qū)
3 git add -A? ? # 添加所有文件(除刪除的文件,自動(dòng) git rm)
? ? 刪除
1 git rm file? ? ? ? #刪除文件
2 git rm <file> --cached? # 從版本庫中刪除文件,但不刪除文件
? ? 撤銷回退
1 git checkout -- xx? #撤銷xx文件修改
2 git checkout .? ? #撤銷工作區(qū)修改
3 git checkout ${commit} /path/to/file #撤銷指定文件到指定版本
4 git revert <$id>? ? # 恢復(fù)某次提交的狀態(tài)吊履,恢復(fù)動(dòng)作本身也創(chuàng)建了一次提交對(duì)象
5 git revert HEAD? ? # 恢復(fù)最后一次提交的狀態(tài)
6 git reset <file>? ? # 從暫存區(qū)恢復(fù)某一文件
7 git reset -- .? ? ? # 從暫存區(qū)恢復(fù)所有文件
8 git reset? –hard HEAD^/HEAD~? #回退到上一版本
9 git reset? –hard <commit_id>? ? #回退到指定版本
10 git reset HEAD file? #取消add文件
? ? 提交
1 git commit? <file> #提交單個(gè)文件
2 git commit –m “description”? #提交暫存區(qū)到服務(wù)器
3 git commit -a? ? ? ? ? # 等同執(zhí)行g(shù)it add安皱、 git rm及git commit
4 git commit -am "some comments"
5 git commit --amend? ? ? # 修改最后一次提交記錄
? ? 查看狀態(tài)記錄
1 git status? ? ? ? #查看倉庫狀態(tài)
2 git show ($id)? # 顯示某次提交的內(nèi)容
3 git log? (file)? ? ? #查看(文件)提交記錄
4 git log -p <file>? # 查看每次詳細(xì)修改內(nèi)容的diff
5 git log -p -2? ? ? # 查看最近兩次詳細(xì)修改內(nèi)容的diff
6 git log --stat? ? ? # 查看提交統(tǒng)計(jì)信息
7 git reflog? ? ? #查看歷史版本號(hào)
8 git log -g #同上,用'log'格式輸出
9 git log -- grep "name" # 搜索包含name的log
10 git log record-ID? -l -p #查看指定ID記錄艇炎,-l:顯示一行酌伊,-p:顯示詳細(xì)修改
? ? 查看差異
1 git diff <file>? ? # 比較當(dāng)前文件和暫存區(qū)文件差異
2 git diff? #比較所有文件
3 git diff master..Andylee-Github/master #比較本地和遠(yuǎn)端倉庫
4 git diff <$id1> <$id2>? # 比較兩次提交之間的差異
5 git diff <branch1>..<branch2> #比較分支
6 git diff --staged? # 比較暫存區(qū)和版本庫差異
7 git diff --cached? # 比較暫存區(qū)和版本庫差異
8 git diff --stat? ? # 僅僅比較統(tǒng)計(jì)信息
? ? 本地分支管理
1 git branch? #查看本地分支
2 git branch? -r? ? # 查看遠(yuǎn)程分支
3 git branch? -a? ? #查看包括遠(yuǎn)程文件在內(nèi)的所有分支
4 git branch? <new_branch> # 創(chuàng)建新分支
5 git branch? -v? ? ? ? ? # 查看各個(gè)分支最后提交信息
6 git branch? --merged? ? # 查看已經(jīng)被合并到當(dāng)前分支的分支
7 git branch? --no-merged? # 查看尚未被合并到當(dāng)前分支的分支
8 git checkout <branch>? #切換分支
9 git checkout –b <new_branch>#創(chuàng)建新分支,并切換到新分支
10 git merge dev? ? #在當(dāng)前的分支上合并dev分支
11 git checkout -b <new_branch> <branch>? # 基于branch創(chuàng)建新的new_branch
12 git checkout? $id? ? ? ? ? # 把某次歷史提交記錄checkout出來冕臭,但無分支信息腺晾,切換到其他分支會(huì)自動(dòng)刪除
13 git checkout? $id -b <new_branch>? # 把某次歷史提交記錄checkout出來,創(chuàng)建成一個(gè)分支
14 git branch-d <branch>? # 刪除分支
15 git branch-D <branch>? # 強(qiáng)制刪除分支 (未被合并的分支被刪除的時(shí)需要強(qiáng)制)
16 git merge <branch>? ? ? ? ? ? ? # 將branch分支合并到當(dāng)前分支
17 git merge origin/master --no-ff? # 不要Fast-Foward合并辜贵,這樣可以生成merge提交
18 git rebase master <branch>? ? ? # 將master rebase到branch,等同于:
19 #git checkout? <branch> + git rebase master + git checkout? master + git merge <branch>
? ? 補(bǔ)丁應(yīng)用
1 git diff > ../sync.patch? ? ? ? # 生成補(bǔ)丁
2 git apply ../sync.patch? ? ? ? ? # 打補(bǔ)丁
3 git apply --check ../sync.patch? # 測試補(bǔ)丁能否成功
? ? 暫存管理
1 git stash? #暫存當(dāng)前工作归形,恢復(fù)現(xiàn)場后可繼續(xù)工作
2 git stash list? #查看暫存文件列表
3 git stash apply? #恢復(fù)暫存內(nèi)容托慨,暫存區(qū)不刪除
4 git stash drop? #刪除暫存文件
5 git stash pop? #恢復(fù)并刪除文件
? ? 遠(yuǎn)程分支管理
1 git pull? ? ? ? ? ? ? ? ? ? ? ? # 抓取遠(yuǎn)程倉庫所有分支更新并合并到本地
2 git pull --no-ff? ? ? ? ? ? ? ? # 抓取遠(yuǎn)程倉庫所有分支更新并合并到本地,不要快進(jìn)合并
3 git fetch origin? ? ? ? ? ? ? ? # 抓取遠(yuǎn)程倉庫更新暇榴,加下一條指令等同于git pull
4 git merge origin/master? ? ? ? ? # 將遠(yuǎn)程主分支合并到本地當(dāng)前分支
5 git checkout? --track origin/branch? ? # 跟蹤某個(gè)遠(yuǎn)程分支創(chuàng)建相應(yīng)的本地分支
6 git checkout? -b <local_branch> origin/<remote_branch>? # 基于遠(yuǎn)程分支創(chuàng)建本地分支厚棵,功能同上
7 git push? ? ? ? ? ? ? ? ? ? ? ? # push所有分支
8 git push origin branch? # 將本地分支推到遠(yuǎn)程分支
9 git push –u origin branch? #推送本地分支到遠(yuǎn)程倉庫蕉世,首次提交需要加-u
10 git push origin <local_branch>? # 創(chuàng)建遠(yuǎn)程分支, origin是遠(yuǎn)程倉庫名
11 git push origin <local_branch>:<remote_branch>? # 創(chuàng)建遠(yuǎn)程分支
12 git push origin :<remote_branch>? #先刪除本地分支(git br -d <branch>)婆硬,然后再push刪除遠(yuǎn)程分支
? ? 遠(yuǎn)程倉庫管理
1 git remote? #查看遠(yuǎn)程庫的信息
2 git remote –v? #查看遠(yuǎn)程庫地址和名稱
3 git remote show origin? ? ? ? ? # 查看遠(yuǎn)程服務(wù)器倉庫狀態(tài)
4 git remote add origin git@github:robbin/robbin_site.git? ? ? ? # 添加遠(yuǎn)程倉庫地址
5 git remote set-url origin git@github.com:robbin/robbin_site.git # 設(shè)置遠(yuǎn)程倉庫地址(用于修改遠(yuǎn)程倉庫地址)
6 git remote rm <repository>? ? ? # 刪除遠(yuǎn)程倉庫
7 git clone https://github.com/AndyLee-Github/cartboon.git? #從遠(yuǎn)程倉庫中克隆
8 git clone --bare robbin_site robbin_site.git? # 用帶版本的項(xiàng)目創(chuàng)建純版本倉庫
9 scp -r my_project.git git@git.csdn.net:~? ? ? # 將純倉庫上傳到服務(wù)器上
10 mkdir robbin_site.git + cd robbin_site.git + git --bare init # 在服務(wù)器創(chuàng)建純倉庫
11 git remote add origin git@github.com:robbin/robbin_site.git? ? # 設(shè)置遠(yuǎn)程倉庫地址
12 git push -u origin master? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 客戶端首次提交
13 git push -u origin develop? # 首次將本地develop分支提交到遠(yuǎn)程develop分支狠轻,并且track
14 git remote set-head origin master? # 設(shè)置遠(yuǎn)程倉庫的HEAD指向master分支
? ? 命令設(shè)置跟蹤遠(yuǎn)程庫和本地庫
git branch --set-upstream-to=origin/<branch>?? master
設(shè)置本地分支master追蹤origin/<branch>遠(yuǎn)程分支
? ? 命令行新建倉庫
1 echo "# hexo-theme" >> README.md
2 git init
3 git add README.md
4git commit -m "first commit"
5 git remote add origin git@github.com:huaqianlee/hexo-theme.git
6 git push -u origin master
? ? 推送一個(gè)已存在的倉庫到遠(yuǎn)端
1 git remote add origin git@github.com:huaqianlee/hexo-theme.git
2 git push -u origin master