Git入门
Git介绍
版本控制。
- 牛X的互联网,牛X的大神们都在用
- 完整的版本控制功能,解决多人协作的问题
- 提高开发效率
- 程序员的必备技能,务必学习git,并习惯把自己的代码同步到github上
- Git不同于github,理清两者之间的联系和差异
Git安装
官网下载,傻瓜式安装即可。
Git仓库
信息配置
- git configgit config --global user.name "Bicomir"
- git config --global user.email "18120570301@163.com"
- git config --list
初始化版本库
- git init
添加文件到版本库
- git add
- git commit
查看仓库状态
- git status
Git工作流
工作区–>暂存区–>版本库
假设有这么个故事是这样展开的,有个程序员刚去上班,每天产品经理会给他布置任务,任务以bash demo命名。
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# 1.第一天产品经理bash demo任务完成
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
bash_demo.txt
nothing added to commit but untracked files present (use "git add" to track)
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git add bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git commit -m "bash 1st commit"
[master 9adb6ec] bash 1st commit
1 file changed, 1 insertion(+)
create mode 100644 bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
nothing to commit, working tree clean
# 2.事情不是那么简单,下午增加了临时任务,并要求下班前完成,
下班前把东西放到了暂存区。
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: bash_demo.txt
no changes added to commit (use "git add" and/or "git commit -a")
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git add bash_demo.txt
# 3.第二天上班,被产品经理告知昨天附加需求多余,要求改回。
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git reset HEAD bash_demo.txt
Unstaged changes after reset:
M bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: bash_demo.txt
no changes added to commit (use "git add" and/or "git commit -a")
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git checkout -- bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
nothing to commit, working tree clean
# 4.将附加任务改回后,并开始第二天的任务。
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: bash_demo.txt
no changes added to commit (use "git add" and/or "git commit -a")
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git add bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git commit -m "2nd commit"
[master 1111222] 2nd commit
1 file changed, 2 insertions(+), 1 deletion(-)
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
nothing to commit, working tree clean
# 5.被产品经理告知第二天的需求其实不需要,需要第一个版本。
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git log
commit 111122203a20e045217ff59323edcfeb8f12cc92 (HEAD -> master)
Author: Bicomir <18120570301@163.com>
Date: Mon Jul 8 20:09:53 2019 +0800
2nd commit
commit 9adb6ec069364e8e09e726aed9109b70e8ab86be
Author: Bicomir <18120570301@163.com>
Date: Mon Jul 8 19:58:42 2019 +0800
bash 1st commit
commit e8fe36664ee4f613126382440f58f5b30799c30b
Author: Bicomir <18120570301@163.com>
Date: Mon Jul 8 19:43:06 2019 +0800
repo2 1st commit
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git rests --hard
git: 'rests' is not a git command. See 'git --help'.
The most similar command is
reset
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git reset --hard 9adb6ec069364e8e09e726aed9109b70e8ab86be
HEAD is now at 9adb6ec bash 1st commit
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
nothing to commit, working tree clean
# 第三天,被产品经理告知,bash demo这个需求不需要,要求删除。
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git rm bash_demo.txt
rm 'bash_demo.txt'
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ ls -a
./ ../ .git/ test.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git commit -m "delete bash_demo"
[master 311da2e] delete bash_demo
1 file changed, 1 deletion(-)
delete mode 100644 bash_demo.txt
wjq@LAPTOP-H1TBSSQ3 MINGW64 /d/git_Demo (master)
$ git status
On branch master
nothing to commit, working tree clean
|
本地与远程仓库
远程仓库
我所熟知的远程仓库国外有GitHub、Bitbucket、GitLab,Azure Devops,国内有Coding,实际开发中国内或国外看场景都有可能用的到,不管仓库是什么,大体上差不多,在这里以熟知的GitHub为例。
git remote add -> git pull -> git push -> git clone
创建SSH Key
ssh-keygen -t rsa -C "youremail@example.com"
测试是否连通
ssh -T git@github.com
添加远程仓库
git remote add origin git@github.com:tylerdemo/demo4.git
git pull origin master --allow-unrelated-histories
git push -u origin master
克隆仓库
方便团队协作开发,就是把远端的代码复制一份到本地。
git clone git@github.com:tylerdemo/demo4.git
标签管理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# 查看所有标签
git tag
# 创建标签
git tag name
# 指定提交信息
git tag -a name -m "comment"
# 删除标签
git tag -d name
# 标签发布
git push origin name
|
git tag -> git push
,以向远端仓库推送一个标签V1.0.1为例,命令如下:
git tag
git tag v1.0.1
git tag
git push origin v1.0.1
git tag -d v1.0.1
分支管理
创建自己的分支,既安全又不影响工作。
git branch -> git cheakout -> git merge
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 第一次任务
git init
git status
echo "first branch demo" >> branch.txt
git status
git add branch.txt
git commit -m "first branch commit"
git status
# 需要创建分支
git branch feature_x # 创建分支
git branch # 查看当前分支
git cheakout feature_x # 切换分支
echo "new feature add" >> branch.txt
git add branch,txt
git commit -m "new feature add"
git status
# 新代码合并到master分支上
git cheakout master
git merge feature_x
cat branch.txt
git branch -d feature_x # 删除分支
|
建议平时为提高开发效率使用source tree,高级的一些用法用命令行。
在这里,只是记录了常见的git命令,实际上高级玩家的玩法会复杂的多,后期再逐步积累吧,