Git 筆記:常用命令與原理

一、安裝 Git

  • Windows:下載並安裝 Git.
  • Mac:使用 Homebrew, MacPorts 或者下載 安裝程序
  • Linux (Ubuntu, Debian):sudo apt-get install git-core
  • Linux (Fedora, Red Hat, CentOS):sudo yum install git-core

二、設置 Git

如果需要使用 SSH 協議連接

添加 SSH-Key

ssh-keygen -t rsa -C "[email protected]" # 第一次添加,key generator(密匙生成器)、rsa 一種算法
ssh-keygen -t rsa -f ~/.ssh/id_rsa_x -C "[email protected]" # 非第一次添加
cd ~/.ssh 
open ~/.ssh # mac 下打開

SSH Key 是一對,分爲私鑰和公鑰,是非對稱加密,當本地倉庫需要往遠程倉庫 push 時,私鑰加密傳輸內容,而只有對應的公鑰才能解密,所以遠程倉庫需要添加公鑰。

**一臺電腦要連多個遠程倉庫,可以就用一對 SSH Key。**如果在一臺電腦上,要爲不同遠程倉庫對應不同私鑰時,需要新增 config 文件,爲不同遠程倉庫指定使用不同的私鑰。

# 第一個賬號,默認使用的賬號
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# 第二個賬號
Host second.github.com # second 爲前綴名,可以任意設置
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_2

# 第三個賬號
Host e.coding.net
User git
IdentityFile ~/.ssh/id_rsa_3
  • 原理分析:本地 SSH 客戶端(Git)使用類似 [email protected]:deppwang/deppwang.github.io.git 地址來連接遠程倉庫(可通過 git remote 命令查看)。@ 後面跟的是 Host@github.com 代表使用 id_rsa 這個私鑰

測試 SSH-Key

# 清空本地的 SSH 緩存,添加新的 SSH 密鑰 到 SSH agent 中
ssh-add -D
ssh-add id_rsa
ssh-add id_rsa_x

ssh-add -l # 最後確認一下新密鑰已經添加成功

ssh -T [email protected] # 測試是否連接成功

用戶配置

# 全局配置 用戶名 / 郵箱
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# 取消全局 用戶名 / 郵箱 配置
git config --global --unset user.name
git config --global --unset user.email

# 進入項目文件夾,單獨設置每個 repo 用戶名 / 郵箱
git config user.email "[email protected]"
git config user.name "xxxx" 

# 設置使用 https 使用代理
git config --global http.proxy socks5://127.0.0.1:1080
git config --global https.proxy socks5://127.0.0.1:1080
git config --list # 查看當前倉庫所有配置

git config --global color.ui true # 顯示顏色

LF 與 CRLF

  • LF (Line Feed) 代表換行,對應字符 \n,Unix 系統使用;CR(Carriage Return) ,CRLF 對應字符 \r,Windows 系統使用

標準化 指在提交代碼到 Git 版本庫中時,將文本文件中的換行符 CRLF 轉爲 LF 的過程
轉換 指在檢出 Git 版本庫代碼過程中,將文本文件中的換行符 LF 轉換爲 CRLF 的過程

git config --global core.autocrlf  [true | input | false]  # 全局設置
git config --local core.autocrlf  [true | input | false] # 針對本項目設置
  • true 自動完成標準化與轉換
  • input 只做標準化操作,不做轉換操作
  • false 提交與檢出的代碼都保持文件原有的換行符不變

Git 安裝後默認爲 false,常用設置:

git config --global core.autocrlf  true # Windows
git config --global core.autocrlf  input # Unix

配置別名

git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.unstage 'reset HEAD'
git config --global alias.last 'log -1'
git config --global alias.lg "log --color --graph --pretty=format:'% Cred% h% Creset -% C(yellow)% d% Creset % s % Cgreen(% cr) % C(bold blue)<% an>% Creset' --abbrev-commit"

# macOS 版 Git 默認設置的別名
git config --global alias.aa '!git add . && git add -u . && git status'
git config --global alias.d diff
git config --global alias.cm 'commit -m'

三、常用命令

遠程

git remote add origin [email protected]:username/repo.git # 關聯遠程庫,可關聯多個
git remote # 查看遠程庫
git remote -v # 查看更詳細信息
git remote rename origin orgin2 #  origin 重命名爲 origin2

git co -b dev origin/dev # 將遠程 dev 分支切換爲本地 dev 分支

git push origin (-u) <branch-name> # 推送到遠程分支,'-u' 參數,將當前分支和遠程的 master 分支關聯起來。不能 push 空到遠程去
git push origin :branch-name /git branch -dr <remote/branch> # 刪除遠程分支
git push -f origin master # 回滾遠程分支,讓其跟本地分支一致

git pull # pull 所有的分支到本地,如果不是直接 clone 的項目,直接使用此命令會報錯
git fetch # 拉取所有分支到本地,不 merge

