Git命令行

幾個概念



(圖片來自http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html)

  • Workspace:工作區(qū)
  • Index / Stage:暫存區(qū)
  • Repository:本地倉庫
  • Remote:遠程倉庫

配置用戶信息


用戶的信息保存在.gitconfig文件中。全局的.gitconfig文件位于用戶目錄下。

// 查看配置信息(global + local)
git config --list
// 查看配置信息(global)
git config --global --list
// 查看配置信息(local)
git config --local --list

// 修改配置信息
git config --global --edit

// 配置用戶名
git config --global user.name "yourname"

// 配置用戶郵箱
git config --global user.email "youremail@gmail.com"

// 配置默認文本編輯器
git config --global core.editor sublime

// 配置默認merge工具
git config --global merge.tool p4merge

// 設(shè)置推送默認方式(simple或者matching)
// ?matching:Git 1.x 的默認行為,如果執(zhí)行 git push 但沒有指定分支,將 push 所有本地的分支到遠程倉庫中。
// ?simple:Git 2.x 的默認行為,如果執(zhí)行 git push 但沒有指定分支,將 push 當(dāng)前本地的分支到遠程倉庫中。
git config --global push.default simple/matching

如果用了 --global 選項,那么更改的配置文件就是位于你用戶主目錄下的那個,以后你 所有的項目都會默認使用這里配置的用戶信息。如果要在某個特定的項目中使用其他名字或 者電郵,只要去掉 --global 選項重新配置即可,新的設(shè)定保存在當(dāng)前項目的 .git/config 文件里。

常用命令


// 創(chuàng)建本地Git倉庫(將已有項目加入 git 管理)
git init
// 創(chuàng)建本地Git倉庫(新建項目并加入 git 管理)
git init <project_name>
// 將本地Git倉庫上傳到遠程倉庫(首先遠程倉庫要存在)
git remote add origin git@github.com:username/reponame.git
git push origin master
// 刪掉本地 Git 倉庫關(guān)聯(lián)的遠程倉庫
git remote rm origin

// 克隆倉庫
git clone https://github.com/test/test.git
// 倉庫太大,克隆不下來,先淺克隆(淺克隆相關(guān)內(nèi)容參考:淺克隆
git clone --depth=1 https://github.com/test/test.git

// 查看當(dāng)前狀態(tài)
git status

// 把文件添加到暫存區(qū)域
git stage <文件名>
git add <文件名>
// 把所有文件添加到暫存區(qū)域
git stage .
git add .

// 提交文件
git commit <文件名>
// 提交所有文件
git commit . / git commit
// 提交所有文件并附帶注釋
git commit -m "comment"
// 跳過 git add 直接 commit
git commit -a -m "comment"

// 推送到遠程倉庫
git push
// 把當(dāng)前分支push到遠程倉庫,并制定分支在遠程倉庫的名稱
git push origin <分支名>

// 抓取遠程倉庫數(shù)據(jù)到本地,抓取僅僅是看遠程倉庫有哪些更新
// 如果想要這些更新反應(yīng)到本地工作目錄,必須要執(zhí)行pull操作
git fetch origin
git fetch --all
git fetch
// 在抓取的同時,把已經(jīng)不存在的branch給刪除掉
git fetch --prune
git fetch -p

// 把遠程倉庫最新內(nèi)容更新到本地工作目錄
git pull

// 刪除文件
rm <文件名>
// 刪除文件并添加到暫存區(qū)域
git rm <文件名>
// 刪除文件(僅僅是從Git倉庫刪除,也就是從暫存區(qū)域刪除,本地工作目錄不刪除)
git rm --cached <文件名>

// 重命名文件
git mv <原文件名> <新文件名>

// 查看工作目錄中當(dāng)前文件和暫存區(qū)域快照之間的差異
git diff
// 查看已經(jīng)暫存起來的文件和上次提交時的快照之間的差異
git diff --staged/--cached
// 工具diff
git difftool
// 工具diff(所有文件一起顯示)(kdiff3可以,p4Merge不可以)
git difftool --dir-diff
git difftool -d

// 修正提交內(nèi)容(有些文件忘了提交或者想要修改提交注釋)
// 下面兩個提交會合并成一個提交
git stage file1
git commit -m "commit 1"
git stage file2
git commit --amend -m "commit 1 and 2"
git push -f

// 取消已暫存的文件
git reset HEAD <文件名>
// 取消對文件的修改
git checkout -- <文件名>
// 取消對所有文件的修改
git checkout .
// 撤銷某次提交內(nèi)容及之后的所有提交內(nèi)容,恢復(fù)到所選提交id的前一次提交id的狀態(tài)
// 撤銷完成后如果沒有沖突的話會自動新建一個commit
git revert <提交id>
// 撤銷遠程倉庫的提交
git reset --hard <提交id> // 提交id是想保留的最近的一次提交id
git push -f origin <分支名>
或者
git checkout <提交id>
git push -f origin HEAD:<分支名>

