常用的Git命令
配置全局的用戶名和郵箱
????git config --global user.name "username"
????git config --global user.email "email"
查看全局的用戶名和郵箱
????git config --global user.name
????git config --global user.email
Git記住遠(yuǎn)程賬號(hào)密碼
設(shè)置記住密碼(默認(rèn)15分鐘):
????git config --global credential.helper cache
如果想自己設(shè)置時(shí)間洗显,可以這樣做:
????git config credential.helper 'cache --timeout=3600'
這樣就設(shè)置一個(gè)小時(shí)之后失效
長(zhǎng)期存儲(chǔ)密碼(這句):
????git config --global credential.helper store
克隆遠(yuǎn)程項(xiàng)目
????git clone? "遠(yuǎn)程庫(kù)地址"
重置遠(yuǎn)程賬號(hào)
????git config --system --unset credential.helper
查看本地分支
????git branch
查看遠(yuǎn)程分支
????git branch -a
切換遠(yuǎn)程分支
????git checkout -b future origin/future
Git更改分支名稱
????git branch -m old_branch new_branch # Rename branch locally git push origin :old_branch # Delete the old branch git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
一外潜、概述
先用一幅圖,從總體上描述主要git命令的工作流程
workspace: 本地的工作目錄挠唆。(記作A)
index:緩存區(qū)域处窥,臨時(shí)保存本地改動(dòng)。(記作B)
local repository: 本地倉(cāng)庫(kù)玄组,只想最后一次提交HEAD滔驾。(記作C)
remote repository:遠(yuǎn)程倉(cāng)庫(kù)。(記作D)
二俄讹、命令筆記
以下所有的命令的功能說(shuō)明哆致,都采用上述的標(biāo)記的A、B颅悉、C沽瞭、D的方式來(lái)闡述。
初始化
git init?//創(chuàng)建
git clone /path/to/repository?//檢出
git config --global user.email "you@example.com"?//配置email
git config --global user.name "Name"?//配置用戶名
操作
????git add <file>?// 文件添加剩瓶,A → B
????git add .?// 所有文件添加驹溃,A → B
????git commit -m "代碼提交信息"?//文件提交,B → C
????git commit --amend?//與上次commit合并, *B → C
????git push origin master?//推送至master分支, C → D
????git pull?//更新本地倉(cāng)庫(kù)至最新改動(dòng)延曙, D → A
????git fetch?//抓取遠(yuǎn)程倉(cāng)庫(kù)更新豌鹤, D → C
????git log?//查看提交記錄
????git status?//查看修改狀態(tài)
????git diff//查看詳細(xì)修改內(nèi)容
????git show//顯示某次提交的內(nèi)容
撤銷操作
????git reset <file>//某個(gè)文件索引會(huì)回滾到最后一次提交, C → B
????git reset//索引會(huì)回滾到最后一次提交枝缔, C → B
????git reset --hard?// 索引會(huì)回滾到最后一次提交布疙, C → B → A
????git checkout?// 從index復(fù)制到workspace, B → A
????git checkout -- files?// 文件從index復(fù)制到workspace愿卸, B → A
????git checkout HEAD -- files?// 文件從local repository復(fù)制到workspace灵临, C → A
分支相關(guān)
????git checkout -b branch_name?//創(chuàng)建名叫“branch_name”的分支,并切換過(guò)去
????git checkout master?//切換回主分支
????git branch -d branch_name?// 刪除名叫“branch_name”的分支
????git push origin branch_name?//推送分支到遠(yuǎn)端倉(cāng)庫(kù)
????git merge branch_name?// 合并分支branch_name到當(dāng)前分支(如master)
????git rebase?//衍合趴荸,線性化的自動(dòng)儒溉, D → A
沖突處理
????git diff?//對(duì)比workspace與index
????git diff HEAD?//對(duì)于workspace與最后一次commit
????git diff <source_branch> <target_branch>?//對(duì)比差異
????git add <filename>?//修改完沖突,需要add以標(biāo)記合并成功
其他
????gitk?//開(kāi)燈圖形化git
????git config color.ui true?//彩色的 git 輸出
????git config format.pretty oneline?//顯示歷史記錄時(shí)发钝,每個(gè)提交的信息只顯示一行
????git add -i?//交互式添加文件到暫存區(qū)