git 記錄

潼关路边的一只野鬼發表於2024-06-28

忽略

參考

Git 小技巧 - 忽略不想要提交的本地修改

總結

本地忽略檔案

某些檔案需要忽略,但是不想修改 .gitignore ,可以在 .git/info/exclude 中配置,此檔案與 .gitignore 有相同的規則

忽略某些檔案的提交

某些檔案在本地倉庫中會修改,但是又不想提交修改, 可以使用如下命令

# 忽略
git update-index --skip-worktree /path/to/file
# 檢視忽略的檔案
git ls-files -v | grep ^S
# 撤銷
git update-index --no-skip-worktree /path/to/file

忽略檔案的方法

  • .gitignore
  • .git/info/exclue
  • git update-index --assume-unchanged
  • git update-index --skip-worktree

區別

.gitignore

說明:顯式地阻止提交檔案。
優勢:.gitignore 檔案本身提交至遠端倉庫,全組共享忽略檔案配置。
侷限:如果專案已經存在遠端倉庫,即使被加入 .gitignore,仍然可以進行修改並提交。本地的修改會顯示在 git status 結果中

.git/info/exclue

說明:顯式地阻止提交檔案。
優勢exclude 檔案本身不會提交至遠端倉庫,因此適合放一些個人定製的 gitignore 專案。
侷限:和 .gitignore 存在同樣地侷限。檔案若已存在遠端倉庫,則本地修改仍可以提交至遠端倉庫。本地的修改會顯示在 git status 結果中。

assume-unchanged

說明:宣告本地遠端都不會修改這個檔案。
優勢:git 直接跳過這些檔案的處理以提升效能。檔案不會出現在 git status
侷限:不適合本地或遠端需要修改的檔案。本地會忽略掉之後遠端檔案的修改。

skip-worktree

說明:宣告忽略檔案的本地修改。
優勢:本地可以對檔案做一些個人定製。檔案不會出現在 git status
侷限:拉取遠端檔案更新,或切換分支時有可能出現衝突,需要撤銷忽略後手動解決衝突。

多使用者配置

刪除全域性使用者資訊

多使用者情況下,儘量不要設定全域性使用者資訊

# 新增全域性使用者資訊
git config --global user.name "使用者名稱"
git config --global user.email "郵箱"
#刪除全域性使用者資訊
git config --global --unset user.name
git config --global --unset user.email

生成 key

注意儲存路徑

$ ssh-keygen -t rsa -C "github@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:K8ZzHA4rrhgHlv7qyP+dAmvpQIq+jPUpbMdjXZncAnE github@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|      . E        |
|       o         |
|  .   .          |
|.+    .oS+       |
|*. . . =*o.      |
|++o.=.*.=.       |
|*=**==o+.        |
|=OBO=o.o         |
+----[SHA256]-----+

將 key 新增至響應的網站

配置 config

檔案位置 ~/.ssh/config
不可有註釋

Host github.com
    User github
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
Host 192.168.12.5
    User gitlab
    Hostname 192.168.12.5
    Port 122 # 如果不是22則新增次行
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_gitlab

將密碼加入密匙管理器

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa_github
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_github:
Identity added: /c/Users/Administrator/.ssh/id_rsa_github (/c/Users/Administrator/.ssh/id_rsa_github)
 
$ ssh-add ~/.ssh/id_rsa_gitlab
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_gitlab:

TortoiseGit 結合 bcompare

參考

TortoiseGit 檔案比對工具使用 Beyond Compare 和 DiffMerge

設定

diff: C:\Program Files (x86)\Beyond Compare 3\BComp.exe %base %mine /title1=%bname /title2=%yname /leftreadonly
merge: C:\Program Files (x86)\Beyond Compare 3\BComp.exe” %mine %theirs %base %merged /title1=%yname /title2=%tname /title3=%bname /title4=%mname

修改 commit 資訊

參考

如何修改已提交commit資訊

無痕修復:優雅修改本地及遠端Commit資訊,保持專案歷史整潔

修改最近一次的提交

git commit --amend

修改多次

n 為次數

# 第一步
git rebase -i HEAD~n
# 第二步
修改 pick 為 edit,然後 wq 退出
# 第三步
git commit --amend 修改提交資訊
git rebase --continue
# 重複第三部,直到編輯完畢

例如如下 commit

$ git log --oneline
fd2b105 (HEAD -> master) commit 002
0e82138 commit 001
5659187 (origin/master, origin/HEAD) 解決編譯報錯
80c5b7c 匯入原始碼
7fe7020 Initial commit

修改 commit 001commit 002 可以使用 git rebase -i HEAD~2,操作結果如下

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master)
$ git rebase -i HEAD~2
Stopped at 0e82138...  commit 001
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master|REBASE 1/2)
$ git commit --amend
[detached HEAD 0dd2c0e] commit 001-001
 Date: Fri Jun 28 07:36:28 2024 +0800
 1 file changed, 1 insertion(+)
 create mode 100644 one.txt

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master|REBASE 1/2)
$ git rebase --continue
Stopped at fd2b105...  commit 002
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master|REBASE 2/2)
$ git commit --amend
[detached HEAD 604ff01] commit 002-002
 Date: Fri Jun 28 07:38:57 2024 +0800
 1 file changed, 1 insertion(+)

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master|REBASE 2/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master)
$ git log --oneline
604ff01 (HEAD -> master) commit 002-002
0dd2c0e commit 001-001
5659187 (origin/master, origin/HEAD) 解決編譯報錯
80c5b7c 匯入原始碼
7fe7020 Initial commit

laolang@DESKTOP-O12ME4M MINGW64 /e/gitee/bzrj/acwj (master)
$

推送遠端

--force-with-lease 提供了一種更安全的強制推送方式。它會在推送前檢查遠端分支的狀態是否與你預期的一致。如果遠端分支在你上次拉取後有其他人的新提交,推送會失敗,從而避免意外覆蓋他人的工作。在多人協作的環境中,--force-with-lease 是一個更好的選擇

git push --force-with-lease origin <your_branch_name>

關於 git log

參考

Git log 進階用法(含格式化、以及資料過濾)
git log 單行、多行 詳細顯示結果、提交的檔名【彙總引數演示】

相關文章