Git 本地常見快捷操作
📌 1. 基本操作
操作 | 命令 |
---|---|
初始化 Git 倉庫 | git init |
查看 Git 狀態 | git status |
添加所有文件到暫存區 | git add . |
添加指定文件 | git add <file> |
提交更改 | git commit -m "提交信息" |
修改最后一次提交信息 | git commit --amend -m "新提交信息" |
顯示提交歷史 | git log --oneline --graph |
顯示修改的文件 | git diff |
📌 2. 分支管理
操作 | 命令 |
---|---|
查看當前分支 | git branch |
創建新分支 | git branch <branch-name> |
切換分支 | git checkout <branch-name> |
創建并切換到新分支 | git checkout -b <branch-name> |
刪除本地分支 | git branch -d <branch-name> |
強制刪除本地分支 | git branch -D <branch-name> |
📌 3. 遠程倉庫
操作 | 命令 |
---|---|
查看遠程倉庫 | git remote -v |
添加遠程倉庫 | git remote add origin <repo-url> |
刪除遠程倉庫 | git remote remove origin |
推送到遠程倉庫 | git push origin <branch-name> |
拉取遠程分支 | git pull origin <branch-name> |
獲取遠程更新但不合并 | git fetch origin |
📌 4. 回退與撤銷
操作 | 命令 |
---|---|
撤銷 git add 操作 | git reset HEAD <file> |
撤銷最后一次提交 | git reset --soft HEAD~1 |
強制回退到某個提交 | git reset --hard <commit-hash> |
撤銷對文件的修改 | git checkout -- <file> |
📌 5. 臨時存儲
操作 | 命令 |
---|---|
保存未提交的更改 | git stash |
查看存儲的更改 | git stash list |
恢復最近的存儲 | git stash pop |
應用某個存儲項 | git stash apply stash@{0} |
刪除某個存儲項 | git stash drop stash@{0} |
📌 6. 其他常用命令
操作 | 命令 |
---|---|
查看某個文件的修改歷史 | git log -- <file> |
查看某次提交的詳細信息 | git show <commit-hash> |
配置用戶名 | git config --global user.name "你的名字" |
配置郵箱 | git config --global user.email "你的郵箱" |
列出所有別名 | git config --global --list |
清除所有未跟蹤的文件(慎用) | git clean -fd |