0%

git 学习笔记

Git 安装及配置

  1. 安装 Git
  • 在 Windows 上安装
    在 Windows 上安装 Git 也有几种安装方法。 官方版本可以在 Git 官方网站下载。 打开 https://git-scm.com/download/win,下载会自动开始。 要注意这是一个名为 Git for Windows 的项目(也叫做 msysGit),和 Git 是分别独立的项目;更多信息请访问 http://msysgit.github.io/。
    要进行自动安装,你可以使用 Git Chocolatey 包。 注意 Chocolatey 包是由社区维护的。
    另一个简单的方法是安装 GitHub Desktop。 该安装程序包含图形化和命令行版本的 Git。 它也能支持 Powershell,提供了稳定的凭证缓存和健全的换行设置。 稍后我们会对这方面有更多了解,现在只要一句话就够了,这些都是你所需要的。 你可以在 GitHub for Windows 网站下载,网址为 GitHub Desktop 网站。
  • 其他系统可以参考官方文档
  • 安装完成之后查看版本号
    1
    2
    3
    C:\Users\Administrator>git --version
    git version 2.26.0.windows.1
    C:\Users\Administrator>
  • 可以使用 git help 查看 git 常用的命令,使用 git help -a 查看 git 可用的所有命令
  1. 配置信息
  • 添加配置
    1
    2
    $ git config [--local | --global | --system] user.name 'Your name'
    $ git config [--local | --global | --system] user.email 'Your email'
  • 查看配置
    1
    $ git config --list [--local | --global | --system]
  • 清除配置
1
2
3
$ git config --unset --local user.name
$ git config --unset --global user.name
$ git config --unset --system user.name
  • 参数说明
    local:区域为本仓库,缺省默认。
    global:当前用户的所有仓库。
    system:对系统所有登录的用户有效。
  • 优先级

local > global > system

建 Git 仓库

  1. 用 Git 之前已经有项目代码
1
2
$ cd 项目代码所在的文件夹
$ git init
  1. 用 Git 之前还没有项目代码
1
2
3
$ cd 某个文件夹
$ git init your_project #会在当前路径下创建和项目名称同名的文件夹
$ cd your_project
  1. 拉取远程仓库到本地
1
$ git clone 仓库地址

认识工作区和暂存区

git-command

说明:

  • workspace:工作区
  • staging area:暂存区
  • local repository:本地仓库
  • remote repository:远程仓库

添加文件到暂存区

添加单个文件:git add 指定的文件名

添加所有文件:git add .

忽略的文件:.gitignore 中指定的文件以及空目录会被忽略

提交到本地仓库

1
git commit -m "commit message" 
  • 对最近提交的 commit 信息进行修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ git log -1
commit 91b8f2907fccd4848b36f3ac47feb37e857ccccb (HEAD -> master)
Author: userName
Date: Sat Sep 26 18:29:47 2020 +0800

Move readme to readme.md

# 对最近提交的信息进行修改
$ git commit --amend
[master 204a801] Move filename readme to readme.md
Date: Sat Sep 26 18:29:47 2020 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme => readme.md (100%)



$ git log -1
commit 204a8015f31de3d01979c9d181198be866caeb75 (HEAD -> master)
Author: userName
Date: Sat Sep 26 18:29:47 2020 +0800

Move filename readme to readme.md
  • 修改指定 commit 的提交信息 (只限于在自己工作区的操作,如果已经 push,需要慎重!)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$ git log -3
commit 204a8015f31de3d01979c9d181198be866caeb75 (HEAD -> master)
Author: userName
Date: Sat Sep 26 18:29:47 2020 +0800

Move filename readme to readme.md

commit 4a1df26fc4973b6ccd27ec7a9bd6c2e889227e26
Author: userName
Date: Sat Sep 26 16:57:29 2020 +0800

add refering projects

