Git原理(03)
18. 本地分支和远程分支
新建文件夹,将远程仓库克隆到本地

1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone |
克隆的仓库只有一个master分支,且是默认分支
克隆完远程仓库后,本地仓库也会新建出一个master分支 ,这个分支就是远程仓库的默认分支。
①查看本地分支git branch
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone |
②查看远程分支git branch -r
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
③查看refs
1 | xiong@SURFACE-PRO6 C:\Files\git-clone\2021-git-demo |
④refs里只有远程的HEAD,没有远程的分支,远程的分支被放在了packed-refs里了,查看该文件内容
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
文件存储了远程的master分支所指向的commit
⑤查看提交历史
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
模拟远程仓库有人进行提交
①仓库的初始情况
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
远程分支与本地分支指向的commit相同
②在远程仓库修改file1.txt

查看远程的origin
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
使用git fetch与远程进行同步
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
再次查看远程origin:仍未发生改变
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
查看refs:出现了master
1 | xiong@SURFACE-PRO6 C:\Files\git-clone\2021-git-demo |
查看master:指向的是最新的一次提交
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
出现的原因,packed-refs不是实时的压缩,是在某个时间点进行的,所以不同步。
在本地查看提交历史
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
origin/master, origin/HEAD与现在的指向不一样了,因为远程仓库进行了一次提交,现在它超前一次本地提交
一些命令
git branch -a:列出本地和远程所有的分支
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
git remote -v:fetch与push的远程仓库信息
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
git remote show origin:给出远程仓库的的信息
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
修改远程仓库的分支
git remote show origin:显示远程仓库信息
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
dev new (next fetch will store in remotes/origin):检测到远程仓库有新分支产生,使用fetch来存储信息到remotes/origin(如果不进行同步,本地存储的关于远程仓库的信息就仍然是旧的)
1 |
|
再次使用git remote show origin:就会显示dev tracked
删除远程分支
git fetch对于远程分支被删除的情况是不做任何反应的,它只会将远程仓库新增的信息抓取到本地
使用git remote show origin查看远程仓库信息
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
refs/remotes/origin/dev stale (use 'git remote prune' to remove):检测到dev是过期的,让用户使用git remote prune去移除这个垃圾对象,实际上也可以通过git fetch --prune进行移除。
使用git fetch --prune时,它会先将本地有而远程没有的进行修剪,然后再进行抓取
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
19. git fetch & git pull
1.初始状态

2.远程仓库更新一次提交,本地仓库使用fetch抓取更新

3.执行git merge origin/master,合并分支

4.远程仓库有一次提交,本地仓库在未fetch的情况下更新一次提交,同时进行git fetch 抓取远程仓库

5.执行git merge origin/master

6. 执行git push origin master

git pull可以一次性代替git fetch和git merge origin/master将本地仓库推送到远程仓库
20. FETCH_HEAD与git pull
如果是在某个分支上去执行了git fetch,则排在FETCH_HEAD第一行的就是当前分支所对应的远程分支所对应的最新一次commit,只要切换分支并运行了git fetch那么FETCH_HEAD里的内容顺序就会改变
验证
在远程仓库创建新的分支dev
并在新分支上做一次commit

