轉(zhuǎn)載至:https://www.cnblogs.com/fanyegong/p/5127780.html
一、定義:以下均使用括號中的簡稱
工作區(qū):workspace (W)
暫存區(qū):index (I)
本地倉庫:local repository (L)
遠程倉庫:remote repository (R)
二、常用命令:
1.初始化
git init
2.常用配置信息
(如果不使用--global,則只設(shè)置當(dāng)前項目的配置信息)
設(shè)置代理:
git config --global http.proxy http://xxx.com:port
取消代理:
git config --global --unset http.proxy
設(shè)置用戶名:
git config --global user.name "[name]" ?
設(shè)置郵箱:?
git config --global user.email?"[email address]"?
設(shè)置命令行顏色:
git config --global color.ui auto
查看用戶名:
git config --global user.name
配置crlf:
git config --global core.autocrlf [true/false/input]
(在windows下設(shè)置為true,則提交的內(nèi)容是LF,本地是CRLF)
配置文件名大小寫敏感:(git默認不區(qū)分文件名大小寫)
(可以新增文件,但是之前文件還沒有刪除)?git config core.ignorecase false?
(可以嘗試)?git mv --force myfile myFile
3.添加--從工作區(qū)W到暫存區(qū)I
添加單個文件/多個文件/目錄:
git add [file1] [file2] [directory]?
添加新增和修改的文件,不做刪除操作:
git add .?
添加修改和刪除的文件,不做新增操作:
git add -u
添加增刪改的文件:等于git add .;git add -u; ?
git add -A
4.提交:
(1)從暫存區(qū)I到本地倉庫L
git commit -m [提交信息]
(2)
5.撤銷
(1)從暫存區(qū)I到工作區(qū)W
git rm --cached <file> ...
(2)恢復(fù)到HEAD版本
git reset --hard HEAD
(3)修改提交的用戶名信息
git commit --amend --author="Author Name <email@address.com>"
6.遠程
- 更改遠程倉庫地址
git remote set-url origin?http://xxx.xx.com/x.git
- 查看遠程倉庫地址
git remote show origin
- 本地項目與遠程git地址進行關(guān)聯(lián),比如項目初始化,新建的項目可以使用
git remote add origin https://xxx.xxx.xxx.xxx/xxx/xxx-wechat-web.git
- 查看遠程倉庫日志
git log origin/master
(3)比較本地分支和遠程分支
git diff master origin/master
(4)查看遠程分支
git branch -r
(5)拉取遠程
方法1:拉取并合并
git pull
拉取遠端指定分支代碼:
git pull origin 分支名稱
方法2:先拉取后合并
git fetch origin
git merge origin/master
(6)查看本地分支和遠程分支的差異
git diff master origin/master
(7)基于遠程分支創(chuàng)建本地分支
git checkout -b feature origin/feature
7.分支
查看分支:git branch
查看遠程分支的本地copies:git branch -r
查看遠程分支:?git ls-remote
查看所有分支:?git branch -a
創(chuàng)建分支:git branch <origin/name>
切換分支:git checkout <name>
創(chuàng)建+切換分支:git checkout -b <name> <origin/name>
合并某分支到當(dāng)前分支:git merge <name>
刪除分支:git branch -d <name>
刪除遠程分支:?git push origin --delete
獲取遠程分支:?git fetch
更新遠程分支的本地copies:?git fetch --prune origin
刪除不存在的遠程分支的信息:git remote prune origin
8.儲藏
保存:git stash
查看stash列表:git stash list
恢復(fù):git stash apply
恢復(fù)特定版本:git stash apply stash@{2}
清除某個stash記錄:git stash drop stash@{0}
9.引用
(1)查看提交的哈希字串
git log
git log --pretty=oneline
查看歷史中有xxx的
git log -G"刪除掉的內(nèi)容" -p
git log -S"刪除掉的內(nèi)容" -p
git log --grep "刪除掉的內(nèi)容"
git log -p | grep "刪除掉的內(nèi)容"
(2)查看某個提交
git show 0c708f(哈希)
(3)查看分支、標簽或間接引用的哈希
git rev-parse master
(4)查看引用
在.git/refs目錄下
git show refs/heads/some-feature
在.git目錄下還有一些引用:
HEAD – 當(dāng)前所在的提交或分支。
FETCH_HEAD – 遠程倉庫中fetch到的最新一次提交。
ORIG_HEAD – HEAD的備份引用,避免損壞。
MERGE_HEAD – 你通過git merge并入當(dāng)前分支的引用(們)。
CHERRY_PICK_HEAD – 你cherry pick使用的引用。
(5)refspec
refspec的定義是這樣的:[+]<src>:<dst>。<src>參數(shù)是本地的源分支,<dst>是遠程的目標分支??蛇x的+號強制遠程倉庫采用非快速向前的更新策略。
(6)相對引用
git show HEAD~2?祖父節(jié)點
git show HEAD^2?第二個父節(jié)點(分支合并時,原所在分支是第一個父節(jié)點)
git show HEAD^2^1?可以多層
(7)引用日志
git reflog
gitcheckoutHEAD@{1}
10.代碼回滾
reset checkout revert的選擇
reset checkout 可以指定文件或某次提交,revert只針對提交,不針對文件
checkout revert 有可能會重寫文件,在使用前要提交或緩存工作目錄的修改
(1)reset(僅僅用在私有分支上,一般只用HEAD)
git checkout feature
git reset HEAD?(末端兩個提交變成懸掛提交,git垃圾回收時會被刪除)
git reset --hard HEAD
--soft – 緩存區(qū)和工作目錄都不會被改變
--mixed – 默認選項。緩存區(qū)和你指定的提交同步,但工作目錄不受影響
--hard – 緩存區(qū)和工作目錄都同步到你指定的提交
git reset HEAD file1.js?將HEAD提交中的該文件加入到緩存區(qū),針對文件沒有--mixed之類的參數(shù)
(2)checkout
git checkout feature 切換分支
git checkout HEAD~2?把HEAD移動到特定提交,但會造成HEAD分離,非常危險,如果你接著添加新的提交,然后切換到別的分支之后就沒辦法回到之前添加的這些提交。因此,在為分離的HEAD添加新的提交的時候你應(yīng)該創(chuàng)建一個新的分支。
git checkout HEAD~2 file1.js?將工作目錄的該文件同步到HEAD~2
(和git reset HEAD --hard很像,但只影響特定文件)
(3)revert(可以用在公共分支,不會影響歷史,創(chuàng)建一個新的提交來撤銷某個提交的修改)
git checkout feature
git revert HEAD~2?
11.打標簽
(1)查看當(dāng)前標簽
git tag
(2)打標簽
git tag -a v1.0.0 -m "my version v1.0.0"
(3)查看某個tag
git show v1.0.0
(4)刪除某個tag
git tag -d v1.0.0
(4)把tag推送到遠程
git push origin --tags
12.比較
git diff?工作區(qū)(W)跟暫存區(qū)(I)的區(qū)別(使用 f 翻下一頁,使用b 翻上一頁)
git diff HEAD工作區(qū)(W)跟本地倉庫(L)的區(qū)別
git diff --staged?暫存區(qū)(I)跟本地倉庫(L)的區(qū)別
(后邊跟上文件名則只對比某個文件)
提交前比較某個文件
git diff -- myfile.js
git diff --cached myfile.js
比較時忽略空格變化
git diff -w (--ignore-all-space)
使用圖形化工具查看比較
gitk &
設(shè)置圖形化工具的編碼
git config --global gui.encoding utf-8
13.打包
git archive --format zip --output /path/to/file.zip master
14.分支的衍合
git checkout feature
git rebase master
解決沖突后?git add .
git rebase --continue
15.只合并某個提交或者某個文件
git cherry-pick?<commit id>? 合并某一個提交
git cherry-pick -x <commit id> 合并某一個提交,并保留原提交的信息
git cherry-pick <start commit id>..<end commit id> 合并某個區(qū)間的提交,左開右閉
git cherry-pick <start commit id>^..<end commit id> 合并某個區(qū)間的提交,閉區(qū)間
git checkout branchname -- <paths>
git merge --no-ff --no-commit branchname ?(不快速向前,不提交)
16.git 免重復(fù)登錄方式
git本身支持SSH方式,在此不具體介紹了。
git支持credential的方式,在win7下可以設(shè)置credential helper為wincred,可以在"控制面板"--"用戶賬戶"--"憑據(jù)管理器" 里查看憑據(jù)。