commit 6509a1f31f478efeafc1601ac878fcbb2bcd59c7 (tag: js01)
Author: userName
Date: Sat Sep 26 16:51:00 2020 +0800

add js

# 更新指定commit的提交信息 回车之后进入交互界面,
# 将需要修改的message前面的pick 改成 reword|r
# 保存之后进入下一个页面修改message信息进行保存。
$ git rebase -i 6509a1f31f47

r 4a1df26 add a refering projects
pick 204a801 Move filename readme to readme.md

# Rebase 6509a1f..204a801 onto 6509a1f (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.

# 修改message信息
add a refering projects

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Sat Sep 26 16:57:29 2020 +0800
#
# interactive rebase in progress; onto 6509a1f
# Last command done (1 command done):
# reword 4a1df26 add a refering projects
# Next command to do (1 remaining command):
# pick 204a801 Move filename readme to readme.md
# You are currently editing a commit while rebasing branch 'master' on '6509a1f'.
#
# Changes to be committed:
# modified: index.html
# modified: styles/style.css
#
# 保存之后提示修改成功 ** 此处用到了分离头指针 detached HEAD **
$ git rebase -i 6509a1f31f47
[detached HEAD 66a0265] add a refering projects
Date: Sat Sep 26 16:57:29 2020 +0800
2 files changed, 20 insertions(+), 1 deletion(-)
Successfully rebased and updated refs/heads/master.

# 修改之后commit id会有相应的变化。
$ git log
commit cb14cef0390ad96036a03671e72ac60e8c7dede1 (HEAD -> master)
Author: userName
Date: Sat Sep 26 18:29:47 2020 +0800

Move filename readme to readme.md

commit 0cb0cdbeadb7436ad73763b7fc39be04601868ee
Author: userName
Date: Sat Sep 26 16:57:29 2020 +0800

add a refering project

commit 6509a1f31f478efeafc1601ac878fcbb2bcd59c7 (tag: js01)
Author: userName
Date: Sat Sep 26 16:51:00 2020 +0800

add js

commit 832bbdc5af50920b335b269c4f319ed55d33b0db
Author: userName
Date: Sat Sep 26 16:49:33 2020 +0800

add style.css
  • 把连续的多个 commit 整理为 1 个
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 查看信息
$ git log --graph
* commit cb14cef0390ad96036a03671e72ac60e8c7dede1 (HEAD -> master)
| Author: userName
| Date: Sat Sep 26 18:29:47 2020 +0800
|
| Move filename readme to readme.md
|
* commit 0cb0cdbeadb7436ad73763b7fc39be04601868ee
| Author: userName
| Date: Sat Sep 26 16:57:29 2020 +0800
|
| add a refering project
|
* commit 6509a1f31f478efeafc1601ac878fcbb2bcd59c7 (tag: js01)
| Author: userName
| Date: Sat Sep 26 16:51:00 2020 +0800
|
| add js
|
* commit 832bbdc5af50920b335b269c4f319ed55d33b0db
| Author: userName
| Date: Sat Sep 26 16:49:33 2020 +0800
|
| add style.css
|
* commit 0db0317097726af89d5adb96e21e02504f62d26c
| Author: userName
| Date: Sat Sep 26 16:47:18 2020 +0800
|
| add index+log
|
* commit a3a2b8a63b911300a2e18fd2344a725a3e59b931
Author: userName
Date: Sat Sep 26 16:42:06 2020 +0800

add readme

# 修改commit
$ git rebase -i a3a2b8a63b

将以下内容
pick 0db0317 add index+log
pick 832bbdc add style.css
pick 6509a1f add js
pick 0cb0cdb add a refering project
pick cb14cef Move filename readme to readme.md
修改为
pick 0db0317 add index+log
s 832bbdc add style.css
s 6509a1f add js
s 0cb0cdb add a refering project
pick cb14cef Move filename readme to readme.md


# Rebase a3a2b8a..cb14cef onto 0cb0cdb (5 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.
#