git clone [email protected]:username/repo.git # clone 後 不用再關聯遠程庫
git clone [email protected]:username/repo.git new-name  # 將克隆的倉庫重命名爲 new-name

# 建立本地分支和遠程分支的關聯,git pull 失敗時執行
git br --set-upstream-to=origin/branch-name branch-name
git br --set-upstream branch-name origin/branch-name

常規

git add . /git add -A  /git add --all # 添加所有的文件
git add -u # update,add 所有 modified 文件

git st # 查看狀態

git ci -m <message> ## Commit

git ci --amend # 修改最後一次 commit,已提交到遠程的 commit 不要修改

git merge dev # 將 dev 分支合併到當前分支(默認 Fast-forward 模式,看不出來合併歷史)
git merge --no-ff -m "merge with no-ff" dev # 禁用 Fast-forward,可以看出合併歷史

git diff <file> # 查看修改的內容 (工作區和暫存區的不同,工作區和原來的不同)
git diff HEAD -- <file> # 查看工作區與版本庫最新版本(分支的最新 commit)的不同
git diff --cached <file> # 查看暫存區和分支的不同

git log -p <file> # 查看當前文件 commit 記錄
git log -p -1 # 查看最近一次 commit 修改記錄
git blame <file> # 查看當前文件被誰修改過

Ctrl + C # 命令錯誤時,退出編輯頁面
Ctrl + Z # 退出
  • rebase 讓分支變爲一根直線,更加清晰

分支

git br dev # 創建分支
git br -m oldName newName # 修改分支名字
git br # 查看當前分支
git br -v # 查看本地分支詳情
git br -a # 查看所有分支
git br -av # 查看所有分支詳情
git br -d dev # 刪除 dev 分支,只是刪除了 dev 指針。
git br -D <branch-name> # 強行刪除沒有合併的分支
git br | grep -v "dev" | xargs git branch -D # 刪除除 dev 外的所有分支
git co -b dev # 創建並切換分支

撤銷、刪除與回退

git co -- <file> # 把文件在工作區的修改全部撤銷掉(回到最近 git commit 或 git add 時的狀態)
git co -- . # 全部撤銷

git unstage <file> # 撤銷 add 操作,將修改的內容放回工作區
git unstage . # 全部撤銷

rm file # 刪除工作區的文件,需要 commit 生效。刪錯了可以使用 `git co -- file` 還原
git rm --cache <file> # 從暫存區刪除文件
git rm -rf --cache <file> # 從暫存區中清除文件文件
git rm <file> # 從分支上刪除文件

git merge --abort # 撤銷 pull/merge 操作,常用於 pull 衝突時

git reset --hard HEAD^ # 回退到上一版本
git reset --hard <commit> # 回退到某一版本

儲藏

git stash save "message" # 把未提交的,當前現場「儲藏」起來。
git stash list # 查看所有的 “儲藏”
git stash apply # 恢復不刪除
git stash drop ## 刪除
git stash pop # 恢復順便刪除
git stash apply stash@{x} # 恢復指定下標 stash

標籤

git tag <name> # 打標籤
git tag # 查看所有標籤
git tag <name> <commitId> # 將標籤打在某個 commit 上
git show <name> # 查看標籤信息
git tag -a <name> -m "***" <commitId> # -a 指定標籤名,- 
git tag -d <name> # 刪除標籤
git push origin --tags # 推送所有未推送的標籤
git tag -d <name> /git push origin :refs/tags/<name> # 刪除遠程標籤

.gitignore

# 忽略除 .sh/.py 結尾的其他文件
*
!*.sh
!*.py

項目協作時,本地修改某個文件,但不想將修改提交到遠程。可以使用 「skip-worktree」來關閉 GIT 跟蹤本地文件修改

git update-index --skip-worktree /Users/yanjie/Workspace/medweb/.PYTHON.sh

Commit

git ci -m "Article updated: `date +'% Y-% m-% d % H:% M:% S'`" # Article updated: 2019-05-29 21:11:06
git ci -m "`date +'% Y-% m-% d'`" # 2019-05-29
git ci -m "`date`"
current="`date +'% Y-% m-% d % H:% M:% S'`"
msg="Article updated: $current"
git ci -m "$msg" # Article updated: 2019-05-29 21:11:06
git ci -m ":bug: Fixing a bug."  # 🐛 Fixing a bug.

IDEA Git

  • IDEA 版本控制是默認記錄工作區和暫存區與分支上的不同,即:直接修改工作區的內容,不同;add 到暫存區後,還是不同
  • IDEA 可以直接 commit 文件而不用 add,可以理解爲默認幫助 add
  • IDEA 的 revert 相當 git co -- file
  • 修改 commit 描述:Undo commit

四、原理

image.png

image.png

image.png

References

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章