設(shè)置和取消憑證管理的方式:
git config --global --unset credential.helper
git config --global credential.helper wincred
17.git 設(shè)置命令別名
git config --global alias.co checkout?
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.lg "log --color --gragh --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(blod blue)<%an>%Creset' --abbrev-commit"
參考地址:http://blog.csdn.net/zhang31jian/article/details/41011313
18.git 判斷代碼最初是由誰提交的
git blame <filename>
git blame -L 100,100 <filenname>
git blame -L 100,+10 <filename>
三、常用場景:
1.創(chuàng)建新的庫
mkdir tutorial
cd tutorial
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin http://xxx.xx.com/x.git
git push -u origin master
2.push已經(jīng)存在的庫
cd existing_repo
git remote add origin http://xxx.xx.com/x.git
git push -u origin master
3.merge request
git fetch origin
git checkout -b request-branch origin/request-branch
git checkout master
git merge --no-ff request-branch
git push origin master
?默認情況下,Git執(zhí)行"快進式合并"(fast-farward merge),會直接將Master分支指向Develop分支。
使用--no-ff參數(shù)后,會執(zhí)行正常合并,在Master分支上生成一個新節(jié)點。為了保證版本演進的清晰,我們希望采用這種做法。
4.github quick setup
(1)https://github.com/username/gittest.git
git@github.com:username/gittest.git
(2)create a new repository on the command line:
echo "# gittest" >> README.md
git init
git add REAME.md
git commit -m "first commit"
git remote add origin git@github.com:username/gittest.git
git push -u origin master
(3)push an existing repository from the command line
git remote add origin git@github.com:username/gittest.git
git push -u origin master
(4)import code from another repository
You can initialize the repository with code from a Subversion,Mercurial, or TFS project.
5.gitflow
參考鏈接:http://www.cnblogs.com/cnblogsfans/p/5075073.html
可以使用GUI工具SourceTree(http://blog.csdn.net/victor_barnett/article/details/51211282)
git flow init之后,配置文件和hooks分別在.git/config ?.git/hooks/
初始化:git flow init
開始新Feature:git flow feature start MYFEATURE
Publish一個Feature(也就是push到遠程):git flow feature publish MYFEATURE
獲取Publish的Feature:git flow feature pull origin MYFEATURE
完成一個Feature:git flow feature finish MYFEATURE
開始一個Release:git flow release start RELEASE [BASE]
Publish一個Release:git flow release publish RELEASE
發(fā)布Release:git flow release finish RELEASE
別忘了git push --tags
開始一個Hotfix:git flow hotfix start VERSION [BASENAME]
發(fā)布一個Hotfix:git flow hotfix finish VERSION