# 说明变更理由
# This is a combination of 4 commits.
Create a complete web page
# This is the 1st commit message:

add index+log

# This is the commit message #2:

add style.css

# This is the commit message #3:

add js

# This is the commit message #4:

add a refering project

$ git rebase -i a3a2b8a63b
[detached HEAD 0ec7bf2] Create a complete web page
Date: Sat Sep 26 16:47:18 2020 +0800
4 files changed, 133 insertions(+)
create mode 100644 images/git-logo.png
create mode 100644 index.html
create mode 100644 js/script.js
create mode 100644 styles/style.css
Successfully rebased and updated refs/heads/master.

变更后
$ git log --graph
* commit 94e510605de89629b444f6d928d9bb6878875bbe (HEAD -> master)
| Author: userName
| Date: Sat Sep 26 18:29:47 2020 +0800
|
| Move filename readme to readme.md
|
* commit 0ec7bf205f199bd9184522bd7f0f2f6215055f0b
| Author: userName
| Date: Sat Sep 26 16:47:18 2020 +0800
|
| Create a complete web page
|
| add index+log
|
| add style.css
|
| add js
|
| add a refering project
|
* commit a3a2b8a63b911300a2e18fd2344a725a3e59b931
Author: userName
Date: Sat Sep 26 16:42:06 2020 +0800

add readme
  • 将间隔的几个 commit 整理为 1 个
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
将原内容
pick 0ec7bf2 Create a complete web page
pick 94e5106 Move filename readme to readme.md

修改为
# 需要合并的内容放到一起
pick a3a2b8a
s 94e5106 Move filename readme to readme.md
pick 0ec7bf2 Create a complete web page