git remote show origin查看远程仓库信息1
2
3
4
5
6
7
8
9
10
11
12
13xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git remote show origin
* remote origin
Fetch URL: https://github.com/xiongzhuozhuo/2021-git-demo.git
Push URL: https://github.com/xiongzhuozhuo/2021-git-demo.git
HEAD branch: master
Remote branches:
dev new (next fetch will store in remotes/origin)
master tracked
Local branch configured for 'git pull':
master merges with remote master
Local ref configured for 'git push':
master pushes to master (local out of date)git fetch拉取远程仓库更新1
2
3
4
5
6
7
8
9xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git fetch
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 660 bytes | 22.00 KiB/s, done.
From https://github.com/xiongzhuozhuo/2021-git-demo
* [new branch] dev -> origin/dev查看objects文件内容
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
78xiong@SURFACE-PRO6 C:\Files\git-clone\2021-git-demo
tree .git /f
卷 Local Disk 的文件夹 PATH 列表
卷序列号为 C0000100 F296:2801
C:\FILES\GIT-CLONE\2021-GIT-DEMO\.GIT
│ config
│ description
│ FETCH_HEAD #新增
│ HEAD
│ index
│ packed-refs
│
├─hooks
│ applypatch-msg.sample
│ commit-msg.sample
│ fsmonitor-watchman.sample
│ post-update.sample
│ pre-applypatch.sample
│ pre-commit.sample
│ pre-merge-commit.sample
│ pre-push.sample
│ pre-rebase.sample
│ pre-receive.sample
│ prepare-commit-msg.sample
│ push-to-checkout.sample
│ update.sample
│
├─info
│ exclude
│
├─logs
│ │ HEAD
│ │
│ └─refs
│ ├─heads
│ │ master
│ │
│ └─remotes
│ └─origin
│ dev #新增
│ HEAD
│ master
│
├─objects
│ ├─1a
│ │ 4a4bc16d7019273dfacb0060e1cb654e8b4e11
│ │
│ ├─27
│ │ 8dcf612bf8e033a15c9b429adbb32b0a9210b6
│ │
│ ├─7c
│ │ 8ac2f8d82a1eb5f6aaece6629ff11015f91eb4
│ │
│ ├─b8
│ │ db963d8a1508baf093baf5e7f9424d3e384717
│ │
│ ├─ba
│ │ b3f8573c3317c30242494a0f48d1c99d97eabe
│ │
│ ├─da
│ │ 1f73460673c0e876e2995d4a44889d75372bef
│ │
│ ├─info
│ └─pack
│ pack-531f25c34d918bbf4e84a1004317c263c51a18aa.idx
│ pack-531f25c34d918bbf4e84a1004317c263c51a18aa.pack
│
└─refs
├─heads
│ master
│
├─remotes
│ └─origin
│ dev #新增
│ HEAD
│ master
│
└─tags虽然抓取了远程新增的dev分支,但是本地的heads里没有dev分支信息
查看新增的FETCH_HEAD文件内容
1
2
3
4xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
cat .git/FETCH_HEAD
278dcf612bf8e033a15c9b429adbb32b0a9210b6 branch 'master' of https://github.com/xiongzhuozhuo/2021-git-demo
da1f73460673c0e876e2995d4a44889d75372bef not-for-merge branch 'dev' of https://github.com/xiongzhuozhuo/2021-git-demo使用
git checkout dev,本地会新建一个dev分支,并与远程的dev分支进行关联1
2
3
4
5
6
7
8
9
10
11
12
13xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git branch -vv
* master 70795aa [origin/master: behind 1] Update README.md
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git checkout dev
Switched to a new branch 'dev'
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev)
git branch -vv
* dev da1f734 [origin/dev] Create file3.txt
master 70795aa [origin/master: behind 1] Update README.md查看FETCH_HEAD内容:没有变化
1
2
3
4xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev)
cat .git/FETCH_HEAD
278dcf612bf8e033a15c9b429adbb32b0a9210b6 branch 'master' of https://github.com/xiongzhuozhuo/2021-git-demo
da1f73460673c0e876e2995d4a44889d75372bef not-for-merge branch 'dev' of https://github.com/xiongzhuozhuo/2021-git-demo但是在当前分支上运行
git fetch后,再次查看1
2
3
4
5
6
7xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev)
git fetch
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev)
cat .git/FETCH_HEAD
da1f73460673c0e876e2995d4a44889d75372bef branch 'dev' of https://github.com/xiongzhuozhuo/2021-git-demo
278dcf612bf8e033a15c9b429adbb32b0a9210b6 not-for-merge branch 'master' of https://github.com/xiongzhuozhuo/2021-git-demo描述得到验证
查看分支状况
此时本地分支和远程分支是同步的
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master) |
在远程仓库的dev分支添加一次commit,再执行一次git pull拉取远程仓库的更新
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev) |
执行git pull时等同于先执行git fetch再执行git merge,查看FETCH_HEAD内容,发现出现在首行的正式dev,得到验证
1 | xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (dev) |
22. git push
如果本地分支与远程分支已经同步,则可以直接使用git push进行推送,如果本地仓库又有新建的分支,而且没有和远程仓库的分支进行关联,则可以使用`git
验证
查看本地分支与远程分支的同步情况
1
2
3
4xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git branch -vv
dev 53d3f54 [origin/dev] Update file3.txt
* master 278dcf6 [origin/master] Update file1.txt新建一个本地分支,查看分支关联情况
1
2
3
4
5
6
7
8
9xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (master)
git checkout -b tmp
Switched to a new branch 'tmp'
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git branch -vv
dev 53d3f54 [origin/dev] Update file3.txt
master 278dcf6 [origin/master] Update file1.txt
* tmp 278dcf6 Update file1.txttmp现在没有远程分支进行关联
在tmp分支上进行一次提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
echo 'tmp' > tmp.txt
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git add tmp
fatal: pathspec 'tmp' did not match any files
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git add tmp.txt
warning: LF will be replaced by CRLF in tmp.txt.
The file will have its original line endings in your working directory
xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git commit -m 'commit from tmp'
[tmp 11cdb0f] commit from tmp
1 file changed, 1 insertion(+)
create mode 100644 tmp.txt如果直接
git push:当前分支tmp没有上游分支。推送当前分支并将远程分支设置为上游1
2
3
4
5
6xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git push
fatal: The current branch tmp has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin tmp--set -upstream:进行关联的选项先不进行关联,进行测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git push origin tmp
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 271.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.
remote:
remote: Create a pull request for 'tmp' on GitHub by visiting:
remote: https://github.com/xiongzhuozhuo/2021-git-demo/pull/new/tmp
remote:
To https://github.com/xiongzhuozhuo/2021-git-demo.git
* [new branch] tmp -> tmp查看关联情况
1
2
3
4
5xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git branch -vv
dev 53d3f54 [origin/dev] Update file3.txt
master 278dcf6 [origin/master] Update file1.txt
* tmp 11cdb0f commit from tmptmp仍然没有远程分支进行关联,虽然此时远程仓库已经新建有tmp分支
删除远程分支,测试
git push -u命令1
2
3
4xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git push origin -d tmp
To https://github.com/xiongzhuozhuo/2021-git-demo.git
- [deleted] tmp将本地tmp再推送一遍
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git push -u origin tmp
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 271 bytes | 271.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.
remote:
remote: Create a pull request for 'tmp' on GitHub by visiting:
remote: https://github.com/xiongzhuozhuo/2021-git-demo/pull/new/tmp
remote:
To https://github.com/xiongzhuozhuo/2021-git-demo.git
* [new branch] tmp -> tmp
Branch 'tmp' set up to track remote branch 'tmp' from 'origin'.查看此时关联情况:拥有了关联
1
2
3
4
5xiong@Surface-pro6 MINGW64 /c/Files/git-clone/2021-git-demo (tmp)
git branch -vv
dev 53d3f54 [origin/dev] Update file3.txt
master 278dcf6 [origin/master] Update file1.txt
* tmp 11cdb0f [origin/tmp] commit from tmp有了关联之后直接使用
git push就能进行推送
22. git hook
