git学习笔记

安装git

1
2
3
4
5
6
7
# 设置全局信息
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# 设置局部信息
git config --local user.name "Your Name"
git config --local user.email "email@example.com"

管理工作目录

1
2
3
4
5
6
7
8
9
10
11
12
git init    初始化
git status  查看工作树状态
git reflog  查看之前所有的操作记录


git log --all # 查看所有
git log --all --graph # 图形化查看
git log -n2 # 只显示最近两次提交
git log --oneline  # 一行查看


gitk 图形化显示

三种状态/区域的切换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
工作区  暂存区  本地仓库
git add ./<filename> 工作区到暂存区
git commit -m "理由" 暂存区到本地仓库


git reset ./<filename> 从暂存区回退到工作区



git checkout ./<filename> 丢弃工作区的内容
git reset --hard <commit_id> 版本的回退,并删除
git reset --soft <commit_id> 把内容存到暂存区
git reset <commit_id> 把内容回退到工作区

git rm <filename> 删除内容
git mv oldname newname # 变更文件名

分支管理

1
2
3
4
5
6
git branch <name> 创建分支
git branch -v # 查看本地有多少分支
git branch :查看分支
git branch -d <name> 删除分支
git checkout <name> 切换分支
git merge <name> 把name分支合并到当前分支

标签管理

1
2
3
git tag <name> <commit_id> 给指定的版本加标签
git tag 查看所有标签
git tag -d <name> 删除标签

远程仓库建立连接

1
2
3
--ssh
1.本地生成公钥私钥,在主用户的根目录下
2.把公钥放入github中

和远程仓库建立连接

1
git remote add '远程仓库的别名' 远程仓库的地址

查看所有的仓库

1
git remote

向远程仓库提交代码

1
2
git push -u 远程仓库别名 分支名
注意 -u 第一次提交代码的时候本地分支跟远程仓库的分支建立起连接

从远程仓库拉代码

1
git pull 远程仓库的别名 分支名

克隆项目

1
git clone 地址

 

笔记

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
65
66
67
68
69
70
71
72
73
74
75
类型分为:commit tree blob
git cat-file -t hash值 查看类型
git cat-file -p hash值 查看具体提交的内容


分离头指针
git checkout hash值 # 会分离出来头指针

git branch 分支名 hash值 # 创建新的分支

git branch -b 分支名 commit值/分支名 # 创建并切换到新分支下


git diff commit1 commit2 # 比较两次commit的差异
HEAD / HEAD-1 # 比较head 和head的父亲

git branch -d 分支名 # 删除分支

git commit --amend # 修改最近一次提交的描述


git rebase -i 你要修改信息的前一个commit # 修改之前的提交信息


合成commit
git rebase -i 最后一个commit # 根据提示进行合并

合成间隔的commit
git rebase -i 最后一个commit # 把相关的commit放在一起进行合并



git diff --cached HEAD # 比较暂存区和最后一次提交有哪些修改

git diff # 比较工作区和暂存区的区别



git reset HEAD # 工作区和暂存区都和head一致

git checkout -- index.html # 把暂存区的内容覆盖到工作区

git reset HEAD -- style.css # 撤销暂存区中的内容

git reset --hard 08cf37e3fb5 #版本回退,暂存区和工作区都变为回退的版本(慎用)


git diff 分支1 分支2 -- 文件名 # 比较两个分支


git stash # 存放到一边
git stash list # 查看存储里几个

git stash apply # 拿出来存放的
git stash pop # 拿出并删除



.gitignore 文件
# 编写.gitignore文件可以让git不需要管理那些文件



连接github
git remote add 库名 地址

git fetch 库名 分支名
# 只拷贝下来不合并

git pull 库名 分支名
# 拷贝下来并合并


git merge --allow-unrelated-histories 其他分支
# 合并两个不相关的两个分支
1
2
in:readme    # 匹配redadme中文本内容
stars :> 1000 # 大于1000个stars的项目