Git 从入门到精通命令总结
一、配置 Git
1 2 3 4 5 6 7 8 9
| git config --global user.name "Your Name" git config --global user.email "[email protected]"
git config --list
git config --global core.editor "vim"
|
二、创建和初始化仓库
1 2 3 4 5
| git init
git clone <repository_url>
|
三、基本操作
1. 添加文件到暂存区
1 2 3 4 5
| git add <file>
git add .
|
2. 提交到本地仓库
1 2 3 4 5
| git commit -m "Commit message"
git commit -a -m "Commit message"
|
3. 查看状态与日志
1 2 3 4 5 6 7 8
| git status
git log
git log --oneline
|
4. 比较修改
1 2 3 4 5
| git diff
git diff --cached
|
5. 删除与恢复文件
1 2 3 4 5 6 7 8
| git rm <file>
git restore <file>
git restore --staged <file>
|
四、分支管理
1. 创建与切换分支
1 2 3 4 5 6 7 8
| git branch <branch_name>
git checkout <branch_name>
git checkout -b <branch_name>
|
2. 查看分支
1 2 3 4 5
| git branch
git branch -r
|
3. 合并分支
1 2
| git merge <branch_name>
|
4. 删除分支
1 2 3 4 5
| git branch -d <branch_name>
git branch -D <branch_name>
|
五、远程仓库
1. 管理远程仓库
1 2 3 4 5 6 7 8
| git remote -v
git remote add <name> <url>
git remote remove <name>
|
2. 拉取与推送
1 2 3 4 5
| git pull <remote> <branch>
git push <remote> <branch>
|
3. 克隆仓库
1 2
| git clone <repository_url>
|
六、标签管理
1. 创建标签
1 2 3 4 5
| git tag <tag_name>
git tag -a <tag_name> -m "Tag message"
|
2. 查看与删除标签
1 2 3 4 5
| git tag
git tag -d <tag_name>
|
3. 推送标签到远程仓库
1 2 3 4 5
| git push <remote> <tag_name>
git push <remote> --tags
|
七、高级操作
1. 暂存修改
1 2 3 4 5 6 7 8 9 10 11
| git stash
git stash list
git stash pop
git stash apply
|
2. 修改历史
1 2 3 4 5
| git commit --amend -m "New commit message"
git rebase -i <commit_hash>
|
3. 回滚
1 2 3 4 5
| git revert <commit_hash>
git reset --hard <commit_hash>
|
八、其他有用的命令
1 2 3 4 5 6 7 8
| git blame <file>
git log -p <file>
git shortlog
|
总结
基本上通过这些命令,可以逐步掌握 Git 的基本操作及其进阶功能,从而高效管理代码版本。