本文最后更新于 2025年8月9日 上午
Git 常用命令总结
Git 是目前最流行的分布式版本控制系统,掌握 Git 的基本操作对于开发者来说至关重要。本文将介绍 Git 的常用命令,帮助你快速上手 Git。
基本配置
1 2 3 4 5 6 7 8
| git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list
|
创建仓库
1 2 3 4 5 6 7 8
| git init
git clone <repository_url>
git clone -b <branch_name> <repository_url>
|
基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git status
git add <file_name> git add .
git commit -m "commit message"
git log git log --oneline git log --graph
|
分支管理
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
| git branch git branch -v git branch -a
git branch <branch_name>
git checkout <branch_name> git switch <branch_name>
git checkout -b <branch_name> git switch -c <branch_name>
git branch -d <branch_name> git branch -D <branch_name>
git merge <branch_name>
git rebase <branch_name>
|
远程操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| git remote -v
git remote add <remote_name> <repository_url>
git fetch <remote_name>
git pull <remote_name> <branch_name>
git push <remote_name> <branch_name> git push -u <remote_name> <branch_name>
git push <remote_name> --delete <branch_name>
|
撤销操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git checkout -- <file_name>
git reset HEAD <file_name>
git commit --amend
git reset --hard <commit_id>
git revert <commit_id>
|
标签管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git tag
git tag <tag_name> git tag -a <tag_name> -m "tag message"
git push <remote_name> <tag_name> git push <remote_name> --tags
git tag -d <tag_name> git push <remote_name> :refs/tags/<tag_name>
|
其他实用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| git diff git diff <file_name> git diff <commit_id1> <commit_id2>
git stash git stash list git stash apply git stash drop
git blame <file_name>
|
总结
这些 Git 命令涵盖了日常开发中的大部分需求。随着使用经验的增加,你会逐渐掌握更多高级用法。记住,Git 是一个强大的工具,熟练使用它将大大提高你的开发效率。
建议初学者多实践这些命令,遇到问题时可以使用 git help <command>
查看详细帮助文档。