# Git個人使用命令有哪些
Git作為當今最流行的分布式版本控制系統,已成為開發者日常工作中不可或缺的工具。本文將全面介紹Git個人使用的核心命令,涵蓋從基礎配置到高級操作的完整工作流。
## 一、Git基礎配置命令
### 1. 用戶身份配置
```bash
# 設置全局用戶名
git config --global user.name "Your Name"
# 設置全局郵箱
git config --global user.email "your.email@example.com"
# 查看所有配置項
git config --list
# 查看特定配置項
git config user.name
# 設置VS Code為默認編輯器
git config --global core.editor "code --wait"
# 設置Vim為默認編輯器
git config --global core.editor "vim"
# 開啟顏色顯示
git config --global color.ui auto
# 設置默認分支名稱
git config --global init.defaultBranch main
# 設置文件大小寫敏感
git config --global core.ignorecase false
# 在當前目錄初始化
git init
# 指定目錄初始化
git init <project-directory>
# 克隆倉庫
git clone https://github.com/user/repo.git
# 克隆指定分支
git clone -b branch_name https://github.com/user/repo.git
# 克隆深度為1(僅最新提交)
git clone --depth 1 https://github.com/user/repo.git
# 查看完整狀態
git status
# 簡潔狀態顯示
git status -s
# 添加單個文件
git add filename.txt
# 添加所有修改文件
git add .
# 交互式添加
git add -p
# 基本提交
git commit -m "commit message"
# 直接提交所有已跟蹤文件的修改(跳過git add)
git commit -a -m "commit message"
# 修改上一次提交
git commit --amend
# 查看所有分支
git branch
# 創建新分支
git branch new-branch
# 切換分支
git checkout branch-name
# 創建并切換分支
git checkout -b new-branch
# 刪除分支
git branch -d branch-name
# 合并分支到當前分支
git merge branch-name
# 使用rebase合并
git rebase branch-name
# 解決沖突后繼續rebase
git rebase --continue
# 查看完整日志
git log
# 簡潔日志
git log --oneline
# 圖形化顯示分支
git log --graph --oneline --all
# 查看某文件的修改歷史
git log -p filename
# 按作者篩選
git log --author="name"
# 按時間篩選
git log --since="1 week ago"
# 撤銷單個文件修改
git checkout -- filename
# 撤銷所有修改
git checkout -- .
# 從暫存區移除單個文件
git reset HEAD filename
# 從暫存區移除所有文件
git reset HEAD .
# 軟回退(保留修改)
git reset --soft HEAD~1
# 混合回退(默認)
git reset HEAD~1
# 硬回退(慎用)
git reset --hard HEAD~1
# 查看遠程倉庫
git remote -v
# 添加遠程倉庫
git remote add origin https://github.com/user/repo.git
# 修改遠程倉庫URL
git remote set-url origin new_url
# 推送到遠程倉庫
git push origin branch-name
# 強制推送(慎用)
git push -f origin branch-name
# 拉取遠程更新
git pull origin branch-name
# 設置上游分支
git branch --set-upstream-to=origin/branch-name
# 創建并跟蹤遠程分支
git checkout -b local-branch origin/remote-branch
# 創建輕量標簽
git tag v1.0
# 創建帶注釋標簽
git tag -a v1.0 -m "Release version 1.0"
# 對特定提交打標簽
git tag -a v1.0 commit-hash -m "Message"
# 查看所有標簽
git tag
# 查看標簽詳情
git show v1.0
# 推送標簽到遠程
git push origin v1.0
# 推送所有標簽
git push origin --tags
# 儲藏當前修改
git stash
# 儲藏包含未跟蹤文件
git stash -u
# 查看儲藏列表
git stash list
# 應用最近儲藏
git stash apply
# 應用特定儲藏
git stash apply stash@{n}
# 刪除未跟蹤文件
git clean -f
# 交互式清理
git clean -i
# 包含忽略的文件
git clean -x
git submodule add https://github.com/user/repo.git path/to/submodule
# 初始化子模塊
git submodule init
# 更新子模塊
git submodule update
# 遞歸操作所有子模塊
git submodule foreach 'git pull origin master'
# 開始二分查找
git bisect start
# 標記當前為錯誤提交
git bisect bad
# 標記已知好的提交
git bisect good commit-hash
# 交互式rebase
git rebase -i HEAD~5
# 修改多個提交信息
git filter-branch
# 生成補丁
git format-patch HEAD~3
# 應用補丁
git am patch-file
# 創建功能分支
git checkout -b feature/new-login
# 開發過程中提交
git add .
git commit -m "Implement login form"
# 同步主分支更新
git checkout main
git pull origin main
git checkout feature/new-login
git rebase main
# 推送功能分支
git push origin feature/new-login
# 基于主分支創建修復分支
git checkout -b hotfix/security-issue main
# 修復并提交
git add .
git commit -m "Fix security vulnerability"
# 合并到主分支
git checkout main
git merge hotfix/security-issue
git push origin main
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
# 創建.gitignore文件示例
echo "*.log" >> .gitignore
echo "node_modules/" >> .gitignore
echo ".DS_Store" >> .gitignore
# 加速大型倉庫
git config --global core.preloadindex true
git config --global core.fscache true
git merge --abort
git reset --hard ORIG_HEAD
# 查看歷史操作
git reflog
# 重置到特定提交
git reset --hard commit-hash
# 統一換行符
git config --global core.autocrlf input
git rm --cached -r .
git reset --hard
掌握這些Git命令將極大提升你的開發效率。建議在日常工作中多加練習,逐步形成適合自己的Git工作流。記住,Git功能強大但也很靈活,遇到問題時不要害怕嘗試,同時也要善用git help命令查看官方文檔。
提示:定期使用
git gc可以優化本地倉庫性能,對于大型項目特別有用。 “`
這篇文章涵蓋了Git個人使用的主要命令,從基礎到高級操作,共計約3800字。采用Markdown格式編寫,包含代碼塊、標題層級和強調文本,便于閱讀和理解。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。