保存之后有提示报错
$ git rebase -i a3a2b8a63b
interactive rebase in progress; onto a3a2b8a
Last command done (1 command done):
pick a3a2b8a
Next commands to do (2 remaining commands):
squash 94e5106 Move filename readme to readme.md
pick 0ec7bf2 Create a complete web page
(use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on 'a3a2b8a'.
(all conflicts fixed: run "git rebase --continue")

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

git commit --allow-empty

Otherwise, please use 'git rebase --skip'
Could not apply a3a2b8a...

# 使用git rebase --continue 继续运行
# 按照之前的输入修改说明

# 说明变更理由
# This is a combination of 4 commits.
Add readme.md
# This is the 1st commit message:

# 提示修改成功
$ git rebase --continue
[detached HEAD b659751] Add readme.md
Date: Sat Sep 26 16:42:06 2020 +0800
1 file changed, 2 insertions(+)
create mode 100644 readme.md
Successfully rebased and updated refs/heads/master.

$ git log --graph
* commit 205e0abd61d4f5e819036cc87c1858520f253afa (HEAD -> master)
| Author: userName
| Date: Sat Sep 26 16:47:18 2020 +0800
|
| Create a complete web page
|
| add index+log
|
| add style.css
|
| add js
|
| add a refering project
|
* commit b65975163d96edabe7e9bd2fee82e58753c160b3
Author: userName
Date: Sat Sep 26 16:42:06 2020 +0800

Add readme.md

add readme

Move filename readme to readme.md

建议提交遵循 commit message 规范:

每次提交,Commit message 都包括三个部分:Header,Body 和 Footer。

1
2
3
<type>(<scope>): <subject>  // 必须
<body> // 可省略
<footer> // 可省略

其中,Header 是必需的,Body 和 Footer 可以省略。
不管是哪一个部分,任何一行都不得超过 72 个字符(或 100 个字符)。这是为了避免自动换行影响美观。

Header 部分只有一行,包括三个字段:type(必需)、scope(可选)和 subject(必需)。

(1)type

type 用于说明 commit 的类别,只允许使用下面 7 个标识。

1
2
3
4
5
6
7
feat:新功能(feature)
fix:修补bug
docs:文档(documentation)
style: 格式(不影响代码运行的变动)
refactor:重构(即不是新增功能,也不是修改bug的代码变动)
test:增加测试
chore:构建过程或辅助工具的变动

备注:fix #任务 id 提交时可以直接关联华为云任务。

如果 typefeatfix,则该 commit 将肯定出现在 Change log 之中。其他情况(docschorestylerefactortest)由你决定,要不要放入 Change log,建议是不要。

(2)scope

scope 用于说明 commit 影响的范围,比如数据层、控制层、视图层等等,视项目不同而不同。

(3)subject

subject 是 commit 目的的简短描述,不超过 50 个字符。

1
2
3
以动词开头,使用第一人称现在时,比如change,而不是changed或changes
第一个字母小写
结尾不加句号(.)

查看工作区状态

1
2
3
$ git status
# 通常我们使用 -s 参数来获得简短的输出结果
$ git status -s

对比工作区文件变化

  • 尚未缓存的改动:git diff
  • 查看已缓存的改动: git diff –cached
  • 查看已缓存的与未缓存的所有改动:git diff HEAD
  • 显示摘要而非整个 diff:git diff –stat
1
2
3
4
5
6
7
8
9
10
# 显示暂存区和工作区的差异
$ git diff [file]

# 显示暂存区和上一次提交(commit)的差异:
$ git diff --cached [file]

$ git diff --staged [file]

# 显示两次提交之间的差异:
$ git diff [first-branch]...[second-branch]

Git 代码提交切换

重命名文件 git mv

1
2
3
4
# 重命名文件
$ git mv [file] [newfile]
# 如果新文件名已经存在,但还是要重命名它,可以使用 -f 参数:
$ git mv -f [file] [newfile]

删除文件 git rm

1
2
3
4
5
6
# 将文件从暂存区和工作区中删除:
$ git rm <file>
# 如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f
$ git rm -f <file>
# 如果想把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可:
$ git rm --cached <file>

查看提交历史 git log

  • git log - 查看历史提交记录。
  • git blame - 以列表形式查看指定文件的历史修改记录。
  • 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 查看日志
$ git log
commit 91b8f2907fccd4848b36f3ac47feb37e857ccccb (HEAD -> master)
Author: userName<userEmail>
Date: Sat Sep 26 18:29:47 2020 +0800

Move readme to readme.md

commit 4a1df26fc4973b6ccd27ec7a9bd6c2e889227e26
Author: userName<userEmail>
Date: Sat Sep 26 16:57:29 2020 +0800

add refering projects

#
$ git log --oneline
91b8f29 (HEAD -> master) Move readme to readme.md
4a1df26 add refering projects
6509a1f add js
832bbdc add style.css
0db0317 add index+log
a3a2b8a add readme

$ git log -n2 --oneline
91b8f29 (HEAD -> master) Move readme to readme.md
4a1df26 add refering projects

# 以拓扑图的形式查看
$ git log --all --graph
* commit 81a7b4e6882a4f7b696c205fc8339530faad2b68 (HEAD -> temp)
| Author: userName<userEmail>
| Date: Sat Sep 26 18:41:25 2020 +0800
| Date: Sat Sep 26 18:41:25 2020 +0800
* commit 81a7b4e6882a4f7b696c205fc8339530faad2b68 (HEAD -> temp)
| Author: userName<userEmail>
| Date: Sat Sep 26 18:41:25 2020 +0800
* commit 81a7b4e6882a4f7b696c205fc8339530faad2b68 (HEAD -> temp)
| Author: userName<userEmail>
| Date: Sat Sep 26 18:41:25 2020 +0800
|
| Add test
|
* commit 81a7b4e6882a4f7b696c205fc8339530faad2b68 (HEAD -> temp)
| Author: userName<userEmail>
| Date: Sat Sep 26 18:41:25 2020 +0800
| * commit 91b8f2907fccd4848b36f3ac47feb37e857ccccb (master)
| | Author: userName<userEmail>
| | Date: Sat Sep 26 18:29:47 2020 +0800
| |
| | Move readme to readme.md
| |
| * commit 4a1df26fc4973b6ccd27ec7a9bd6c2e889227e26
| | Author: userName<userEmail>
| | Date: Sat Sep 26 16:57:29 2020 +0800
| |
| | add refering projects
| |
| * commit 6509a1f31f478efeafc1601ac878fcbb2bcd59c7
|/ Author: userName<userEmail>
| Date: Sat Sep 26 16:51:00 2020 +0800
|
| add js
|
* commit 832bbdc5af50920b335b269c4f319ed55d33b0db
| Author: userName<userEmail>
| Date: Sat Sep 26 16:49:33 2020 +0800
|
| add style.css
|
* commit 0db0317097726af89d5adb96e21e02504f62d26c
| Author: userName<userEmail>
| Date: Sat Sep 26 16:47:18 2020 +0800
|
| add index+log
|
* commit a3a2b8a63b911300a2e18fd2344a725a3e59b931
Author: userName<userEmail>
Date: Sat Sep 26 16:42:06 2020 +0800

add readme

# 修改显示日期
$ git log --date=format:%Y-%m-%d\ %H:%M:%S
commit 4934416387b24367d59ced922895a8bd0544ab5f (HEAD -> master)
Author: userName
Date: 2020-09-25 14:12:31

新增 readme文件

$ git log --date=iso
commit 4934416387b24367d59ced922895a8bd0544ab5f (HEAD -> master)
Author: userName
Date: 2020-09-25 14:12:31 +0800

新增 readme文件

$ git log --date=short
commit 4934416387b24367d59ced922895a8bd0544ab5f (HEAD -> master)
Author: userName
Date: 2020-09-25

新增 readme文件

$ git log --oneline --all -n4 --graph
* 81a7b4e (HEAD -> temp) Add test
| * 91b8f29 (master) Move readme to readme.md
| * 4a1df26 add refering projects
| * 6509a1f add js
|/


# 通过web浏览器查看log命令
$ git help --web log

# 用 --reverse 参数来逆向显示所有日志
$ git log --reverse --oneline

# 查找指定用户的提交日志可以使用命令:
$ git log --author=userName --oneline -5

# 查看指定文件的修改记录
$ git blame <file>
$ git blame readme
^4934416 (dxj 2020-09-25 14:12:31 +0800 1) Hi,we are learning Git together.
^4934416 (dxj 2020-09-25 14:12:31 +0800 2) Have a good time!


# 查看历史提交以及被回退的提交
$ git reflog
4934416 (HEAD -> master) HEAD@{0}: commit (initial): 新增 readme文件

通过图形界面工具查看版本历史

命令行输入 gitk 可直接打开图形界面

1
$ gitk

image-20201020155743046

版本回退 git reset

git reset 命令用于回退版本,可以指定退回某一次提交的版本。

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
git reset [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
# --mixed 为默认,可以不用带该参数,用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变。
# --soft 参数用于回退到某个版本:
# --hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交,使用时需要慎重!!!

# 让暂存区恢复成和HEAD的一样
$ git reset HEAD
Unstaged changes after reset:
M index.html
M readme.md
M styles/style.css

HEAD 说明:
HEAD 表示当前版本
HEAD^ 上一个版本
HEAD^^ 上上一个版本
HEAD^^^ 上上上一个版本
以此类推...

可以使用 ~数字表示
HEAD~0 表示当前版本
HEAD~1 上一个版本
HEAD^2 上上一个版本
HEAD^3 上上上一个版本
以此类推...

# 回退到某次提交
$ git reset --hard commitid

$ git reset --hard HEAD

分支和多人协作

分支操作

创建分支

1
$ git branch (branchname)

切换分支

1
$ git checkout (branchname)

创建并切换分支

1
$ git checkout -b (branchname)

注意:在本地仓库操作,创建的都是本地分支。

合并分支

1
2
3
4
5
$ git merge 合并某分支到当前分支
# 官方文档:https://git-scm.com/book/zh/v2/Git-分支-变基
# 建议不要在公共分支使用rebase
$ git rebase

备注:git rebase 和 merge 区别

git merge 合并方式:

--ff 平常什么都不加的时候,则使用默认的 –ff , 即 fast-forward 方式;

--no-ff 指的是强行关闭 fast-forward 方式。
fast-forward 方式就是当条件允许的时候,git 直接把 HEAD 指针指向合并分支的头,完成合并。属于 “快进方式”,不过这种情况如果删除分支,则会丢失分支信息。因为在这个过程中没有创建 commit。
--squash 是用来把一些不必要 commit 进行压缩,比如说,你的 feature 在开发的时候写的 commit 很乱,那么我们合并的时候不希望把这些历史 commit 带过来,于是使用–squash 进行合并,此时文件已经同合并后一样了,但不移动 HEAD,不提交。需要进行一次额外的 commit 来 “总结” 一下,然后完成最终的合并。

总结:

–no-ff:不使用 fast-forward 方式合并,保留分支的 commit 历史

–squash:使用 squash 方式合并,把多次分支 commit 历史压缩为一次

image-20201021145732506

查看分支

1
2
3
4
5
6
7
8
9
10
11
# 查看当前分支
$ git branch
* master

# 查看所有分支
$ git branch -av

* fix_readme 2c64e99 Background to green
fixix_css 2c64e99 Background to green
master 91b8f29 Move readme to readme.md
temp 81a7b4e Add test

删除分支

  • 删除本地分支
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
$ git branch -d 分支名
# -D Shortcut for --delete --force.
$ git branch -D 分支名

# 查看所有分支
$ git branch -av

* fix_readme 2c64e99 Background to green
fixix_css 2c64e99 Background to green
master 91b8f29 Move readme to readme.md
temp 81a7b4e Add test
# 删除分支 提示无法删除
$ git branch -D fix_readme
error: Cannot delete branch 'fix_readme' checked out at 'F:/demo/git/git_learning'

# 切换分支到master
$ git checkout master
Switched to branch 'master'
# 重新删除分支 可以先尝试使用-d参数,如果提示告警,在使用-D
$ git branch -d fix_readme
error: The branch 'fix_readme' is not fully merged.
If you are sure you want to delete it, run 'git branch -D fixix_css'.

$ git branch -D fix_readme
Deleted branch fix_readme (was 2c64e99).

$ git branch -av
fixix_css 2c64e99 Background to green
* master 91b8f29 Move readme to readme.md
temp 81a7b4e Add test

  • 删除远程分支
1
2
$ git push origin -d 分支名
$ git push <远程仓库名> -d 分支名

建议进行界面操作

暂存修改

暂存工作现场 git stash

恢复 git stash apply

删除 git stash drop

恢复 + 删除 git stash pop

多人协作

查看远程仓库信息

1
2
3
4
5
6
$ git remote -v
origin https://github.com/alibaba/COLA.git (fetch)
origin https://github.com/alibaba/COLA.git (push)

$ git remote
origin

更新推送远程仓库

更新远程库信息 git fetch

将远程库最新修改更新到本地 git pull

git pull 可以认为是 git fetch+git merge

将本地修改推送到远程库 git push

1
$ git push origin 分支名

本地分支与远程分支交互

使用远程分支 A 创建本地分支

1
2
# origin是远程仓库名,若名字一样origin/A可以省略
$ git checkout -b A origin/A

将本地分支与远程分支作关联

1
2
# 提示no tracking information错误
$ git branch --set-upstream A origin/A

GitHub

https://github.com/

GitLab

https://about.gitlab.com/