Git 是分布式版本控制系统,GitHub 是一个基于 Git 的代码托管平台。本文总结常用的 Git 命令。
一、创建仓库
1 2 3 4 5
| cd /path/to/your/project
git init
|
配置用户名和邮箱
1 2
| git config --global user.name "你的GitHub用户名" git config --global user.email "你的GitHub注册邮箱"
|
生成 SSH 密钥
1
| ssh-keygen -t rsa -C "你的邮箱"
|
验证密钥
关联远程仓库
1 2 3 4 5
| git remote add origin 远程仓库地址
git remote rm origin
|
拉取/克隆仓库
1 2 3 4 5
| git pull origin master --allow-unrelated-histories
git clone 远程仓库地址
|
二、提交与拉取
1 2 3 4 5 6 7 8 9 10 11
| git add 文件名
git add .
git commit -m "提交备注"
git pull origin master
|
忽略文件
创建 .gitignore 文件:
三、查看命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git status
git log
git log --pretty=oneline
git reflog
git diff
|
四、版本回退
1 2 3 4 5 6 7 8 9 10 11
| git reset --hard HEAD^
git reset --hard HEAD~100
git reset --hard 版本号
git checkout -- 文件名
|
五、分支管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| git branch
git branch 分支名
git checkout 分支名
git checkout -b 分支名
git branch -d 分支名
git push origin --delete 分支名
git merge 要合并的分支名
git log --graph
|
六、标签管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git tag 标签名
git tag
git checkout 标签名
git push origin 标签名
git fetch origin tag 标签名
git tag -d 标签名
git push origin :refs/tags/标签名
|