# Git命令的介紹及使用
## 1. Git簡介
Git是一個開源的分布式版本控制系統,由Linus Torvalds于2005年為Linux內核開發而設計。它具有以下顯著特點:
- **分布式架構**:每個開發者都有完整的代碼倉庫副本
- **高效性能**:設計時就考慮了大型項目的性能需求
- **強大的分支管理**:創建和切換分支幾乎瞬間完成
- **數據完整性**:采用SHA-1哈希確保數據不可篡改
## 2. 安裝與配置
### 2.1 安裝Git
不同操作系統的安裝方法:
```bash
# Ubuntu/Debian
sudo apt-get install git
# CentOS/RHEL
sudo yum install git
# macOS (使用Homebrew)
brew install git
# Windows
# 下載官方安裝包:https://git-scm.com/download/win
安裝后首先需要配置用戶信息:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
常用配置選項:
# 設置默認編輯器為VSCode
git config --global core.editor "code --wait"
# 開啟顏色顯示
git config --global color.ui auto
# 查看所有配置
git config --list
# 初始化新倉庫
git init
# 克隆現有倉庫
git clone https://github.com/user/repo.git
# 克隆指定分支
git clone -b branch_name https://github.com/user/repo.git
Git文件生命周期:
未跟蹤(untracked) → 已跟蹤(tracked)
已跟蹤 → 已修改(modified) → 已暫存(staged) → 已提交(committed)
相關命令:
# 查看當前狀態
git status
# 添加文件到暫存區
git add file.txt
git add . # 添加所有文件
# 提交更改
git commit -m "描述信息"
# 一次性添加并提交
git commit -am "描述信息"
# 刪除文件
git rm file.txt
# 重命名文件
git mv old.txt new.txt
# 基本日志
git log
# 簡潔日志
git log --oneline
# 圖形化顯示分支
git log --graph --all --oneline
# 顯示文件修改歷史
git log -p file.txt
# 顯示某作者的提交
git log --author="name"
# 按時間篩選
git log --since="1 week ago"
# 查看分支
git branch
# 創建分支
git branch new_feature
# 切換分支
git checkout branch_name
# 或
git switch branch_name
# 創建并切換分支
git checkout -b new_branch
# 刪除分支
git branch -d branch_name # 安全刪除
git branch -D branch_name # 強制刪除
# 重命名當前分支
git branch -m new_name
# 合并分支
git merge branch_name
# 變基操作
git rebase main
# 解決沖突后標記為已解決
git add conflicted_file.txt
git commit
沖突文件示例:
<<<<<<< HEAD
當前分支內容
=======
要合并的分支內容
>>>>>>> branch_name
# 查看遠程分支
git branch -r
# 跟蹤遠程分支
git checkout --track origin/remote_branch
# 推送本地分支到遠程
git push -u origin local_branch
# 刪除遠程分支
git push origin --delete branch_name
# 添加遠程倉庫
git remote add origin https://github.com/user/repo.git
# 查看遠程倉庫
git remote -v
# 獲取遠程更新
git fetch origin
# 拉取并合并
git pull origin main
# 推送到遠程
git push origin main
典型Git Flow:
# 推薦的工作流程示例
git checkout -b feature/login
# ...開發代碼...
git add .
git commit -m "實現登錄功能"
git checkout main
git pull origin main # 獲取最新代碼
git checkout feature/login
git rebase main # 變基到最新代碼
git push origin feature/login
# 然后在GitHub創建PR
# 撤銷工作區修改
git checkout -- file.txt
# 撤銷暫存區修改
git reset HEAD file.txt
# 修改最后一次提交
git commit --amend
# 回退到指定提交
git reset --hard commit_hash
# 創建回退提交
git revert commit_hash
# 儲藏當前修改
git stash
# 查看儲藏列表
git stash list
# 恢復最近儲藏
git stash pop
# 應用特定儲藏
git stash apply stash@{n}
# 刪除儲藏
git stash drop stash@{n}
# 添加子模塊
git submodule add https://github.com/user/repo.git path/to/submodule
# 克隆包含子模塊的項目
git clone --recurse-submodules https://github.com/user/main_repo.git
# 更新子模塊
git submodule update --init --recursive
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
文件示例:
# 忽略所有.class文件
*.class
# 忽略特定文件
config.ini
# 忽略目錄
/target/
/node_modules/
# 但包含特定文件
!important.class
git bisect start
git bisect bad # 標記當前為錯誤版本
git bisect good v1.0 # 標記已知好的版本
# Git會自動檢出中間版本,測試后標記good或bad
git bisect reset # 結束二分查找
提交規范:
分支策略:
日常習慣:
團隊協作:
Q: 如何恢復已刪除的分支?
A: 可以通過以下步驟恢復:
# 查找刪除分支的最后提交
git reflog
# 從提交創建新分支
git branch recovered_branch commit_hash
Q: 如何修改多個提交信息?
A: 使用交互式變基:
git rebase -i HEAD~3 # 修改最近3個提交
Q: 如何清理歷史中的大文件?
A: 使用BFG Repo-Cleaner或git filter-branch:
git filter-branch --tree-filter 'rm -f large_file' HEAD
Git作為現代軟件開發的核心工具,掌握其命令和工作流程對開發者至關重要。本文涵蓋了從基礎到高級的Git操作,建議讀者:
通過持續練習和使用,Git將成為你開發過程中不可或缺的得力助手。 “`
這篇文章大約2900字,采用Markdown格式編寫,包含了Git的主要命令和使用方法,結構清晰,適合作為Git入門和參考指南。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。