1. 程式人生 > >git實操筆錄二:Git常用命令

git實操筆錄二:Git常用命令

git作為最常見的分散式版本管理工具,掌握其常用的命令,可以讓你用得更順暢。以下是本人通過查閱官網以及其他大佬的文章,熬夜整理出來的。大家可以收藏起來,方便查閱
一、新建程式碼庫

#在當前目錄新建一個git程式碼庫
git init
#新建一個目錄,將其初始化為git程式碼 庫
git init [project-name]
# 下載一個專案和它的整個程式碼歷史
git clone [url]

二、配置
Git的配置檔案為.gitconfig,它可以在使用者主目錄下(全域性配置),也可以在專案目錄下(專案配置)

#顯示當前的Git配置 git config --list
#編輯Git配置檔案 git config -e [–global]
#設定提交程式碼時的使用者資訊 git config [–global]

user.name “[name]” git config [–global] user.email"[email-name]"

三、增加/刪除/修改檔案

#檢視狀態 git status
#檢視變更內容 git diff
#新增指定檔案到暫存區 git add [file] [file2]
#添加當前目錄所有檔案 git add .
#新增每個變化前,都會要求確認
#對於同一個檔案的多次變化,可以實現分段提交 git add -p
#刪除工作區檔案,並且將這次檔案放入暫存區 git rm [file1] [file2]
#停止追蹤指定檔案,但是該檔案會保留在工作區 git rm --cached [file]
#git mv [file-original][file-rename]

四、程式碼提交
#提交暫存區到倉庫
git commit -m [message]
#提交暫存區指定檔案到倉庫區
git commit [file1] [file2] …
#提交從上次提交之後的內容,直接到倉庫
git commit -a
#提交時顯示所有的變化情況
git commit -v
#重做上一次提交,如果程式碼沒有變化,可用於覆蓋上次message
git commit -amend -m [message]
#重做上一次提交,指定新檔案變化
git commit -amend [file] [file2]…

五、分支

#顯示所有本地分支 git branch
#顯示所有遠端分支 git branch -r
#列出所有本地分支和遠端分支 git branch -a
#新建一個分支但依然保留在當前分支 git branch [branch-name]
#新建一個分支,與指定的遠端分支建立追蹤關係 git branch --track [branch] [remote-branch]
#刪除分支 git branch -d [branch-name]
#刪除遠端分支 git push origin --delete [branch-name] git branch -dr [remote/branch]
#新建一個分支,並切換到該分支 git checkout -b [branch]
#切換到上一分支 git checkout -
#建立追蹤關係,在現有的分支與遠端分支之間 git branch --set-upstrea [branch][remote-branch]
#合併指定分支到當前分支 git merge [branch]
#衍合指定到當前分支 git rebase branch
#選擇一個commit,合併到當前分支 git cherry-pick [commit]

六、標籤

#列出所有本地標籤 git tag
#基於最新提交建立標籤 git tag tagname
#刪除標籤 git tag -d tagname
#刪除遠端標籤 git push origin :rfes/tags/[tagname]
#檢視tag資訊 git show [tag]
#提交指定的tag git push [remote] [tag]
#提交所有tag git push [remote] --tags
#新建一個分支,指向某個tag git checkout -b [branch] [tag]

七、檢視資訊

#檢視有變更的檔案 git status
#顯示當前分支的版本歷史 git log
#顯示commit歷史,以及每次commit發生變更的檔案 git log --stat
#搜尋提交歷史,根據關鍵詞 git log-S [keyname]
#顯示某個commit之後的所有變動,其“提交說明”必須符合搜素條件 git log [tag] HEAD --grep feature
#顯示某個commit之後的所有變動,每個commit佔一行 git log [tag] HEAD --pretty=format:%s
#顯示某個檔案的版本歷史包括檔案改名 git log --follow [file] git wharchanged [file]
#顯示指定檔案相關的沒喲從diff git log -p [file]
#顯示過去的5次提交 git log-5 --pretty --oneline
#顯示所有提交過得使用者,按提交次數排序 git shortlog -sn
#顯示指定檔案是什麼人什麼時間修改過 git blame [file]
#顯示暫存區和工作區的差異 git diff
#顯示暫存區和上一個commit的差異 git diff --cached [file]
#顯示工作區去當前最新commit差異 git diff HEAD
#顯示兩次提交之間的差異 git diff [first] …[second]
#顯示今天寫了多少程式碼 git diff --shortstat “@{0 day ago}”
#顯示當前分支最近幾次提交 git reflog

八、遠端操作

#下載遠端倉庫的所有變動 git fetch [remote]
#取回遠端倉庫的變化,並與本地分支合併 git pull [remote] [branch]
#顯示遠端倉庫 git remote -v
#顯示遠端某個倉庫的資訊 git remote show [remote]
#增加一個遠端倉庫並命名 git remote add [shortname][url] #上傳本地指定分支到遠端分支 git push [remote] [branch] #強行推送當前分支到遠端倉庫,即使有衝突 git push [remote]
–force #瑞鬆所有分支到遠端倉庫 git push [remote] --all