// 查看所有本地branch
git branch
// 查看所有遠程branch
git branch -r
// 查看所有本地和遠程branch
git branch -a
// 查看所有本地和遠程branch(帶最新一次commit id 和 commit log)
git branch -av
// 查看所有branch,并顯示最后一次commit信息
git branch -v
// 切換到指定的branch
git checkout <分支名>
// 從遠程倉庫checkout分支
git checkout -b <本地分知名> origin/<遠程倉庫分支名>
git checkout --track origin/<遠程倉庫分支名>
// 創(chuàng)建branch(不會自動切換到創(chuàng)建的branch)
git branch test-branch
// 創(chuàng)建branch(并切換到創(chuàng)建的branch)
git checkout -b test-branch
// 基于某個branch創(chuàng)建branch(并切換到創(chuàng)建的branch)
git checkout -b test-branch base-branch
// 基于某個hash值創(chuàng)建branch(并切換到創(chuàng)建的branch)
git checkout -b test-branch f9ea07b74b4b00
// push本地分支到遠程倉庫
git push --set-upstream origin test-branch
// 將其他分支的內(nèi)容merge到master
git checkout master
git merge test-branch
// 查看那些分支已經(jīng)被合并到當(dāng)前分支
git branch --merged
// 查看那些分支還沒有被合并到當(dāng)前分支
git branch --no-merged
// 刪除分支
git branch -d test-branch
// 把某次的提交內(nèi)容合并到當(dāng)前分支
git cherry-pick <提交id>

// 刪除遠程分支
git push origin :<遠程分支名>

// 顯示所有 tag
git tag
// 顯示所有1.x的tag
git tag -l "v1.*"
// 顯示tag的詳細信息
git show v1.0
// 創(chuàng)建 tag
git tag -a v1.0 -m 'tag v1.0'
// 以某個提交 id 創(chuàng)建 tag
git tag -a v1.0 <提交 id> -m 'tag v1.0'
// 將創(chuàng)建的 tag 推送到遠程倉庫
git push origin v1.0 // 單個 tag
git push --tags 或者 git push origin --tags // 所有 tag

// 查看log
git log
// 查看log(每條log一行顯示)
git log --oneline
// 查看log并顯示簡要的增高行數(shù)
git log --stat
// 查看提交時的補丁信息
git log -p
// 查看最近2次log
git log -2
// 以關(guān)鍵字搜索log
git log -S <關(guān)鍵字>
// 顯示所有提交過的用戶和提交message
git shortlog
// 顯示所有提交過的用戶,按提交次數(shù)排序
git shortlog -sn
// 查看所有分支log
git log --all
// 查看所有分支log,以圖形的方式顯示
git log --all --graph
// 顯示指定文件是什么人在什么時間修改過
git blame [file]
// 查看所有分支的所有操作記錄(注意是操作記錄,不僅僅局限于提交紀錄)
git reflog
// 使用可視化工具查看log
gitk

// 添加一個遠程版本庫,并指定名稱為 github
git remote add github <url>
// 查看遠程版本庫信息
git remote -v
// 查看指定的遠程倉庫信息
git remote show origin
// 重命名遠程倉庫(默認的遠程倉庫名為 origin)
git remote rename <原倉庫名> <新倉庫名>
// 刪除遠程倉庫
git remote rm <倉庫名>

// 緩存當(dāng)前增在進行的工作
git stash
git stash save
git stash save "stash name"
// 緩存當(dāng)前增在進行的工作(包括沒有被追蹤的文件)
git stash --include-untracked
git stash -u
// 查看所有stash
git stash list
// 恢復(fù)緩存(不加緩存名稱則默認最近一次緩存)
git stash apply
git stash apply stash@{0}
// 恢復(fù)緩存并刪除該緩存
git stash pop
git stash pop stash@{0}
// 刪除緩存
git stash drop
git stash drop stash@{0}
// 顯示緩存內(nèi)容(文件一覽)
git stash show stash@{0}
// 顯示緩存內(nèi)容(差分一覽)
git stash show -p stash@{0}

// 通過 hash 值查看對象的類型
git cat-file -t f9ea07b74b4b00
// 通過 hash 值查看對象的大小
git cat-file -s f9ea07b74b4b00
// 通過 hash 值查看對象的內(nèi)容
git cat-file -p f9ea07b74b4b00

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容