Git常用命令

编程小妹發表於2024-03-15

# 顯示當前的Git配置
$ git config --list
​
# 編輯Git配置檔案 ctrl+x 退出編輯
$ git config -e [--global]
​
# 設定提交程式碼時的使用者資訊
$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

# 增加/刪除檔案
建立一個本地倉庫,將自己的相關專案檔案放入其中。

# 新增指定檔案到暫存區
$ git add [file1] [file2] ...
​
# 新增指定目錄到暫存區,包括子目錄
$ git add [dir]
​
# 新增當前目錄的所有檔案到暫存區
$ git add .
​
# 新增每個變化前,都會要求確認
# 對於同一個檔案的多處變化,可以實現分次提交
$ git add -p
​
# 刪除工作區檔案,並且將這次刪除放入暫存區
$ git rm [file1] [file2] ...
​
# 停止追蹤指定檔案,但該檔案會保留在工作區
$ git rm --cached [file]
​
# 改名檔案,並且將這個改名放入暫存區
$ git mv [file-original] [file-renamed]

剛剛操作的檔案只在暫存區(緩衝區),未在倉庫區(硬碟) ,如何提交到倉庫區尼?

# 提交暫存區到倉庫區 -m 備註資訊
$ git commit -m [message]
​
# 提交暫存區的指定檔案到倉庫區
$ git commit [file1] [file2] ... -m [message]
​
# 提交工作區自上次commit之後的變化,直接到倉庫區
$ git commit -a
​
# 提交時顯示所有diff資訊
$ git commit -v
​
# 使用一次新的commit,替代上一次提交
# 如果程式碼沒有任何新變化,則用來改寫上一次commit的提交資訊
$ git commit --amend -m [message]
​
# 重做上一次commit,幷包括指定檔案的新變化
$ git commit --amend [file1] [file2] ...

# 分支

分支就像多個資料夾一樣。

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

# 列出所有tag
$ git tag
​
# 新建一個tag在當前commit
$ git tag [tag]
​
# 新建一個tag在指定commit
$ git tag [tag] [commit]
​
# 刪除本地tag
$ git tag -d [tag]
​
# 刪除遠端tag
$ git push origin :refs/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 [keyword]
​
# 顯示某個commit之後的所有變動,每個commit佔據一行
$ git log [tag] HEAD --pretty=format:%s
​
# 顯示某個commit之後的所有變動,其"提交說明"必須符合搜尋條件
$ git log [tag] HEAD --grep feature
​
# 顯示某個檔案的版本歷史,包括檔案改名
$ git log --follow [file]
$ git whatchanged [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-branch]...[second-branch]
​
# 顯示今天你寫了多少行程式碼
$ git diff --shortstat "@{0 day ago}"
​
# 顯示某次提交的後設資料和內容變化
$ git show [commit]
​
# 顯示某次提交發生變化的檔案
$ git show --name-only [commit]
​
# 顯示某次提交時,某個檔案的內容
$ git show [commit]:[filename]
​
# 顯示當前分支的最近幾次提交
$ git reflog

# 遠端同步

# 下載遠端倉庫的所有變動
$ git fetch [remote]

# 顯示所有遠端倉庫
$ git remote -v

# 顯示某個遠端倉庫的資訊
$ git remote show [remote]

# 增加一個新的遠端倉庫,並命名
$ git remote add [shortname] [url]

# 取回遠端倉庫的變化,並與本地分支合併
$ git pull [remote] [branch]

# 上傳本地指定分支到遠端倉庫
$ git push [remote] [branch]

# 強行推送當前分支到遠端倉庫,即使有衝突
$ git push [remote] --force

# 推送所有分支到遠端倉庫
$ git push [remote] --all

kali@ubuntu:~/MyGit/git/qt$ git  remote show 
origin
kali@ubuntu:~/MyGit/git/qt$ git  remote show origin 
* 遠端 origin
  獲取地址:git@gitee.com:xxxx/qt.git
  推送地址:git@gitee.com:xxxx/qt.git
  HEAD 分支:master
  遠端分支:
    QPushButton 已跟蹤
    cplusplus   已跟蹤
    master      已跟蹤
  為 'git pull' 配置的本地分支:
    master 與遠端 master 合併
  為 'git push' 配置的本地引用:
    master 推送至 master (最新)
kali@ubuntu:~/MyGit/git/qt$
​

kali@ubuntu:~/MyGit/git/qt$ git branch 
* master
kali@ubuntu:~/MyGit/git/qt$ git branch sub_master
kali@ubuntu:~/MyGit/git/qt$ git branch 
* master
  sub_master
kali@ubuntu:~/MyGit/git/qt$ git push origin sub_master 
Total 0 (delta 0), reused 0 (delta 0)
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'sub_master' on Gitee by visiting:
remote:     https://gitee.com/xxxx/qt/pull/new/xxxx:sub_master...xxxxmaster
To gitee.com:xxxx/qt.git
 * [new branch]      sub_master -> sub_master
kali@ubuntu:~/MyGit/git/qt$
​

kali@ubuntu:~/MyGit/git/qt$ git push origin --delete sub_master 
remote: Powered by GITEE.COM [GNK-6.4]
To gitee.com:abc19960820/qt.git
 - [deleted]         sub_master

# 恢復暫存區的指定檔案到工作區
$ git checkout [file]
​
# 恢復某個commit的指定檔案到暫存區和工作區
$ git checkout [commit] [file]
​
# 恢復暫存區的所有檔案到工作區
$ git checkout .
​
# 重置暫存區的指定檔案,與上一次commit保持一致,但工作區不變
$ git reset [file]
​
# 重置暫存區與工作區,與上一次commit保持一致
$ git reset --hard
​
# 重置當前分支的指標為指定commit,同時重置暫存區,但工作區不變
$ git reset [commit]
​
# 重置當前分支的HEAD為指定commit,同時重置暫存區和工作區,與指定commit一致
$ git reset --hard [commit]
​
# 重置當前HEAD為指定commit,但保持暫存區和工作區不變
$ git reset --keep [commit]
​
# 新建一個commit,用來撤銷指定commit
# 後者的所有變化都將被前者抵消,並且應用到當前分支
$ git revert [commit]
​
暫時將未提交的變化移除,稍後再移入
$ git stash
$ git stash pop

# 其他

# 生成一個可供釋出的壓縮包$ git archive

本作品採用《CC 協議》,轉載必須註明作者和本文連結