(使用的教程:极客时间《玩转git三剑客》网站地址)
一、Git基础
1、版本管理的演变
VCS 出现前:
- ⽤⽬录拷⻉区别不同版本
- 公共⽂件容易被覆盖
- 成员沟通成本很⾼
- 代码集成效率低下
集中式 VCS:
- 有集中的版本管理服务器
- 具备⽂件版本管理和分⽀管理能⼒
- 集成效率有明显地提⾼
- 客户端必须时刻和服务器相连
分布式 VCS:
- 服务端和客户端都有完整的版本库
- 脱离服务端,客户端照样可以管理版本
- 查看历史和版本⽐较等多数操作,都不需要访问服务器,⽐集中式 VCS更能提⾼版本管理效率
Git特点:
- 最优的存储能力
- 非凡的性能
- 开源
- 很容易做备份
- 支持离线操作
- 很容易定制工作流程
2、安装Git
3、使用Git前需要做的最小配置
配置user信息(修改只需重新写引号内的内容即可,命令相同)
git config --global user.name 'root'
git config --global use.email 'root@163.com'
config的三个作用域
local只对某个仓库有效,global对当前用户所有仓库有效,system对系统所有登录的用户有效,缺省等同于local
git config --local
git config --global
git config --system
显示config的配置,加–list(list后不加范围显示所有配置信息)
git config --list --local
git config --list --global
git config --list --system
在普通路径下查看local配置会报错
git config --list --local
fatal: --local can only be used inside a git repository
FAIL
清除配置(删除键,不要删除值)
git config --unset --local user.name
git config --unset --global user.name
git config --unset --system user.name
4、创建第一个仓库并配置local信息
两种场景:
1、把已有的项目代码纳入Git管理
cd 项目代码所在的文件夹
git init
2、新建的项目直接用Git管理
cd 某个文件夹
git init your_project #会在当前路径下创建和项目名称同名的文件夹
cd your_project
# 目前文件夹下添加了一个readme文件,但还未被git管理
git status #查看git状态
#输出结果
#下面这几句话表明readme文件暂未被git管理,即readme文件还在工作区,可以使用git add命令将其添加到暂存区
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
readme
nothing added to commit but untracked files present (use "git add" to track)
git add readme #将readme文件添加到git暂存区中
git status
#输出结果
#下面这几句话表明暂存区中readme文件应被提交,也可以使用git rm --cached命令使readme文件离开暂存区回到工作区
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: readme
git commit -m'Add readme' #这里是暂存区的所有文件提交到当前分支,-m表示添加提交信息
#输出结果
[main (root-commit) 582a01a] Add readme
1 file changed, 2 insertions(+)
create mode 100644 readme
git status
#输出结果
nothing to commit, working tree clean
git log #查看日志
#输出结果
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea (HEAD -> main)
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
5、认识工作区和暂存区
#目前路径下有index文件和images文件夹未被git管理
git status
#输出结果
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
images/
index.html
nothing added to commit but untracked files present (use "git add" to track)
#添加文件到暂存区,一次可以添加多个文件
git add index.html images
git status
#输出结果
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: images/git-logo.png
new file: index.html
#提交暂存区文件,提交时是提交暂存区所有文件
git commit -m'Add index + logo'
#输出结果
[main 5cbc909] Add index + logo
2 files changed, 49 insertions(+)
create mode 100644 images/git-logo.png
create mode 100644 index.html
#查看日志
git log
#输出结果
commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75 (HEAD -> main)
Author: root <root@163.com>
Date: Mon Jun 13 16:56:16 2022 +0800
Add index + logo
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
#新建styles文件夹添加style.css文件
mkdir styles
cd styles
vim style.css
cd ../
git status
#输出结果
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
styles/
nothing added to commit but untracked files present (use "git add" to track)
# 添加styles文件夹到暂存区
git add styles/
git status
#输出结果
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: styles/style.css
#提交style.css文件
git commit -m 'Add style.css'
#输出结果
[main 0010c76] Add style.css
1 file changed, 50 insertions(+)
create mode 100644 styles/style.css
#查看日志
git log
#输出结果
commit 0010c7650f925fd0d808402f5bd2d3a024a59417 (HEAD -> main)
Author: root <root@163.com>
Date: Mon Jun 13 17:06:03 2022 +0800
Add style.css
commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75
Author: root <root@163.com>
Date: Mon Jun 13 16:56:16 2022 +0800
Add index + logo
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
#目前路径下新添加了js文件
git status
#输出结果
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
js/
nothing added to commit but untracked files present (use "git add" to track)
#添加js文件至暂存区
git add js
git status
#输出结果
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: js/script.js
#提交js文件
git commit -m 'Add js'
#输出结果
[main 52d54ac] Add js
1 file changed, 15 insertions(+)
create mode 100644 js/script.js
#查看日志
git log
#输出结果
commit 52d54ac0847b566cec7a6dcc3f1afd125d3b516e (HEAD -> main)
Author: root <root@163.com>
Date: Mon Jun 13 17:11:54 2022 +0800
Add js
commit 0010c7650f925fd0d808402f5bd2d3a024a59417
Author: root <root@163.com>
Date: Mon Jun 13 17:06:03 2022 +0800
Add style.css
commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75
Author: root <root@163.com>
Date: Mon Jun 13 16:56:16 2022 +0800
Add index + logo
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
#在index.html文件尾部添加参考项目信息并修改style.css文件
index.html
<script src="js/script.js"></script>
<footer>
<p>
<a href="https://github.com/TTN-js/unforGITtable">参考项目01</a>
</p>
</footer>
style.css
footer p{
margin-bottom: 0;
}
git status
#输出结果
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
git add -u#将工作区所有文件添加到暂存区,无需写文件名
git status
#输出结果
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
modified: styles/style.css
git commit -m'Add refering projects'
#输出结果
[main 2d404c3] Add refering projects
2 files changed, 9 insertions(+)
git log
#输出结果
commit 2d404c360e1446529a48b54e73db6658de4e89da (HEAD -> main)
Author: root <root@163.com>
Date: Mon Jun 13 17:45:57 2022 +0800
Add refering projects
commit 52d54ac0847b566cec7a6dcc3f1afd125d3b516e
Author: root <root@163.com>
Date: Mon Jun 13 17:11:54 2022 +0800
Add js
commit 0010c7650f925fd0d808402f5bd2d3a024a59417
Author: root <root@163.com>
Date: Mon Jun 13 17:06:03 2022 +0800
Add style.css
commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75
Author: root <root@163.com>
Date: Mon Jun 13 16:56:16 2022 +0800
Add index + logo
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
6、文件重命名的简便方法
git mv readme readme.md
git status
#输出结果
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
renamed: readme -> readme.md
git commit -m'Move readme to readme.md'
#输出结果
[main 91000e1] Move readme to readme.md
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme => readme.md (100%)
如果不使用git mv命令,则需要先在工作区使用mv命令修改名称,然后用git add命令将重命名文件添加到暂存区,用git rm命令将重命名前的文件删除,再用git commit命令提交重命名文件,步骤较为繁琐,不建议这样给文件重命名
7、通过git log 命令查看版本演变历史
查看简化的日志信息
git log --oneline
#输出结果
91000e1 (HEAD -> main) Move readme to readme.md
2d404c3 Add refering projects
52d54ac Add js
0010c76 Add style.css
5cbc909 Add index + logo
582a01a Add readme
查看最近提交的2次日志信息
git log -n2
#输出结果
commit 91000e14f3664e5de01bda7e3a813d20bb7fa8ae (HEAD -> main)
Author: root <root@163.com>
Date: Wed Jun 15 21:07:18 2022 +0800
Move readme to readme.md
commit 2d404c360e1446529a48b54e73db6658de4e89da
Author: root <root@163.com>
Date: Mon Jun 13 17:45:57 2022 +0800
Add refering projects
查看本地有多少分支
git branch -v
#输出结果
* main 91000e1 Move readme to readme.md
基于哈希值为0010c76的提交节点新建名为temp的分支
git checkout -b temp 0010c76
#输出结果
Switched to a new branch 'temp'
修改readme文件
Hi,we are learning Git together.
Have a good time!
# 修改的地方
I am happy.
提交修改
#-am可以将添加到暂存区和提交这两个步骤合并
git commit -am'Add test'
#输出结果
[temp 0fc5911] Add test
1 file changed, 2 insertions(+)
git branch -v
#输出结果
main 91000e1 Move readme to readme.md
* temp 0fc5911 Add test
查看所有版本历史
git log --all
#输出结果
commit 0fc5911b2d3368f769919d0ed37e4618a2ed94ea (HEAD -> temp)
Author: root <root@163.com>
Date: Wed Jun 15 21:33:24 2022 +0800
Add test
commit 91000e14f3664e5de01bda7e3a813d20bb7fa8ae (main)
Author: root <root@163.com>
Date: Wed Jun 15 21:07:18 2022 +0800
Move readme to readme.md
commit 2d404c360e1446529a48b54e73db6658de4e89da
Author: root <root@163.com>
Date: Mon Jun 13 17:45:57 2022 +0800
Add refering projects
commit 52d54ac0847b566cec7a6dcc3f1afd125d3b516e
Author: root <root@163.com>
Date: Mon Jun 13 17:11:54 2022 +0800
Add js
commit 0010c7650f925fd0d808402f5bd2d3a024a59417
Author: root <root@163.com>
Date: Mon Jun 13 17:06:03 2022 +0800
Add style.css
commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75
Author: root <root@163.com>
Date: Mon Jun 13 16:56:16 2022 +0800
Add index + logo
commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
(git log 命令只能查看当前分支的版本历史)
以图形化的方法查看版本历史
git log --all --graph
#输出结果
* commit 0fc5911b2d3368f769919d0ed37e4618a2ed94ea (HEAD -> temp)
| Author: root <root@163.com>
| Date: Wed Jun 15 21:33:24 2022 +0800
| Add test
| * commit 91000e14f3664e5de01bda7e3a813d20bb7fa8ae (main)
| | Author: root <root@163.com>
| | Date: Wed Jun 15 21:07:18 2022 +0800
| | Move readme to readme.md
| * commit 2d404c360e1446529a48b54e73db6658de4e89da
| | Author: root <root@163.com>
| | Date: Mon Jun 13 17:45:57 2022 +0800
| | Add refering projects
| * commit 52d54ac0847b566cec7a6dcc3f1afd125d3b516e
|/ Author: root <root@163.com>
| Date: Mon Jun 13 17:11:54 2022 +0800
| Add js
* commit 0010c7650f925fd0d808402f5bd2d3a024a59417
| Author: root <root@163.com>
| Date: Mon Jun 13 17:06:03 2022 +0800
| Add style.css
* commit 5cbc90990cfae23f667ed0380e09f7cb7eeaff75
| Author: root <root@163.com>
| Date: Mon Jun 13 16:56:16 2022 +0800
| Add index + logo
* commit 582a01a99bea9ae6cf95a9333f11892cb85bfaea
Author: root <root@163.com>
Date: Sun Jun 12 18:07:42 2022 +0800
Add readme
综合
git log --all --graph --oneline -n4
#输出结果
* 0fc5911 (HEAD -> temp) Add test
| * 91000e1 (main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac Add js
|/
查看某一分支的版本历史
git log --oneline main
#输出结果
91000e1 (main) Move readme to readme.md
2d404c3 Add refering projects
52d54ac Add js
0010c76 Add style.css
5cbc909 Add index + logo
582a01a Add readme
8、通过图形界面工具gitk查看版本演变历史
需要下载git-gui
gitk
9、探秘.git目录
cd .git
ls -al
#输出内容
drwxr-xr-x 14 root staff 448 6 15 22:54 .
drwxr-xr-x@ 8 root staff 256 6 15 21:24 ..
-rw-r--r-- 1 root staff 9 6 15 21:33 COMMIT_EDITMSG
-rw-r--r-- 1 root staff 21 6 15 21:24 HEAD
-rw-r--r-- 1 root staff 137 6 12 18:30 config
-rw-r--r-- 1 root staff 73 6 12 17:46 description
-rw-r--r-- 1 root staff 502 6 15 22:39 gitk.cache
drwxr-xr-x 15 root staff 480 6 12 17:46 hooks
-rw-r--r-- 1 root staff 447 6 15 21:33 index
drwxr-xr-x 3 root staff 96 6 12 17:46 info
drwxr-xr-x 4 root staff 128 6 12 18:07 logs
drwxr-xr-x 28 root staff 896 6 15 22:54 objects
drwxr-xr-x 4 root staff 128 6 12 17:46 refs
cat HEAD
#输出内容
ref: refs/heads/temp
git branch -av
#输出内容
main 91000e1 Move readme to readme.md
* temp 0fc5911 Add test
cd ..
git checkout main
#输出内容
Switched to branch 'main'
git branch -av
#输出内容
* main 91000e1 Move readme to readme.md
temp 0fc5911 Add test
cd .git
cat HEAD
#输出内容
ref: refs/heads/main
cat config
#输出内容
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = root
email = root@163.com
cd refs
ls -al
#输出内容
drwxr-xr-x 4 root staff 128 6 12 17:46 .
drwxr-xr-x 14 root staff 448 6 15 23:14 ..
drwxr-xr-x 4 root staff 128 6 15 21:33 heads
drwxr-xr-x 3 root staff 96 6 15 22:54 tags
cd heads
ls -al
#输出内容
drwxr-xr-x 4 root staff 128 6 15 21:33 .
drwxr-xr-x 4 root staff 128 6 12 17:46 ..
-rw-r--r-- 1 root staff 41 6 15 21:07 main
-rw-r--r-- 1 root staff 41 6 15 21:33 temp
cat main
#输出内容
91000e14f3664e5de01bda7e3a813d20bb7fa8ae
#-t指查看91000e14f3664e5de01bda7e3a813d20bb7fa8ae的类型
git cat-file -t 91000e14f3664e5de01bda7e3a813d20bb7fa8ae
#输出内容
commit
git branch -av
#输出内容
* main 91000e1 Move readme to readme.md
temp 0fc5911 Add test
cd ../tags
ls -al
#输出内容
drwxr-xr-x 3 root staff 96 6 15 22:54 .
drwxr-xr-x 4 root staff 128 6 12 17:46 ..
-rw-r--r-- 1 root staff 41 6 15 22:54 js01
cat js01
#输出内容
9e4be03c543719284f7f67b4927098f68a8407e9
git cat-file -t 52d54ac0847b566cec7a6dcc3f1afd125d3b516e
#输出内容
commit
#-p指查看52d54ac0847b566cec7a6dcc3f1afd125d3b516e的内容
git cat-file -p 52d54ac0847b566cec7a6dcc3f1afd125d3b516e #输出内容
tree b7f16a0faff7eec017ceae4fbd036854601a3a69
parent 0010c7650f925fd0d808402f5bd2d3a024a59417
author root <root@163.com> 1655111514 +0800
committer root <root@163.com> 1655111514 +0800
Add js
cd ../../objects
ls -al
#输出内容
drwxr-xr-x 28 root staff 896 6 15 22:54 .
drwxr-xr-x 14 root staff 448 6 15 23:20 ..
drwxr-xr-x 3 root staff 96 6 13 17:06 00
drwxr-xr-x 3 root staff 96 6 13 16:56 01
drwxr-xr-x 3 root staff 96 6 15 21:07 0d
drwxr-xr-x 3 root staff 96 6 15 21:33 0f
drwxr-xr-x 3 root staff 96 6 15 21:33 24
drwxr-xr-x 3 root staff 96 6 13 17:45 2d
drwxr-xr-x 3 root staff 96 6 12 18:07 31
drwxr-xr-x 3 root staff 96 6 13 17:11 52
drwxr-xr-x 3 root staff 96 6 13 17:45 53
drwxr-xr-x 3 root staff 96 6 12 18:07 58
drwxr-xr-x 3 root staff 96 6 13 16:56 5c
drwxr-xr-x 3 root staff 96 6 13 16:55 6a
drwxr-xr-x 3 root staff 96 6 12 18:07 7c
drwxr-xr-x 3 root staff 96 6 13 17:11 87
drwxr-xr-x 4 root staff 128 6 15 21:07 91
drwxr-xr-x 3 root staff 96 6 13 16:56 96
drwxr-xr-x 3 root staff 96 6 15 22:54 9e
drwxr-xr-x 4 root staff 128 6 13 17:45 ab
drwxr-xr-x 3 root staff 96 6 13 17:06 ae
drwxr-xr-x 3 root staff 96 6 13 17:11 b7
drwxr-xr-x 3 root staff 96 6 13 17:45 c7
drwxr-xr-x 4 root staff 128 6 13 17:45 da
drwxr-xr-x 3 root staff 96 6 13 17:05 ef
drwxr-xr-x 3 root staff 96 6 15 21:33 fa
drwxr-xr-x 2 root staff 64 6 12 17:46 info
drwxr-xr-x 2 root staff 64 6 12 17:46 pack
cd c7
ls -al
#输出内容
drwxr-xr-x 3 cbx staff 96 6 13 17:45 .
drwxr-xr-x 28 cbx staff 896 6 15 22:54 ..
-r--r--r-- 1 cbx staff 335 6 13 17:45 4ab3ff599e531ee0ad1e2b030cfc8f915fbc73
git cat-file -t c74ab3ff599e531ee0ad1e2b030cfc8f915fbc73
#输出内容
blob
cd ../da
ls -al
#输出内容
drwxr-xr-x 4 root staff 128 6 13 17:45 .
drwxr-xr-x 28 root staff 896 6 15 22:54 ..
-r--r--r-- 1 root staff 174 6 13 17:45 eb110f7e89c426dc148bf851608d8fbf140977
-r--r--r-- 1 cbx staff 19664 6 13 16:55 f480669aa9256fa18b5c28e467af816f16482d
git cat-file -t daeb110f7e89c426dc148bf851608d8fbf140977
#输出内容
tree
10、commit、tree和blob三个对象之间的关系
一个commit对应一棵树tree,这个树里面包含了到目前commit包含的所有文件
blob对应的是每个文件,只要文件内容相同,不管有多少个,都只对应一个blob
11、小练习:数一数tree的个数
新建的git仓库,有且仅有一个commit,仅仅包含doc/readme,请问包含多少个tree,多少个blob?
git init watch_git_objects
cd watch_git_objects
mkdir doc
cd doc
echo "Hello world" > readme
cd ..
git status
#输出内容
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
find .git/objects -type f
git add doc
find .git/objects -type f
#输出内容
.git/objects/80/2992c4220de19a90767f3000a79a31b98d0df7
git cat-file -t 802992c4220de19a90767f3000a79a31b98d0df7
#输出内容
blob
git cat-file -p 802992c4220de19a90767f3000a79a31b98d0df7
#输出内容
Hello world
git status
#输出内容
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: doc/readme
git commit -am'Add readme'
#输出内容
[main (root-commit) a2c89ae] Add readme
1 file changed, 1 insertion(+)
create mode 100644 doc/readme
find .git/objects -type f
#输出内容
.git/objects/a2/c89ae8de514bd5148911cc3180f2751c9729b0
.git/objects/80/2992c4220de19a90767f3000a79a31b98d0df7
.git/objects/5d/fe0df121a9a20c25b76c773c0a78db41b69b8d
.git/objects/64/b18817c391260e6bbc570015b33c0c22514ef8
git cat-file -t a2c89ae8de514bd5148911cc3180f2751c9729b0
#输出内容
commit
git cat-file -p a2c89ae8de514bd5148911cc3180f2751c9729b0
#输出内容
tree 64b18817c391260e6bbc570015b33c0c22514ef8
author root <root@163.com> 1656330567 +0800
committer root <root@163.com> 1656330567 +0800
Add readme
git cat-file -t 802992c4220de19a90767f3000a79a31b98d0df7
#输出内容
blob
git cat-file -p 802992c4220de19a90767f3000a79a31b98d0df7
#输出内容
Hello world
git cat-file -t 5dfe0df121a9a20c25b76c773c0a78db41b69b8d
#输出内容
tree
git cat-file -p 5dfe0df121a9a20c25b76c773c0a78db41b69b8d
#输出内容
100644 blob 802992c4220de19a90767f3000a79a31b98d0df7 readme
git cat-file -t 64b18817c391260e6bbc570015b33c0c22514ef8
#输出内容
tree
git cat-file -p 64b18817c391260e6bbc570015b33c0c22514ef8
#输出内容
040000 tree 5dfe0df121a9a20c25b76c773c0a78db41b69b8d doc
一共1个commit,2个tree,1个blob
12、分离头指针情况下的注意事项
分离头指针状态就是HEAD指针不再指向分支,而是直接指向某个commit。通常,我们工作在某一个分支上,比如 master 分支。这个时候 master 指针和 HEAD 指针是一起前进的,每做一次提交,这两个指针就会一起向前挪一步。但是在某种情况下(例如 checkout 了某个具体的 commit),master 指针 和 HEAD 指针这种「绑定」的状态就被打破了,变成了分离头指针(detacged HEAD)状态。
在处于分离头指针状态下,需要注意如果当我们进行分支切换时,很可能出现变更的丢失,这时我们需要创建一个新的分支,将变更绑定到分支上。
git log --oneline --all --graph
#输出内容
* 0fc5911 (temp) Add test
| * 91000e1 (main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 (HEAD) Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
git checkout 0010c76
#输出内容
Note: switching to '0010c76'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 0010c76 Add style.css
Git提示你现在处于“分离头指针状态”,你可以查看、并且做些代码调试,还可以提交他们,在这种状态下,如果checkout到其他分支,完全可以丢弃在此基础上做的修改,而不会影响到其他分支。 如果你想保留本次的修改,你可以使用”git checkout -b 新的分支名”来保留它(现在或者以后都可以)。
有时候我们不想为某次的修改单独创建一个分支,也没有想要提交到版本库的意思,只是做下调试,那么我们就可以使用git提供的分离头指针方法。如果发现真的有必要提交到版本库,还可以使用git checkout -b命令来为这次的提交新建一个分支,再把分支合并上去。
#将网页背景改成绿色
vi styles/style.css
git status
#输出内容
HEAD detached at 0010c76
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
git add styles/style.css
git commit -am'Background to green'
#输出内容
[detached HEAD 430c0d6] Background to green
1 file changed, 1 insertion(+), 1 deletion(-)
git log --oneline --all --graph
#输出内容
* 430c0d6 (HEAD) Background to green
| * 0fc5911 (temp) Add test
|/
| * 91000e1 (main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
git branch -av
#输出内容
* (HEAD detached from 0010c76) 430c0d6 Background to green
main 91000e1 Move readme to readme.md
temp 0fc5911 Add test
git checkout main #输出内容
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
430c0d6 Background to green
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 430c0d6
Switched to branch 'main'
git log --oneline --all --graph
#输出内容
* 0fc5911 (temp) Add test
| * 91000e1 (HEAD -> main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
git branch fix_css 430c0d6
#输出内容
* 430c0d6 (fix_css) Background to green
| * 0fc5911 (temp) Add test
|/
| * 91000e1 (HEAD -> main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
13、进一步理解HEAD和branch
git checkout -b fix_readme fix_css #输出内容
Switched to a new branch 'fix_readme'
git log --oneline --all --graph
#输出内容
* 430c0d6 (HEAD -> fix_readme, fix_css) Background to green
| * 0fc5911 (temp) Add test
|/
| * 91000e1 (main) Move readme to readme.md
| * 2d404c3 Add refering projects
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
cat .git/HEAD #输出内容
ref: refs/heads/fix_readme
cat .git/refs/heads/fix_readme #输出内容
430c0d63c28c2309246d9a7fea78808e5a4f2196
git cat-file -t 430c0d63c28c2309246d9a7fea78808e5a4f2196 #输出内容
commit
HEAD会最终落脚于一个commit
#比较HEAD所指commit与HEADHEAD所指commit的父亲
git diff HEAD HEAD^1
diff --git a/styles/style.css b/styles/style.css
index 4c6bc45..ef3f137 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,5 +1,5 @@
body{
- background-color: green;
+ background-color: orange;
font-family: 'Monaco', sans-serif;
color: white;
}
#比较HEAD所指commit与HEADHEAD所指commit的父亲
git diff HEAD HEAD^1^1
#等同于
git diff HEAD HEAD~2
二、独自使用Git时的常见场景
14、如何删除不需要的分支
git branch -av
#输出内容
fix_css 430c0d6 Background to green
* fix_readme 430c0d6 Background to green
main 91000e1 Move readme to readme.md
temp 0fc5911 Add test
#删除分支
git branch -d fix_css
#输出内容
Deleted branch fix_css (was 430c0d6).
git branch -d fix_readme
#输出内容
error: Cannot delete branch 'fix_readme' checked out at '/Users/xxx/Documents/Git/repository/git_learning'
FAIL
#当HEAD指向当前分支时无法删除该分支
git checkout main
#输出内容
Switched to branch 'main'
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 fix_readme'.
FAIL
git branch -D fix_readme #输出内容
Deleted branch fix_readme (was 430c0d6).
git branch -av
#输出内容
* main 91000e1 Move readme to readme.md
temp 0fc5911 Add test
15、如何修改最新commit的message
git log -n1
#输出内容
commit 91000e14f3664e5de01bda7e3a813d20bb7fa8ae (HEAD -> main)
Author: root <root@163.com>
Date: Wed Jun 15 21:07:18 2022 +0800
Move readme to readme.md
#假设该commit的message有错误要修改
git commit --amend
#输出内容
Move readme to readme.md
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jun 15 21:07:18 2022 +0800
#
# On branch main
# Changes to be committed:
# renamed: readme -> readme.md
#
#将message修改为Move filename readme to readme.md后保存退出
#输出内容
[main 8517e7e] Move filename readme to readme.md
Date: Wed Jun 15 21:07:18 2022 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
rename readme => readme.md (100%)
16、如何修改老旧commit的message
git log --oneline -n3
#输出内容
8517e7e (HEAD -> main) Move filename readme to readme.md
2d404c3 Add refering projects
52d54ac (tag: js01) Add js
#对2d404c3 Add refering projects进行变更,修改为Add a refering projects
#-i表示交互式
git rebase -i 52d54ac
#输出内容
pick 2d404c3 Add refering projects
pick 8517e7e Move filename readme to readme.md
# Rebase 52d54ac..8517e7e onto 52d54ac (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 [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# 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.
#
# However, if you remove everything, the rebase will be aborted.
#修改pick 2d404c3 Add refering projects这行
r 2d404c3 Add refering projects
#保存退出
#输出内容
Add refering projects
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Jun 13 17:45:57 2022 +0800
#
# interactive rebase in progress; onto 52d54ac
# Last command done (1 command done):
# reword 2d404c3 Add refering projects
# Next command to do (1 remaining command):
# pick 8517e7e Move filename readme to readme.md
# You are currently editing a commit while rebasing branch 'main' on '52d54ac'.
#
# Changes to be committed:
# modified: index.html
# modified: styles/style.css
#修改message后保存退出
#输出内容
[detached HEAD 4618359] Add a refering projects
Date: Mon Jun 13 17:45:57 2022 +0800
2 files changed, 9 insertions(+)
Successfully rebased and updated refs/heads/main.
git log --oneline -n3
#输出内容
c3ba408 (HEAD -> main) Move filename readme to readme.md
4618359 Add a refering projects
52d54ac (tag: js01) Add js
17、怎样把连续的多个commit整合成一个
git log --oneline
#输出内容
c3ba408 (HEAD -> main) Move filename readme to readme.md
4618359 Add a refering projects
52d54ac (tag: js01) Add js
0010c76 Add style.css
5cbc909 Add index + logo
582a01a Add readme
#将5cbc909、0010c76、52d54ac、4618359这几个commit合并
git rebase -i 582a01a
#输出内容
pick 5cbc909 Add index + logo
pick 0010c76 Add style.css
pick 52d54ac Add js
pick 4618359 Add a refering projects
pick c3ba408 Move filename readme to readme.md
# Rebase 582a01a..c3ba408 onto 582a01a (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 [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# 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.
#
# However, if you remove everything, the rebase will be aborted.
#做以下修改,然后保存退出
pick 5cbc909 Add index + logo
s 0010c76 Add style.css
s 52d54ac Add js
s 4618359 Add a refering projects
pick c3ba408 Move filename readme to readme.md
#输出结果
# This is a combination of 4 commits.
# This is the 1st commit message:
Add index + logo
# 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 projects
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Mon Jun 13 16:56:16 2022 +0800
#
# interactive rebase in progress; onto 582a01a
# Last commands done (4 commands done):
# squash 52d54ac Add js
# squash 4618359 Add a refering projects
# Next command to do (1 remaining command):
# pick c3ba408 Move filename readme to readme.md
# You are currently rebasing branch 'main' on '582a01a'.
#
# Changes to be committed:
# new file: images/git-logo.png
# new file: index.html
# new file: js/script.js
# new file: styles/style.css
#做如下修改,保存退出
# This is a combination of 4 commits.
Create a complete web page
# This is the 1st commit message:
#输出结果
[detached HEAD c575ede] Create a complete web page
Date: Mon Jun 13 16:56:16 2022 +0800
4 files changed, 123 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/main.
git log --oneline
#输出结果
62d7252 (HEAD -> main) Move filename readme to readme.md
c575ede Create a complete web page
582a01a Add readme
18、怎样把间隔的多个commit整合成一个
git log --oneline
#输出结果
62d7252 (HEAD -> main) Move filename readme to readme.md
c575ede Create a complete web page
582a01a Add readme
#将582a01a、62d7252这两个commit合并
git rebase -i 582a01a
#输出结果
pick c575ede Create a complete web page
pick 62d7252 Move filename readme to readme.md
# Rebase 582a01a..62d7252 onto 582a01a (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 [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# 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.
#
# However, if you remove everything, the rebase will be aborted.
#将前面两行修改为
pick 582a01a #小技巧,当为第一个commit变基时,可以在变基内容中的第一行添加pick+commit哈希值
s 62d7252 Move filename readme to readme.md
pick c575ede Create a complete web page
git rebase --continue
# This is a combination of 2 commits.
Add readme.md
# This is the 1st commit message:
Add readme
# This is the commit message #2:
Move filename readme to readme.md
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Jun 15 21:07:18 2022 +0800
#
# On branch main
# Last commands done (2 commands done):
# pick 582a01a
# squash 62d7252 Move filename readme to readme.md
# Next command to do (1 remaining command):
# pick c575ede Create a complete web page
# You are currently rebasing branch 'main' on '582a01a'.
#
# Changes to be committed:
# renamed: readme -> readme.md
git log --oneline --graph --all
* c3a87cf (HEAD -> main) Create a complete web page
* b278e72 Add readme.md
* 0fc5911 (temp) Add test
| * 52d54ac (tag: js01) Add js
|/
* 0010c76 Add style.css
* 5cbc909 Add index + logo
* 582a01a Add readme
19、如何比较暂存区和HEAD所含文件的差异
#修改index.html文件
git add index.html
git status #输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
#比较暂存区和HEAD所含文件的差异(--cached表示暂存区)
git diff --cached
#输出内容
diff --git a/index.html b/index.html
index ab8f905..b9f4261 100644
--- a/index.html
+++ b/index.html
@@ -29,7 +29,7 @@
<div class="accordion"><h1>Basic Commands</h1></div>
<div class="panel">
<ol>
- <li></li>
+ <li>add</li>
<li></li>
<li></li>
<li></li>
#将add修改为config后
git add index.html
git diff --cached
diff --git a/index.html b/index.html
#输出内容
index ab8f905..b29bc0e 100644
--- a/index.html
+++ b/index.html
@@ -29,7 +29,7 @@
<div class="accordion"><h1>Basic Commands</h1></div>
<div class="panel">
<ol>
- <li></li>
+ <li>config</li>
<li></li>
<li></li>
<li></li>
git commit -m'Add the first git command with config'
#输出内容
[main fc97092] Add the first git command with config
1 file changed, 1 insertion(+), 1 deletion(-)
20、如何比较工作区和暂存区所含文件的差异
#修改index.html文件后添加到暂存区
git add index.html
#修改style.css文件后不提交到暂存区,此时比较工作区和暂存区区别
git diff
#输出内容
diff --git a/styles/style.css b/styles/style.css
index c74ab3f..e1fd1fd 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
body{
background-color: orange;
font-family: 'Monaco', sans-serif;
- color: white;
+ color: black;
}
body a{
#修改readme.md文件,比较工作区和暂存区
git diff
#输出内容
diff --git a/readme.md b/readme.md
index 7c2bad2..d9ccd75 100644
--- a/readme.md
+++ b/readme.md
@@ -1,2 +1,3 @@
+# say hello
Hi,we are learning Git together.
Have a good time!
diff --git a/styles/style.css b/styles/style.css
index c74ab3f..e1fd1fd 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -1,7 +1,7 @@
body{
background-color: orange;
font-family: 'Monaco', sans-serif;
- color: white;
+ color: black;
}
body a{
# 只比较工作区中的readme.md文件(如果比较多个文件,只要在后面添加文件名即可)
git diff readme.md
#输出内容
diff --git a/readme.md b/readme.md
index 7c2bad2..d9ccd75 100644
--- a/readme.md
+++ b/readme.md
@@ -1,2 +1,3 @@
+# say hello
Hi,we are learning Git together.
Have a good time!
21、如何让暂存区恢复成和HEAD的一样
git status
#输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
modified: readme.md
modified: styles/style.css
# 暂存区恢复成和HEAD一样
git reset HEAD
#输出内容
Unstaged changes after reset:
M index.html
M readme.md
M styles/style.css
git status
#输出内容
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
modified: readme.md
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
22、如何让工作区的文件恢复为暂存区一样
git add index.html
git status
#输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.md
modified: styles/style.css
#再次修改index.html文件
git diff index.html
#输出内容
diff --git a/index.html b/index.html
index c9b6ad4..c0df56c 100644
--- a/index.html
+++ b/index.html
@@ -14,7 +14,7 @@
<div class="accordion"><h1>Terminologys</h1></div>
<div class="panel">
<ol>
- <li>bare repository</li>
+ <li>裸仓库</li>
<li></li>
<li></li>
<li></li>
git status
#输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
modified: readme.md
modified: styles/style.css
#工作区的文件恢复为暂存区一样
git restore index.html
git status
#输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.md
modified: styles/style.css
23、如何取消暂存区部分文件的更改
git status
#输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
modified: readme.md
modified: styles/style.css
#撤销style.css和readme.md两个文件的修改
git restore --staged readme.md styles/style.css
git status #输出内容
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: index.html
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: readme.md
modified: styles/style.css
24、消除最近的几次提交
git log --oneline
#输出内容
55f74b8 (HEAD -> temp) Remove all
d8642f6 Rename git-logo to git-logo2
0fc5911 Add test
0010c76 Add style.css
5cbc909 Add index + logo
582a01a Add readme
#消除最近两次提交
git reset --hard 0fc5911 #输出内容
HEAD is now at 0fc5911 Add test
git log --oneline
#输出内容
0fc5911 (HEAD -> temp) Add test
0010c76 Add style.css
5cbc909 Add index + logo
582a01a Add readme
25、看看不同提交的指定文件的差异
#比较不同分支的差异
git diff temp main
#另一种写法
git diff 0fc5911 c651da7
#比较不同分支的差异指定文件的差异
git diff temp main index.html
26、正确删除文件的方法
git rm readme #输出内容
rm 'readme'
ls #输出内容
images index.html styles
27、开发中临时加塞了紧急任务怎么处理
git status #输出内容
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
# 将工作区中不紧急的文件放到stash栈中
git stash
#输出内容
Saved working directory and index state WIP on temp: 2f50d69 Delete readme
#查看stash栈中有什么
git stash list
#输出内容
stash@{0}: WIP on temp: 2f50d69 Delete readme
git status
#输出内容
On branch temp
nothing to commit, working tree clean
# 还原stash中文件,但stash中并不弹出
git stash apply
#输出内容
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
git stash list
stash@{0}: WIP on temp: 2f50d69 Delete readme
# 还原stash中文件,并从stash中弹出
git stash pop
git stash pop #输出内容
On branch temp
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (579276042c648d20235b5e1c5327cb5f70b91a28)
git stash list
28、如何制定不需要Git管理的文件
添加.gitignore文件
注意:如abc/(加斜杠后表示将abc为名字的文件夹中的文件不纳入git管理,但.abc文件依旧会被管理)
例如下面
######################################################################
# Build Tools
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
target/
!.mvn/wrapper/maven-wrapper.jar
######################################################################
# IDE
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### JRebel ###
rebel.xml
### NetBeans ###
nbproject/private/
build/*
nbbuild/
dist/
nbdist/
.nb-gradle/
######################################################################
# Others
*.log
*.xml.versionsBackup
*.swp
!*/build/*.java
!*/build/*.html
!*/build/*.xml
29、如何将Git仓库备份到本地
常用传输协议
哑协议与智能协议区别
1.哑协议传输进度不可⻅;智能协议传输可⻅。
2.智能协议⽐哑协议传输速度快。
备份特点
多点备份
pwd
#输出内容
/Users/root/Documents/Git/repository
mkdir 666-backup
cd 666-backup
# 本地哑协议传输,--bare表示不带工作区
git clone --bare /Users/root/Documents/Git/repository/git_learning ya.git
#输出内容
Cloning into bare repository 'ya.git'...
done.
# 本地智能协议传输
git clone --bare file:///Users/root/Documents/Git/repository/git_learning/.git zhineng.git
#输出内容
Cloning into bare repository 'zhineng.git'...
remote: Enumerating objects: 32, done.
remote: Counting objects: 100% (32/32), done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 32 (delta 11), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (32/32), 22.50 KiB | 22.50 MiB/s, done.
Resolving deltas: 100% (11/11), done.
在工作时备份文件到其他地方
pwd
#输出内容
/Users/root/Documents/Git/repository/git_learning
# 与远端建立连接
git remote add zhineng file:///Users/cbx/Documents/Git/repository/666-backup/zhineng.git
# 向远端备份
git push --set-upstream zhineng user
#输出内容
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To file:///Users/cbx/Documents/Git/repository/666-backup/zhineng.git
* [new branch] user -> user
Branch 'user' set up to track remote branch 'user' from 'zhineng'.
三、Git与GitHub的简单同步
30、注册一个GitHub账号
31、配置公私钥
32、在GitHub上创建个人仓库
33、把本地仓库同步到GitHub
# 连接到github
git remote add github git@github.com:traveller-cbx/git_learning.git
git remote -v
#输出内容
github git@github.com:traveller-cbx/git_learning.git (fetch)
github git@github.com:traveller-cbx/git_learning.git (push)
zhineng file:///Users/cbx/Documents/Git/repository/666-backup/zhineng.git (fetch)
zhineng file:///Users/cbx/Documents/Git/repository/666-backup/zhineng.git (push)
git fetch github
git merge --allow-unrelated-histories github/main
git push github --all
四、Git多人单分支集成协作时的常见场景
在本地模拟两人
pwd
Users/root/Documents/Git/repository
git clone git@github.com:traveller-cbx/git_learning.git git_learning_02
34、不同人修改了不同文件如何处理
首先在github上添加feature/add_git_commands分支
git clone git@github.com:traveller-cbx/git_learning.git git_learning_02
#输出内容
Cloning into 'git_learning_02'...
remote: Enumerating objects: 34, done.
remote: Counting objects: 100% (34/34), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 34 (delta 9), reused 30 (delta 8), pack-reused 0
Receiving objects: 100% (34/34), 23.80 KiB | 316.00 KiB/s, done.
Resolving deltas: 100% (9/9), done.
cd git_learning_02
git config --local user.name 'user001'
git config --local user.email 'user001@163.com'
git checkout -b feature/add_git_commands origin/feature/add_git_commands
#输出内容
Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'origin'.
Switched to a new branch 'feature/add_git_commands'
#修改readme文件
vim readme
git add -u
git commit -m'Add git commands description in readme.md' #输出内容
[feature/add_git_commands 4e6d9d7] Add git commands description in readme.md
1 file changed, 2 insertions(+)
git push
#输出内容
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 348 bytes | 348.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:traveller-cbx/git_learning.git
6b45532..4e6d9d7 feature/add_git_commands -> feature/add_git_commands
git_learning
git fetch github
#输出内容
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), 328 bytes | 328.00 KiB/s, done.
From github.com:traveller-cbx/git_learning
* [new branch] feature/add_git_commands -> github/feature/add_git_commands
git checkout -b feature/add_git_commands github/feature/add_git_commands
#输出内容
Branch 'feature/add_git_commands' set up to track remote branch 'feature/add_git_commands' from 'github'.
Switched to a new branch 'feature/add_git_commands'
# 修改index.html文件
vim index.html
git add -u
git commit -m'Add git add command in index.html'
#输出内容
[feature/add_git_commands e46277e] Add git add command in index.html
1 file changed, 1 insertion(+), 1 deletion(-)
git fetch github
git merge github/feature/add_git_commands
#输出内容
Updating 6b45532..536ba83
Fast-forward
readme.md | 2 ++
1 file changed, 2 insertions(+)
Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
35、不同人修改了同文件的不同区域如何处理
git_learning_02
git checkout feature/add_git_commands
git pull
vim index.html
git commit -am'Add non fast-forward'
git_learning
git checkout feature/add_git_commands
vim index.html
git commit -am'Add commit command'
git push github
git_learning_02
git fetch
git merge origin/feature/add_git_commands
git push
36、不同人修改了同文件的同一区域如何处理
git_learning
vim index.html
git commit -am'Add mv and rm commands'
git_learning_02
vim index.html
git commit -am'Add stash and log commands'
git push
git_learning
vim index.html
git push github
vim index.html
git commit -am'Resolved conflict by hand with 4 commands'
git push github
37、同时变更了文件名和文件内容如何处理
git_learning
git mv index.html index.htm
git commit -am'Mv index.html to index.htm'
git_learning_02
vim index.html
git commit -am'Improve index head'
git_learning
git push github
git_learning_02
git pull
# 此时本地文件名和文件内容都变化了
git push
38、把同一文件改成了不同的文件名如何处理
git_learning_02
git mv index.htm index1.htm
git commit -am'Mv index.htm to index1.htm'
git_learning
git mv index.htm index2.htm
git commit -am'Mv index.htm to index2.htm'
git push
git_learning_02
git pull
# 输出内容
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
Unpacking objects: 100% (2/2), 210 bytes | 105.00 KiB/s, done.
From github.com:traveller-cbx/git_learning
41ae33c..a3a6a1c feature/add_git_commands -> origin/feature/add_git_commands
hint: Pulling without specifying how to reconcile divergent branches is
hint: discouraged. You can squelch this message by running one of the following
hint: commands sometime before your next pull:
hint:
hint: git config pull.rebase false # merge (the default strategy)
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
CONFLICT (rename/rename): Rename "index.htm"->"index1.htm" in branch "HEAD" rename "index.htm"->"index2.htm" in "a3a6a1cbb6aa625c85d5f362effcc5958553665e"
Automatic merge failed; fix conflicts and then commit the result.
git status
#输出内容
On branch feature/add_git_commands
Your branch and 'origin/feature/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add/rm <file>..." as appropriate to mark resolution)
both deleted: index.htm
added by us: index1.htm
added by them: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
git rm index.htm index2.htm
git add index.htm
git status
# 输出内容
On branch feature/add_git_commands
Your branch and 'origin/feature/add_git_commands' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
git commit -m'Decide to mv index.htm to index1.htm'
五、Git集成使用禁忌
39、禁止向集成分支执行push -f操作
40、禁止向集成分支执行变更历史的操作
六、初识GitHub
41、GitHub为什么会火
git诞生前
git诞生后
github诞生后
github十年
github成功因素
42、GitHub都有哪些功能
43、如何快速淘到感兴趣的开源项目
44、如何在GitHub上搭建个人博客
在github上搜索jekyll-now
进入仓库后按照readme文件进行快速开始
45、开源项目怎么保证代码质量
每个pull request会被check
46、为何需要组织类型的仓库
七、使用GitHub进行团队协作
47、创建团队的项目
48、如何选择适合自己团队的工作流
需要考虑的因素
主干开发
Git Flow
Github Flow
GitLab Flow(带生产分支)
GitLab Flow(带环境分支)
Gitlab Flow(带发布分支)