GIT.xmind

1、获取SSH-Key

生成SSH-Key

  1. $ ssh-keygen -t rsa -C "youremail@youremail.com"
  2. # Generating public/private rsa key pair...
  3. # 三次回车即可生成 ssh key

查看SSH-Key

  1. $ cat ~/.ssh/id_rsa.pub
  2. # ssh-rsa AAAAB3NzaC1yc2E... youremail@youremail.com

2、设置用户信息

  1. $ git config --global user.name "yourname"
  2. $ git config --global user.email "youremail@youremail.com"

3、初始化本地库

客户仓库

  1. $ git clone <repository-url>

本地添加远程仓库

  1. $ git init
  2. $ git remote add origin <远程仓库地址>

4、更新到远程仓库

  1. $ git add . # 指定更新内容 . 表示全部更新,test.txt 表示更新指定文件
  2. $ git commit -m "一些注释说明" # 添加更新说明
  3. $ git push origin master # 执行更新操作
  4. $ git push -f origin master # 强制推送

工作区到暂存区 -> add

从暂存区取消暂存 -> restore —staged

工作区更改,未暂存,取消工作区的更改 -> restore

5、单个文件回退

1、查看文件的更改日志

1、查看文件的全部更改日志

  1. $ git log [file name]

2、查看文件最近n条日志

  1. $ git log -n [file name]

2、文件已修改,未暂存(未执行add)

  1. $ git restore [file name] //执行后直接更改工作区

3、文件已修改,已暂存(已执行add,但是未commit)

1、取消暂存

  1. $ git restore --staged [file name]

或者使用

  1. $ git reset HEAD [file name]
  2. $ git reset [file name]

2、恢复到工作区

  1. $ git restore [file name]

4、文件已提交,回退到某一个提交节点

1、先获取要回退的commit-id

  1. $ git log

或者

  1. $ git log --graph

2、从本地版本库中拉取到暂存区

  1. $ git reset <commit-id> [file name]

执行后查看status,会出现一个暂存状态,一个更改状态

暂存区现在存放的是回退前的状态

更改状态是因为本地工作区与暂存区里的内容不一致

3、放弃暂存,恢复到回前状态

直接执行add

或者执行

  1. $ git restore --staged [file name]

放弃暂存区

4、放弃本地的更改

放弃本地的更改,使回退生效

  1. $ git restore [file name]

但是暂存区的更改还存在,再执行一次add和commit

6、分支操作

1、展示本地分支关联远程的操作

  1. $ git branch -vv

2、列出所有远程分支

  1. $ git branch -r

4、列出本地和远程的分支

  1. $ git brach -a

5、查看本地分支与远程的关系

  1. $ git remote show origin

6、查看本地分支

  1. $ git branch

7、创建本地分支

  1. $ git branch <branch name>

8、创建本地分支并切换到该分支

  1. $ git checkout -b <branch name>

9、删除本地分支

  1. $ git branch -d <branch name>

10、切换分支

  1. $ git checkout <branch name>

  1. $ git switch <branch name>

11、创建远程分支

  1. $ git push origin <local branch name>:<remote branch name>

12、删除远程分支

  1. $ git push origin :<remote branch name> // 推送一个空的本地分支到远程分支上,相当于删除

  1. $ git push origin --delete <remote branch name>

13、关联远程分支

  1. $ git branch set-upstream <local branch name> origin/<remote branch name>

7、忽略文件

.gitignore 文件用以列出忽略的文件。

1、格式规范

  • 所有空行或者以 # 开头的行都会被GIT忽略;
  • 可以使用标准的glob 模式匹配,它会递归地应用在整个工作区中;
  • 匹配模式可以以 / 开头防止递归;
  • 匹配模式可以以 / 结尾指定目录;
  • 要忽略指定模式以外的文件或目录,可以在模式前加上叹号 ! 取反;

glob模式:

  • * : 匹配多个任意字符;
  • [abc] : 匹配 [ ] 里所列出的任意字符。[abc] 要么匹配 a,要么匹配 b, 要么匹配 c;
  • ? : 只匹配一个字符;
  • [0-9] : 匹配所有从0到9之间的数字;
  • ** : 匹配任意中间的目录。a/**/c 匹配 a 文件夹下所有文件夹 c;

2、例子

此部分参考于 《Pro Git》。 访问链接, 查看不同语言的忽略文件

  1. # 忽略所有的 .a 文件
  2. *.a
  3. # 但跟踪所有的 lib.a,即便你在前面忽略了 .a 文件
  4. !lib.a
  5. # 只忽略当前目录下的 TODO 文件,而不忽略 subdir/TODO
  6. /TODO
  7. # 忽略任何目录下名为 build 的文件夹
  8. build/
  9. # 忽略 doc/notes.txt,但不忽略 doc/server/arch.txt
  10. doc/*.txt
  11. # 忽略 doc/ 目录及其所有子目录下的 .pdf 文件
  12. doc/**/*.pdf

8、git log

oneline

将每个commit压缩成一行显示。

  1. $ git log --oneline

image.png

显示变更

详细显示每次提交的差异。

  1. $ git log -p

列出每次提交变更的文件。

  1. $ git log -stat

shortlog

列出所有分支的所有commit 的信息。

  1. $ git shortlog

image.png

graph

图形化的方式显示迭代过程。

  1. $ git log --graph