1. 程式人生 > 其它 >Git常用命令超級詳細(全網最詳細)

Git常用命令超級詳細(全網最詳細)

1.新建程式碼庫

1.1在當前目錄新建一個 Git 程式碼庫

$ git init

1.2新建一個目錄,將其初始化為 Git 程式碼庫

$ git init [project-name]

1.3下載一個專案和它的整個程式碼歷史

$ git clone [url]

2.配置

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

2.1設定提交程式碼時的使用者資訊

$ git config [--global] user.name "[name]"
$ git config [--global] user.email "[email address]"

2.2編輯 Git 配置檔案

$ vim ~/.gitconfig

2.3顯示當前的 Git 配置

$ git config --list

2.4顯示當前的 Git 配置

$ cat ~/.gitconfig

3.增加/刪除檔案

3.1新增指定檔案到暫存區

$ git add [file1] [file2] ...

3.2新增指定目錄到暫存區,包括子目錄

$ git add [dir]

3.3添加當前目錄的所有檔案到暫存區

$ git add .

3.4刪除工作區檔案,並且將這次刪除放入暫存區

$ git rm 1.txt 2.txt
error: the following files have changes staged in the index:
    1.txt
    2.txt
(use --cached to keep the file, or -f to force removal)

3.4.1直接刪除檔案

$ git rm -fr 1.txt

3.4.2停止追蹤指定檔案,但該檔案會保留在工作區

$ git rm --cached [file]
$ git rm --cached 1.txt 2.txt
rm '1.txt'
rm '2.txt'

3.5改名檔案,並且將這個改名放入暫存區

$ git mv [file-original] [file-renamed]

4.程式碼提交

4.1提交暫存區到倉庫區

$ git commit -m [message]

4.2提交暫存區的指定檔案到倉庫區

$ git commit [file1] [file2] ... -m [message]

4.3提交工作區自上次 commit 之後的變化,直接到倉庫區

$ git commit -a

4.4提交時顯示所有 diff 資訊

$ git commit -v

4.5使用一次新的 commit,替代上一次提交如果程式碼沒有任何新變化,則用來改寫上一次 commit 的提交資訊

$ git commit --amend -m [message]

5.分支

5.1列出所有本地分支

$ git branch


*表示當前分支

5.2列出所有遠端分支

$ git branch -r

5.3列出所有本地分支和遠端分支

$ git branch -a

5.4新建一個分支,但依然停留在當前分支

$ git branch [branch-name]

5.5新建一個分支,並切換到該分支

$ git checkout -b [branch]

5.6新建一個分支,指向指定 commit

5.6.1檢視提交的日誌

$ git log

commit 2cb44e585aab4d1b2370a3e42dc6d4070fe217dc (HEAD -> DevOps_RC, origin/master, master, DevOps_Release, DevOps_Dev)
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 11:03:12 2021 +0800
    修改了ReadMe.md檔案,添加了一行說明

commit 89d1d5b198a77deb9096e11b02aec7187221f384
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 10:59:09 2021 +0800
:...skipping...
commit 2cb44e585aab4d1b2370a3e42dc6d4070fe217dc (HEAD -> DevOps_RC, origin/master, master, DevOps_Release, DevOps_Dev)
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 11:03:12 2021 +0800
    修改了ReadMe.md檔案,添加了一行說明

commit 89d1d5b198a77deb9096e11b02aec7187221f384
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 10:59:09 2021 +0800
    新增User資料夾

commit b54962a30b090f02a39fc3c6185750cf07b068ea
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 10:58:30 2021 +0800
    新增忽略檔案

commit e76012d77857841d344ac04893a158e75e8088fc
Author: zhangsan <[email protected]>
Date:   Sat Jul 10 10:56:46 2021 +0800
    新增Readme.md檔案

5.6.2新建一個分支,指向指定 commit ID

$ git branch [branch] [commit]
$ git branch DevOps_V0.3FixBug  89d1d5b198a77deb9096e11b02aec7187221f384

5.7新建一個分支,與指定的遠端分支建立追蹤關係

