一般来说,日常使用只要记住下图 6 个命令,就可以了。但是熟练使用,恐怕要记住 60 ~ 100 个命令~。
![image-20211217102843197](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
名称 | 解释 |
---|
Workspace | 工作区 |
Index / Stage | 暂存区 |
Repository | 仓库区(本地仓库) |
Remote | 远程仓库 |
创建公钥
1
| ssh-keygen -t rsa -C "your email-address"
|
查看公钥
1 2
| cd ~/.ssh cat id_rsa.pub
|
新建代码库
1 2 3 4 5 6 7 8
| $ git init
$ git init <project-name>
$ git clone <url>
|
修改仓库地址方法
1 2 3 4 5 6 7 8 9 10
| $ git remote -v
$ git remote set-url origin <url>
$ git remote rm origin $ git remote add origin <url>
|
配置(.git 文件夹中)
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 config --list
$ git config -e <--global>
$ git config <--global> user.name "huoyou" $ git config <--global> user.email "huoyou1234@gmail.com"
//查看自己的用户名和邮箱地址: $ git config user.name $ git config user.email
//修改自己的用户名和邮箱地址: $ git config --system user.name "huoyou" $ git config --system user.email "huoyou1234@gmail.com"
$ git config --global user.name "huoyou" $ git config --global user.email "huoyou1234@gmail.com"
$ git config --local user.name "huoyou" $ git config --local user.email "huoyou1234@gmail.com"
// 优先级 local > global > system
|
增加、删除文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| $ git add <file1> <file2> ...
$ git add <dir>
$ git add .
$ git add -p
$ git rm <file1> <file2> ...
$ git rm --cached <file>
$ git mv <file-original> <file-renamed>
|
提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| $ git commit -m <message>
$ git commit <file1> <file2> ... -m <message>
$ git commit -a
$ git commit -v
$ git commit --amend -m <message>
$ git commit --amend <file1> <file2> ...
|
分支
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| $ git branch
$ git branch -r
$ git branch -a
$ git branch <branch-name>
$ git checkout -b <branch>
$ git branch <branch> <commit>
$ git branch --track <branch> <remote-branch>
$ git checkout <branch-name>
$ git checkout -
$ git branch --set-upstream <branch> <remote-branch>
$ git merge <branch>
$ git cherry-pick <commit>
$ git branch -d <branch-name>
$ git push origin --delete <branch-name> or $ git branch -dr <remote/branch-name>
|
合并
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 26 27
|
首先切换到dev分支: $ git checkout dev
检测是否有为提交内容: $ git status
将未提交内容添加到暂存区: $ git add .(或git add 具体文件名称)
将暂存区内容提交到本地版本库: $ git commit -m "本次提交内容说明"
推送到远程: $ git push origin dev:dev (推送成功后,在远程可以看到已经新建了一个dev分支)
本地切换到master分支上: $ git checkout master
合并dev分支到master上: $ git merge dev, (看有无冲突,有冲突要解决冲突)
合并完成后,推送到远程: $ git push origin master
|
标签
标签是某个版本的别名,因为 Git 的版本号都是用一串字母数字组成,为了便于管理,Git 可以给版本取个别名(也就是打上标签,比如标签的名字叫做 v1.0.0)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 创建标签: $ git tag v1.0.0
对过去的分支打标签: $ git tag v2.0.0 <commitId>
查看所有标签: $ git tag
展示某个标签详细信息: $ git show <tagname>
删除标签: $ git tag -d <tagname>
推送标签至远程: $ git push origin <tagname>
删除远程标签: 1. 先从本地删除 $ git tag -d <tagname> 2. 从远程删除 $ git push origin :refs/tags/<tagname>
|
查看信息
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| $ git status
$ git log
$ git log --stat
$ git log -S <keyword>
$ git log <tag> HEAD --pretty=format:%s
$ git log <tag> HEAD --grep feature
$ git log --follow <file> $ git whatchanged <file>
$ git log --pretty=oneline <文件路径>文件名 $ git show <修改记录标识>
$ git log -p <file>
$ git log -5 --pretty --oneline
$ git shortlog -sn
$ git blame <file>
$ git diff
$ git diff --cached <file>
$ git diff HEAD
$ git diff <first-branch>...<second-branch>
$ git diff --shortstat "@{0 day ago}"
$ git show <commit>
$ git show --name-only <commit>
$ git show <commit>:<filename>
$ git reflog
|
远程同步
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 26
| $ git fetch <remote>
$ git remote -v
$ git remote show <remote>
$ git remote add <shortname> <url>
$ git pull <remote> <branch>
$ git push <remote> <branch>
$ git push <remote> --force
$ git push <remote> --all
$ git push --set-upstream origin <分支名称>
|
撤销
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| $ git checkout <file>
$ git checkout <commit> <file>
$ git checkout .
$ git revert <commit>
$ git stash $ git stash pop
$ git reset <file>
$ git reset --hard
$ git reset <commit>
$ git reset --hard <commit>
git reset --soft HEAD^
git reset --soft HEAD~1
$ git reset --keep <commit>
--mixed 意思是:不删除工作空间改动代码,撤销commit,并且撤销git add . 操作 这个为默认参数,git reset --mixed HEAD^ 和 git reset HEAD^ 效果是一样的。
--soft 不删除工作空间改动代码,撤销commit,不撤销git add .
--hard 删除工作空间改动代码,撤销commit,撤销git add . 注意完成这个操作后,就恢复到了上一次的commit状态。
git commit --amend
|
变基
查看当前提交记录:
![image-20211217104657662](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
选择需要合并项的下一个 commitId:
![image-20211217104802650](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
i 进入输入模式,将需要被合并的改为 s, 第一项不能改为 s:
![image-20211217104837195](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
esc 退出编辑模式,:wq 保存退出
![image-20211217104903818](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
esc 退出编辑模式,:wq 保存退出
1
| git push origin master -f
|
git log
![image-20211217104948331](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)