git 命令
常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| 🔧 基本指令 git init – 初始化一個新的 Git 儲存庫。 git clone <repo_url> – 克隆遠端儲存庫。 git status – 檢查工作目錄的狀態。 git add <file> – 暫存要提交的變更。 git commit -m "message" – 提交暫存的變更並附帶一則訊息。 git push – 將本機提交推送到遠端儲存庫。 git pull – 從遠端倉庫取得並合併變更。 git diff – 顯示工作目錄中的變更(未提交的變更)。 git diff --staged – 顯示暫存區和上次提交之間的變化。
🛠️ 分支和合併 git branch – 列出分支。 git branch <branch_name> – 建立一個新分支。 git checkout <branch_name> – 切換到另一個分支。 git checkout -b <branch_name> – 建立並切換到新分支。 git merge <branch_name> – 將分支合併到目前分支。 git branch -d <branch_name> – 合併後刪除分支。 git branch -D <branch_name> – 強制刪除一個分支,即使它尚未合併。
🔄 同步 git fetch – 從遠端下載變更而不合併。 git rebase <branch> – 在另一個分支上重新套用提交以維護線性歷史記錄。 git pull --rebase – 取得並在最新的遠端變更之上重新套用您的變更。 git remote add <name> <url> – 新增一個新的遠端儲存庫。
🎯 高級 Git git stash – 暫時儲存變更而不提交。 git stash pop – 重新套用儲存的變更。 git cherry-pick <commit> – 將特定提交套用到目前分支。 git log --oneline – 查看簡化的投稿歷史記錄。 git reflog – 顯示參考變更的歷史記錄(例如,簽出、重設)。 git log --graph --decorate --all – 顯示可視化的提交歷史記錄。
🚨 撤銷更改 git reset <file> – 取消暫存檔案。 git reset --soft <commit> – 重設為提交但保留工作目錄中的變更。 git reset --hard <commit> – 完全重設為上一次提交,丟棄更改。 git revert <commit> – 建立一個撤銷特定提交的新提交。
⚙️ 與他人合作 git fork – 在 GitHub 上 Fork 一個儲存庫(透過 UI)以開始貢獻。 git pull origin <branch> – 從原始遠端分支中提取變更。 git push origin <branch> – 將您的分支推送到原始儲存庫以進行協作。
|
git init
Branch
建立新分支 backup
1 2 3 4
| git branch issue1
git checkout -b backup
|
see branch
1 2 3 4 5 6 7 8
| git branch
git branch -r
git branch -a
git branch -v
|
本地參考儲存庫
1 2 3 4 5 6
| git show-ref d9c9e57d12c9dbdc1b9445fa3333cd7e637c888d refs/heads/master d9c9e57d12c9dbdc1b9445fa3333cd7e637c888d refs/heads/serverfix 7369a897219619143420b24c9df7383ddca643a2 refs/remotes/origin/HEAD 7369a897219619143420b24c9df7383ddca643a2 refs/remotes/origin/master 37d73f1a945ece85dffe798ba7ae43733bf1f4aa refs/remotes/team/master
|
更改 branch 名稱
1 2 3
| git branch -M main git branch -m main
|
刪除 branch
checkout to branch
1 2 3
| git checkout backup
git checkout 1f0aa12
|
Commit
add all file
1 2 3 4 5
| git add .
git add -A git add --all
|
還原檔案修改
1
| git checkout -- branch_test.txt
|
add commit
1 2 3 4 5
| git commit -m "1st commit"
git commit -am "update 2021/04/12"
|
更動最後一筆 commit 說明
移除最後一次 commit
1 2 3 4 5 6
| git reset HEAD^ --hard
git reset HEAD^ --soft git reset HEAD^
|
list commit
但只列出雜湊碼,所以不清楚
1 2 3 4
| git rev-list --remotes
git rev-list -–all
|
dump modify status
list all file
復原已staged的檔案 unstage
1 2 3 4
| git reset HEAD <file>
git reset
|
查看被修改檔案的內容
1
| git diff source/_posts/os-1.md
|
Remote
push origin to remote
1 2 3 4
| git push -u origin backup
git push origin backup
|
fetch
1 2 3 4
| git fetch
git fetch origin backup
|
pull
1 2 3 4
| git pull
git pull origin/backup
|
add origin(remote server)
1
| git remote add origin https://github.com/hot5656/blog.git
|
change origin setting
1
| git remote set-url origin https://github.com/hot5656/blog.git
|
更改 遠端儲存庫名
1
| git remote rename pb paul
|
移除 遠端儲存庫
dump remote server setting
Other
dump log (commit 紀錄)
1 2 3
| git log
git log --oneline
|
merge
1 2 3 4
| git merge new-feature
git merge origin/master
|
remove 刪除檔案
1 2 3
| git rm home/week6/hw1/css/index.css.map
git rm -f home/week6/hw1/css/index.css.map
|
undo 1st commit - not lose data
undo 1 commit - not lose data
git 相關檔案
.gitignore
指定忽略的規則 : 資料庫的存取密碼, AWS 伺服器的存取金鑰, 或編譯產生檔案…
1 2 3 4 5 6 7 8
| secret.yml
config/database.yml
/db/*.sqlite3
*.tmp
|
.gitkeep
可提交一個空目錄