$ git branch --track [branch] [remote-branch]
$ git branch --track DevOps_RC1 DevOps_RC

5.7.1DevOps_RC,增加一次提交

5.7.2DevOps_RC,再增加一次提交

5.8切換到指定分支,並更新工作區

5.8.1切換到建立追蹤的分支DevOps_RC1

$ git checkout [branch-name]
$ git checkout DevOps_RC1

5.8.2從建立追蹤的分支拉取最新的變化

$ git pull

5.9建立追蹤關係,在現有分支與指定的遠端分支之間

$ git branch --set-upstream [branch] [remote-branch]

5.10合併指定分支到當前分支

$ git merge [branch]

5.11選擇一個 commit,合併進當前分支

$ git cherry-pick [commit]

5.12刪除分支

$ git branch -d [branch-name]

5.13刪除遠端分支

$ git push origin --delete
$ git branch -dr

6.標籤

6.1列出所有 tag

$ git tag

6.2新建一個 tag 在當前 commit

$ git tag [tag]

6.3新建一個 tag 在指定 commit

$ git tag [tag] [commit]

6.4檢視 tag 資訊

$ git show [tag]

6.5提交指定 tag

$ git push [remote] [tag]

6.6提交所有 tag

$ git push [remote] --tags

6.7新建一個分支,指向某個 tag

$ git checkout -b [branch] [tag]

7.檢視資訊

7.1顯示有變更的檔案

$ git status

7.2顯示當前分支的版本歷史

$ git log

7.3顯示 commit 歷史,以及每次 commit 發生變更的檔案

$ git log --stat

7.4顯示某個檔案的版本歷史,包括檔案改名

$ git log --follow [file]
$ git whatchanged [file]

7.5顯示指定檔案相關的每一次 diff

$ git log -p [file]

7.6顯示指定檔案是什麼人在什麼時間修改過

$ git blame [file]

7.7顯示暫存區和工作區的差異

$ git diff

7.8顯示暫存區和上一個 commit 的差異

$ git diff --cached []

7.9顯示工作區與當前分支最新 commit 之間的差異

$ git diff HEAD

7.10顯示兩次提交之間的差異

$ git diff [first-branch]...[second-branch]

7.11顯示某次提交的元資料和內容變化

$ git show [commit]

7.12顯示某次提交發生變化的檔案

$ git show --name-only [commit]

7.13顯示某次提交時,某個檔案的內容

$ git show [commit]:[filename]

顯示當前分支的最近幾次提交

$ git reflog

8.遠端同步

8.1下載遠端倉庫的所有變動

$ git fetch [remote]

8.2顯示所有遠端倉庫

$ git remote -v

8.3顯示某個遠端倉庫的資訊

$ git remote show [remote]

8.4增加一個新的遠端倉庫,並命名

$ git remote add [shortname] [url]

8.5取回遠端倉庫的變化,並與本地分支合併

$ git pull [remote] [branch]

8.6上傳本地指定分支到遠端倉庫

$ git push [remote] [branch]

8.7強行推送當前分支到遠端倉庫,即使有衝突

$ git push [remote] --force

8.8推送所有分支到遠端倉庫

$ git push [remote] --all

9.撤銷

9.1恢復暫存區的指定檔案到工作區

$ git checkout [file]

9.2恢復某個 commit 的指定檔案到工作區

$ git checkout [commit] [file]

9.3恢復上一個 commit 的所有檔案到工作區

$ git checkout .

9.4重置暫存區的指定檔案,與上一次 commit 保持一致,但工作區不變

$ git reset [file]

9.5重置暫存區與工作區,與上一次 commit 保持一致

$ git reset --hard

9.6重置當前分支的指標為指定 commit,同時重置暫存區,但工作區不變

$ git reset [commit]

9.7重置當前分支的 HEAD 為指定 commit,同時重置暫存區和工作區,與指定 commit 一致

$ git reset --hard [commit]

9.8重置當前 HEAD 為指定 commit,但保持暫存區和工作區不變

$ git reset --keep [commit]

9.9新建一個 commit,用來撤銷指定 commit

9.10後者的所有變化都將被前者抵消,並且應用到當前分支