版本管理的演变
VCS出现之前
- 用目录拷贝区别不同的版本
- 公共文件容易被覆盖
- 成员沟通成本很高,代码集成效率低下
集中式VCS
SVN、CVS
- 有集中的版本管理服务器,服务器上有所有版本演变的历史文件,具备版本搜索和版本比较能力
- 具备文件版本管理和分支管理能力
- 集成效率有明显地提高
- 客户端必须时刻和服务器相连,客户端一般不具备一个项目的所有版本信息
分布式VCS
- 服务端客户端都有完整的版本库
- 脱离服务端,客户端照样可以进行版本管理
- 查看历史和版本比较等多数操作,都不需要访问服务器,比集中式VCS更能提高版本管理效率
Git的特点s
- 优秀的存储能力
- 非凡的性能
- 开源
- 容易做备份
- 支持离线操作
- 很容易定制工作流程
安装git
git官网:https://git-scm.com/book/zh/v2
https://git-scm.com/book/zh/v2/起步-安装-Git
Git最小配置
配置user信息
git config --gloabl user.name 'your_name'
git config --global user.email 'your_email@domain.com'
config的作用域
缺省等同于local
# local只对某个仓库有效
git config --local
# global对当前用户所有仓库有效
git config --global
# system对系统所有登陆的用户有效
git config --system
显示config的配置,加–list
git config --list --local
git config --list --global
git config --list --system
创建git仓库
两种场景;
1.把已有的项目代码纳入到Git管理
cd 项目代码所在的文件夹
git init
2.新建的项目直接使用Git管理
cd 某个文件夹
git init your_project # 会在当前路径下创建和项目名同名的文件夹
cd your_project
yanghuisheng@192 git % git init git_learning
Initialized empty Git repository in /Users/yanghuisheng/Desktop/study/git/git_learning/.git/
yanghuisheng@192 git % cd git_learning
yanghuisheng@192 git_learning % ls -al
total 0
drwxr-xr-x 3 yanghuisheng staff 96 5 23 08:59 .
drwxr-xr-x 3 yanghuisheng staff 96 5 23 08:59 ..
drwxr-xr-x 10 yanghuisheng staff 320 5 23 08:59 .git
yanghuisheng@192 git_learning % git config --global --list
user.name=yanghuisheng
user.email=62yanghuisheng@163.com
core.excludesfile=/Users/yanghuisheng/.gitignore_global
difftool.sourcetree.cmd=opendiff "$LOCAL" "$REMOTE"
difftool.sourcetree.path=
mergetool.sourcetree.cmd=/Applications/Sourcetree.app/Contents/Resources/opendiff-w.sh "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"
mergetool.sourcetree.trustexitcode=true
commit.template=/Users/yanghuisheng/.stCommitMsg
Git工作区和暂存区
工作目录 git add files --> 暂存区
暂存区 git commit --> 版本历史
在工作目录中,执行git add 将要被git管理的文件加入到git暂存区,暂存区的文件可以回退到未修改的状态,也可以提交到历史版本中
通关git status 可以查看暂存区状态
如果已经被git管理的文件发生变更,可以使用git add -u 将所有变更的文件加入到暂存区
https://github.com/tento-js/unforGITtable,这是一个Git的Demo,可以将使用里面的文件进行git的测试
用git管理readme
# 将README文件拷贝到通过git创建的目录中
yanghuisheng@192 git_learning % cp ../unforGITtable-master/README.md .
# 查看git暂存区状态,可以看到README.md未被git管理
yanghuisheng@192 git_learning % git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
# 将README文件添加到git暂存区
yanghuisheng@192 git_learning % git add README.md
# 查看git暂存区状态,可以看到README.md文件已经被添加到了git的暂存区
yanghuisheng@192 git_learning % git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
# 提交git,并写明提交描述
yanghuisheng@192 git_learning % git commit -m'Add readme'
[master (root-commit) 88cb2de] Add readme
1 file changed, 93 insertions(+)
create mode 100644 README.md
# 查看git日志
yanghuisheng@192 git_learning % git log
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661 (HEAD -> master) # 提交版本号
Author: yanghuisheng <62yanghuisheng@163.com> # 作者
Date: Mon May 23 09:35:19 2022 +0800 # 提交时间
Add readme # 提交描述
将index.html和image用git管理起来
yanghuisheng@192 git_learning % cp -r ../unforGITtable-master/images .
yanghuisheng@192 git_learning % cp -r ../unforGITtable-master/index.html .
yanghuisheng@192 git_learning % git status
On branch master
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暂存区
yanghuisheng@192 git_learning % git add images
yanghuisheng@192 git_learning % git add index.html
yanghuisheng@192 git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: images/gh-branch.png
new file: images/gh-logo.png
new file: index.html
# 提交到版本控制
yanghuisheng@192 git_learning % git commit -m'add index and image'
[master 86cfc3e] add index and image
3 files changed, 76 insertions(+)
create mode 100644 images/gh-branch.png
create mode 100644 images/gh-logo.png
create mode 100644 index.html
# 查看git 日志
yanghuisheng@192 git_learning % git log
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
再把css和js复制过来,进行版本管理
yanghuisheng@192 git_learning % cp -r ../unforGITtable-master/styles ../unforGITtable-master/js .
# 将所有文件加入到git暂存区
yanghuisheng@192 git_learning % git add .
# 查看Git暂存区状态
yanghuisheng@192 git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: js/script.js
new file: styles/style.css
yanghuisheng@192 git_learning % git commit -m'add css and js'
[master 5c98e44] add css and js
2 files changed, 80 insertions(+)
create mode 100644 js/script.js
create mode 100644 styles/style.css
yanghuisheng@192 git_learning % git log
commit 5c98e444eb0c80631009cb58465a469ed29364dd (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
修改js和css
# 修改css和js
yanghuisheng@192 git_learning % vim js/script.js
yanghuisheng@192 git_learning % vim styles/style.css
# 查看git暂存区状态
yanghuisheng@192 git_learning % 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: js/script.js
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
# 添加所有修改的文件到暂存区
yanghuisheng@192 git_learning % git add -u
yanghuisheng@192 git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: js/script.js
modified: styles/style.css
# 提交
yanghuisheng@192 git_learning % git commit -m'modify css and js'
[master 487b2ae] modify css and js
2 files changed, 7 insertions(+), 2 deletions(-)
yanghuisheng@192 git_learning % git log
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7 (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
给文件重命名
比如现在将README.md 重命名为README
传统方式
yanghuisheng@192 git_learning % ls
README.md images index.html js styles
# 重命名
yanghuisheng@192 git_learning % mv README.md README
# 查看git暂存区状态
# 删除了README.md,还有一个未管理的README文件
yanghuisheng@192 git_learning % git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: README.md
Untracked files:
(use "git add <file>..." to include in what will be committed)
README
no changes added to commit (use "git add" and/or "git commit -a")
# 添加README到暂存区
yanghuisheng@192 git_learning % git add README
# 移除README.md
yanghuisheng@192 git_learning % git rm README.md
rm 'README.md'
# 查看git暂存区状态
# git 识别出了重命名操作
yanghuisheng@192 git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
yanghuisheng@192 git_learning % ls
README images index.html js styles
使用git的方式
# 先将传统方式的加入暂存区的方式还原
yanghuisheng@192 git_learning % git reset --hard
# HEAD回到了487b2ae modify css and js位置
HEAD is now at 487b2ae modify css and js
# 查看暂存区状态,没有需要提交的
yanghuisheng@192 git_learning % git status
On branch master
nothing to commit, working tree clean
# 查看工作目录,文件也还原了
yanghuisheng@192 git_learning % ls
README.md images index.html js styles
# 使用git的mv进行重命名
yanghuisheng@192 git_learning % git mv README.md README
# 查看暂存区状态
yanghuisheng@192 git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
renamed: README.md -> README
# 提交
yanghuisheng@192 git_learning % git commit -m'move README.md to README'
[master 4178e84] move README.md to README
1 file changed, 0 insertions(+), 0 deletions(-)
rename README.md => README (100%)
# 查看log日志
yanghuisheng@192 git_learning % git log
commit 4178e844aeb16e4f11f42c450341236d51108f3c (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
move README.md to README
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
查看git的版本历史
git log
查看到的历史信息比较全面
yanghuisheng@192 git_learning % git log
commit 4178e844aeb16e4f11f42c450341236d51108f3c (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
move README.md to README
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
git log --oneline
只显示提交信息
yanghuisheng@192 git_learning % git log --oneline
4178e84 (HEAD -> master) move README.md to README
487b2ae modify css and js
5c98e44 add css and js
86cfc3e add index and image
88cb2de Add readme
git log -n
只显示最近几条log
yanghuisheng@192 git_learning % git log -n2 --oneline
4178e84 (HEAD -> master) move README.md to README
487b2ae modify css and js
分支日志
git branch -v 可以查看本地有多少分支
yanghuisheng@192 git_learning % git branch -v
* master 4178e84 move README.md to README
# 基于提交记录,创建一个新的分支temp
yanghuisheng@192 git_learning % git checkout -b temp 5c98e444eb0c806
Switched to a new branch 'temp'
yanghuisheng@192 git_learning % ls
README.md images index.html js styles
# 修改README.md文件
yanghuisheng@192 git_learning % vim README.md
# 加入暂存区并提交
yanghuisheng@192 git_learning % git commit -am'Add test'
[temp 5c6fb65] Add test
1 file changed, 2 insertions(+)
# git branch -v就能看到两个分支的最新提交记录
yanghuisheng@192 git_learning % git branch -v
master 4178e84 move README.md to README
* temp 5c6fb65 Add test
# 查看git log,默认至展示当前分支的log (HEAD -> temp)
yanghuisheng@192 git_learning % git log
commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (HEAD -> temp)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 14:41:06 2022 +0800
Add test
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
# 可以通过 git log --all来查看所有分支的log
yanghuisheng@192 git_learning % git log --all
commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (HEAD -> temp)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 14:41:06 2022 +0800
Add test
commit 4178e844aeb16e4f11f42c450341236d51108f3c (master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
move README.md to README
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
# git log --all --graph 可以添加--graph参数,展示图形化的log
yanghuisheng@192 git_learning % git log --all --graph
* commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (HEAD -> temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 14:41:06 2022 +0800
|
| Add test
|
| * commit 4178e844aeb16e4f11f42c450341236d51108f3c (master)
| | Author: yanghuisheng <62yanghuisheng@163.com>
| | Date: Mon May 23 10:27:55 2022 +0800
| |
| | move README.md to README
| |
| * commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
|/ Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 10:06:22 2022 +0800
|
| modify css and js
|
* commit 5c98e444eb0c80631009cb58465a469ed29364dd
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:57:10 2022 +0800
|
| add css and js
|
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
yanghuisheng@192 git_learning % git log --all --graph --oneline
* 5c6fb65 (HEAD -> temp) Add test
| * 4178e84 (master) move README.md to README
| * 487b2ae modify css and js
|/
* 5c98e44 add css and js
* 86cfc3e add index and image
* 88cb2de Add readme
yanghuisheng@192 git_learning % git log --all --graph --oneline -n4
* 5c6fb65 (HEAD -> temp) Add test
| * 4178e84 (master) move README.md to README
| * 487b2ae modify css and js
|/
* 5c98e44 add css and js
.git目录
在我们的git项目根目录下,执行ls -al,可以看到有一个.git的隐藏目录,进入这个隐藏目录
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % cd .git
yanghuisheng@yanghuishengdeMacBook-Pro .git % ls -al
total 48
drwxr-xr-x 14 yanghuisheng staff 448 5 23 14:41 .
drwxr-xr-x 8 yanghuisheng staff 256 5 23 14:40 ..
-rw-r--r-- 1 yanghuisheng staff 9 5 23 14:41 COMMIT_EDITMSG
-rw-r--r-- 1 yanghuisheng staff 21 5 23 10:42 HEAD
-rw-r--r-- 1 yanghuisheng staff 41 5 23 10:26 ORIG_HEAD
drwxr-xr-x 2 yanghuisheng staff 64 5 23 08:59 branches
-rw-r--r-- 1 yanghuisheng staff 137 5 23 08:59 config
-rw-r--r-- 1 yanghuisheng staff 73 5 23 08:59 description
drwxr-xr-x 12 yanghuisheng staff 384 5 23 08:59 hooks
-rw-r--r-- 1 yanghuisheng staff 642 5 23 14:41 index
drwxr-xr-x 3 yanghuisheng staff 96 5 23 08:59 info
drwxr-xr-x 4 yanghuisheng staff 128 5 23 09:35 logs
drwxr-xr-x 28 yanghuisheng staff 896 5 23 14:41 objects
drwxr-xr-x 4 yanghuisheng staff 128 5 23 08:59 refs
HEAD
查看正在工作的分支
yanghuisheng@yanghuishengdeMacBook-Pro .git % cat HEAD
# 引用,指向refs/heads/temp
ref: refs/heads/temp
yanghuisheng@yanghuishengdeMacBook-Pro .git % git branch -v
# * 表示现在工作再哪个分支上
master 4178e84 move README.md to README
* temp 5c6fb65 Add test
# 切换分支,必须在工作目录根目录下切换
yanghuisheng@yanghuishengdeMacBook-Pro .git % cd ../
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git checkout master
Switched to branch 'master'
# 查看HEAD,切换分支的时候,HEAD内容会变化
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % cat .git/HEAD
ref: refs/heads/master
config
查看配置
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
这里可以看到,当前git项目使用的是global的用户配置
# 添加local配置
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git config --local user.name 'young'
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git config --local user.email '62yanghuisheng@163.com'
# 查看config
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[user]
name = young
email = 62yanghuisheng@163.com
添加local配置之后,再查看config文件,就可以看到这个项目里的用户信息是用local指定的信息
refs
# 查看refs目录
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % cd .git/refs
yanghuisheng@yanghuishengdeMacBook-Pro refs % ls -al
total 0
drwxr-xr-x 4 yanghuisheng staff 128 5 23 08:59 .
drwxr-xr-x 14 yanghuisheng staff 448 5 23 17:21 ..
drwxr-xr-x 4 yanghuisheng staff 128 5 23 14:41 heads
drwxr-xr-x 2 yanghuisheng staff 64 5 23 08:59 tags
查看refs目录,可以看到有heads和tags两个目录。
tag就是标签,也可以叫里程碑,比如版本上线了,就可以打一个tag,标识这是哪一个版本的里程碑。
heads对应的分支,也就是一个独立的开发空间,不同的分支之间互不影响。
yanghuisheng@yanghuishengdeMacBook-Pro refs % cd heads
yanghuisheng@yanghuishengdeMacBook-Pro heads % ls -al
total 16
drwxr-xr-x 4 yanghuisheng staff 128 5 23 14:41 .
drwxr-xr-x 4 yanghuisheng staff 128 5 23 08:59 ..
-rw-r--r-- 1 yanghuisheng staff 41 5 23 10:27 master
-rw-r--r-- 1 yanghuisheng staff 41 5 23 14:41 temp
yanghuisheng@yanghuishengdeMacBook-Pro heads % cat master
4178e844aeb16e4f11f42c450341236d51108f3c
# 查看文件类型
yanghuisheng@yanghuishengdeMacBook-Pro heads % git cat-file -t 4178e844ae
commit
# master文件中存放的是指针指向
yanghuisheng@yanghuishengdeMacBook-Pro heads % git branch -av
* master 4178e84 move README.md to README
temp 5c6fb65 Add test
yanghuisheng@yanghuishengdeMacBook-Pro heads % cat temp
5c6fb656a0b9733e4576dbbae36e4176c9650c88
# 查看指针的内容
yanghuisheng@yanghuishengdeMacBook-Pro heads % git cat-file -p 4178e844ae
tree 13182adf9006bbe3530674940223271d22ac14ea
parent 487b2aecfbcb94aacf79548f66f70f81c5974bc7
author yanghuisheng <62yanghuisheng@163.com> 1653272875 +0800
committer yanghuisheng <62yanghuisheng@163.com> 1653272875 +0800
move README.md to README
objects
yanghuisheng@yanghuishengdeMacBook-Pro heads % cd ../../
yanghuisheng@yanghuishengdeMacBook-Pro .git % cd objects
yanghuisheng@yanghuishengdeMacBook-Pro objects % ls
13 3d 48 85 88 97 c2 d8 info
1f 3f 5a 86 8a ba c5 de pack
2d 41 5c 87 8c bb cb ed
如果文件较多,git会对这些文件打包,打包的文件就放在pack里面
yanghuisheng@yanghuishengdeMacBook-Pro objects % cd d8
yanghuisheng@yanghuishengdeMacBook-Pro d8 % ls -al
total 8
drwxr-xr-x 3 yanghuisheng staff 96 5 23 10:06 .
drwxr-xr-x 28 yanghuisheng staff 896 5 23 14:41 ..
-r--r--r-- 1 yanghuisheng staff 53 5 23 10:06 19b998f0fc3f020e42a23d8a14b920e3e3acac
# 目录名加里面的文件名可以组成一个hash值
yanghuisheng@yanghuishengdeMacBook-Pro d8 % git cat-file -t d819b998f0fc3f020e42a23d8a14b920e3e3acac
tree
# 查看内容 blob是一个文件对象
yanghuisheng@yanghuishengdeMacBook-Pro d8 % git cat-file -p d819b998f0fc3f020e42a23d8a14b920e3e3acac
100644 blob 85554e9244fa2c5a0d1f9843c38b0f2576a8b375 script.js
yanghuisheng@yanghuishengdeMacBook-Pro d8 % git cat-file -t 85554e9244fa2c5a0d1f9843c38b0f2576a8b375
blob
# 查看内容
yanghuisheng@yanghuishengdeMacBook-Pro d8 % git cat-file -p 85554e9244fa2c5a0d1f9843c38b0f2576a8b375
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function(){
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
}
}
commit、tree、blob
一个commit对应一棵树,这棵树就是这个commit的视图,这个视图就是当前提交时本项目所有文件夹及文件的快照,文件夹也是树。
blob只与文件内容有关,与文件名无关,相同的文件,无论文件名是否相同,只对应一个blob,可以大大的节约空间
yanghuisheng@192 git_learning % git cat-file -p 5c98e444
tree bab7bde859e255535078a569ab1752a263a6cdc6
parent 86cfc3efebca5381c2944ed7c97c2d98e67293c0
author yanghuisheng <62yanghuisheng@163.com> 1653271030 +0800
committer yanghuisheng <62yanghuisheng@163.com> 1653271030 +0800
add css and js
yanghuisheng@192 git_learning % git cat-file -p bab7bde85
100644 blob ed70a022b86b505a077208488146d96f51e795a5 README.md
040000 tree 877df74ffb63bd4d32e6c229750f952ebb40d858 images
100644 blob 1f834a2bce4d5b5d695357dc88fbb6a8fd1baf1b index.html
040000 tree 978a2b1ba7c0f07f9758f234b0b7959fe31a2e93 js
040000 tree deca7330feed3ffb6a545bc41ca1b36a9660b3b6 styles
yanghuisheng@192 git_learning % git cat-file -p 877df74
100644 blob cb8bf67496fceb283216908c112332eefd0993dd gh-branch.png
100644 blob c2d35cc14f8d1c016791117e71daf7ab562875a1 gh-logo.png
yanghuisheng@192 git_learning % git cat-file -p 978a2b
100644 blob c5d5a9aff0137850ed64e1c34c16483b074f66b7 script.js
yanghuisheng@192 git_learning % git cat-file -p deca73
100644 blob 5c6165b0fb8f4438cff3018ea99365b2d7e6cb17 style.css
objects的变化
# 新建一个git项目
yanghuisheng@192 git % git init watch_objects
Initialized empty Git repository in /Users/yanghuisheng/Desktop/study/git/watch_objects/.git/
# 进入新建的git项目
yanghuisheng@192 git % cd watch_objects
# 创建文件夹
yanghuisheng@192 watch_objects % mkdir doc
# 查看git暂存区状态,发现没有任何变化,说明git不会理会空文件夹
yanghuisheng@192 watch_objects % git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# 进入文件夹,创建一个readme文件
yanghuisheng@192 watch_objects % cd doc
yanghuisheng@192 doc % echo "hello,world" > readme
# 返回项目跟目录
yanghuisheng@192 doc % cd ../
# 查看git暂存区状态,可以看到已经检测到了未添加到暂存区的信息
yanghuisheng@192 watch_objects % git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
doc/
nothing added to commit but untracked files present (use "git add" to track)
# 查看objects下是否有文件生成,发现并没有文件
yanghuisheng@192 watch_objects % find .git/objects/ -type f
# 将doc加入到暂存区
yanghuisheng@192 watch_objects % git add doc
# 查看git状态
yanghuisheng@192 watch_objects % git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: doc/readme
# 查看objects,发现有文件生成,说明文件加入到暂存区,git就会生成文件
yanghuisheng@192 watch_objects % find .git/objects/ -type f
.git/objects//2d/832d9044c698081e59c322d5a2a459da546469
# 查看文件类型,是blob类型
yanghuisheng@192 watch_objects % git cat-file -t 2d832d9
blob
# 查看文件内容,就是刚才写入readme的内容
yanghuisheng@192 watch_objects % git cat-file -p 2d832d9
hello,world
# 将暂存区的文件提交
yanghuisheng@192 watch_objects % git commit -m'Add readme'
[master (root-commit) 2d3e036] Add readme
1 file changed, 1 insertion(+)
create mode 100644 doc/readme
# 查看objects下的文件,发现文件数量变成了4个
yanghuisheng@192 watch_objects % find .git/objects/ -type f
.git/objects//08/3e18d286d8d2a9eda8c62d9d98935dcc07ca4c
.git/objects//ba/8711bd7540faa22e4e76a1cf5c78501fa4e162
.git/objects//2d/832d9044c698081e59c322d5a2a459da546469
.git/objects//2d/3e03654ad2f521cf537d62455b88da6143bd13
# 查看各个文件的类型和内容
yanghuisheng@192 watch_objects % git cat-file -t 083e18d2
tree
yanghuisheng@192 watch_objects % git cat-file -p 083e18d2
040000 tree ba8711bd7540faa22e4e76a1cf5c78501fa4e162 doc
yanghuisheng@192 watch_objects % git cat-file -t ba8711bd
tree
yanghuisheng@192 watch_objects % git cat-file -p ba8711bd
100644 blob 2d832d9044c698081e59c322d5a2a459da546469 readme
yanghuisheng@192 watch_objects % git cat-file -t 2d832d90
blob
yanghuisheng@192 watch_objects % git cat-file -p 2d832d90
hello,world
yanghuisheng@192 watch_objects % git cat-file -t 2d3e036
commit
yanghuisheng@192 watch_objects % git cat-file -p 2d3e036
tree 083e18d286d8d2a9eda8c62d9d98935dcc07ca4c
author yanghuisheng <62yanghuisheng@163.com> 1653314288 +0800
committer yanghuisheng <62yanghuisheng@163.com> 1653314288 +0800
Add readme
分离头指针情况的注意事项
在git_learning项目下
# 查看git日志
yanghuisheng@192 git_learning % git log
commit 4178e844aeb16e4f11f42c450341236d51108f3c (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
move README.md to README
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
# 基于提交的hash进行了checkout
yanghuisheng@192 git_learning % git checkout 487b2aecfb
Note: checking out '487b2aecfb'.
# 你正处于分离头指针状态,你可以做一些变更,产生commit,也可以将你生成的commit丢弃掉,不会影响其他分值
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 performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 487b2ae... modify css and js
基于提交的hash进行了checkout就会产生分离头指针,本质就是目前正工作在一个没有分支的状态下,如果做了很多commit,此时切换了其他分支去做别的开发,然后再切换回产生了分离头指针的分支,此时因为分离头指针没有和任何分支挂钩,可能就直接被git当做垃圾给清理掉。如果提交挂载了分支,git肯定不会将它清理掉。
如果需要验证某些改动,可以使用分离头指针,如果不改动不需要,就可以直接丢弃了。
例如,修改项目的style.css
yanghuisheng@192 git_learning % vim styles/style.css
# 查看git暂存区状态,也会有分离有指针的提示,是基于commit 487b2ae 的
yanghuisheng@192 git_learning % git status
HEAD detached at 487b2ae
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: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
# 提交
yanghuisheng@192 git_learning % git commit -am'change background'
[detached HEAD f2a5002] change background
1 file changed, 2 deletions(-)
# 查看git日志,发现master并没有指向任何分支
yanghuisheng@192 git_learning % git log
commit f2a500250909f2f5c9ced6ded82d31412c3a43ed (HEAD)
Author: young <62yanghuisheng@163.com>
Date: Mon May 23 23:57:29 2022 +0800
change background
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
add index and image
commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
# 切换分支到master,此时git会给出提示
yanghuisheng@192 git_learning % git checkout master
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
f2a5002 change background
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> f2a5002
Switched to branch 'master'
# 如果需要保留这些修改,就按照git提上,将这些commit绑定到一个分支上
yanghuisheng@192 git_learning % git branch fix_css f2a5002
yanghuisheng@192 git_learning % git branch -v
fix_css f2a5002 change background
* master 4178e84 move README.md to README
temp 5c6fb65 Add test
进一步理解HEAD和branch
# 创建新分支,并切换到新分支,创建新分支fix_readme 基于fix_css
yanghuisheng@192 git_learning % git checkout -b fix_readme fix_css
Switched to a new branch 'fix_readme'
yanghuisheng@192 git_learning % git log -n1
commit f2a500250909f2f5c9ced6ded82d31412c3a43ed (HEAD -> fix_readme, fix_css)
Author: young <62yanghuisheng@163.com>
Date: Mon May 23 23:57:29 2022 +0800
change background
# 查看.git下的HEAD
yanghuisheng@192 git_learning % cat .git/HEAD
ref: refs/heads/fix_readme
HEAD不仅可以指向分支,也可以在分离头状态下指向commit,切换分支时,HEAD就会指向新的分支。
yanghuisheng@192 git_learning % cat .git/refs/heads/fix_readme
f2a500250909f2f5c9ced6ded82d31412c3a43ed
yanghuisheng@192 git_learning % git cat-file -t f2a5002
commit
HEAD最终是指向某一个commit的。
diff
使用git diff commit1 commit2
就可以查看两次提交之间的差异,因为HEAD是指向commit的
HEAD~N:表示HEAD的前面n个
HEAD1表示HEAD的父亲,也可以写作HEAD,等同于HEAD~1
HEAD11表示HEAD父亲的父亲,也可以写作HEAD^^,等同于HEAD~2
所以也可以使用git diff HEAD HEAD~1
去进行差异比对
删除不需要的分支
yanghuisheng@192 git_learning % git branch -v
4178e84 4178e84 move README.md to README
fix_css f2a5002 change background
fix_readme f2a5002 change background
* master 4178e84 move README.md to README
temp 5c6fb65 Add test
比如新建了一个分支,但是命名使用了一个hash值,正常情况下,分支名应该描述分支的功能
查看分支结构,分支4178e84和master指针指向的是同一个commit
yanghuisheng@192 git_learning % git log --all --graph
* commit f2a500250909f2f5c9ced6ded82d31412c3a43ed (fix_readme, fix_css)
| Author: young <62yanghuisheng@163.com>
| Date: Mon May 23 23:57:29 2022 +0800
|
| change background
|
| * commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (HEAD -> temp)
| | Author: yanghuisheng <62yanghuisheng@163.com>
| | Date: Mon May 23 14:41:06 2022 +0800
| |
| | Add test
| |
| | * commit 4178e844aeb16e4f11f42c450341236d51108f3c (master, 4178e84)
| |/ Author: yanghuisheng <62yanghuisheng@163.com>
|/| Date: Mon May 23 10:27:55 2022 +0800
| |
| | move README.md to README
| |
* | commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
|/ Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 10:06:22 2022 +0800
|
| modify css and js
|
* commit 5c98e444eb0c80631009cb58465a469ed29364dd
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:57:10 2022 +0800
|
删除4178e84分支
yanghuisheng@192 git_learning % git branch -d 4178e84
# 分支没有完全被merge,如果确实需要删除,使用git branch -D命令
error: The branch '4178e84' is not fully merged.
If you are sure you want to delete it, run 'git branch -D 4178e84'.
yanghuisheng@192 git_learning % git branch -D 4178e84
Deleted branch 4178e84 (was 4178e84).
删除fix分支
yanghuisheng@192 git_learning % 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'.
yanghuisheng@192 git_learning % git branch -D fix_readme
Deleted branch fix_readme (was f2a5002).
yanghuisheng@192 git_learning % git branch -d fix_css
error: The branch 'fix_css' is not fully merged.
If you are sure you want to delete it, run 'git branch -D fix_css'.
yanghuisheng@192 git_learning % git branch -D fix_css
Deleted branch fix_css (was f2a5002).
此时再查看分支,分支就只剩下两个了
yanghuisheng@192 git_learning % git branch -v
master 4178e84 move README.md to README
* temp 5c6fb65 Add test
修改commit的message
修改最新commit的message
yanghuisheng@192 git_learning % git log -n1
commit 4178e844aeb16e4f11f42c450341236d51108f3c (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
move README.md to README
比如感觉这个message不太合适,要进行修改
yanghuisheng@192 git_learning % git commit --amend
# 修改之后可以看到message已经变了
[master 82293df] rename README.md to README
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
1 file changed, 0 insertions(+), 0 deletions(-)
rename README.md => README (100%)
# 通过log查看,message确实变了
yanghuisheng@192 git_learning % git log -n1
commit 82293dfa7f4df7bea352778e4c8623fa031d25ee (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
rename README.md to README
修改老旧commit的message
# 查看最近3次的log
yanghuisheng@192 git_learning % git log -n3
commit 82293dfa7f4df7bea352778e4c8623fa031d25ee (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
rename README.md to README
commit 487b2aecfbcb94aacf79548f66f70f81c5974bc7
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify css and js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
# git rebase,变基,-i表示一个交互式界面,要修改modify css and js这个commit的message,需要处理他的parent
yanghuisheng@192 git_learning % git rebase -i 5c98e444eb0c8
进入git rebase的策略交互界面
命令 | 描述 |
---|---|
p, pick | 保留当前commit,不做处理 |
r, reword | 修改commit message |
e, edit | 修改这个commit作的修改。比如某个commit漏掉了什么配置,想要再提交新的文件; 或者删除一些无用代码,等等都可以用这个命令 |
s, squash | 保留这个commit的修改,但是把它合并到前一个commit中 |
d, drop | 删除commit |
这是一个vi窗口,需使用vi的命令操作修改保存
pick 487b2ae modify css and js
pick 82293df rename README.md to README
# Rebase 5c98e44..82293df onto 5c98e44 (2 commands)
# Commands:
# p, pick = use commit 直接使用commit
# r, reword = use commit, but edit the commit message commit的编程文件要保留,但是要修改message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
修改之后
reword 487b2ae modify css and js
pick 82293df rename README.md to README
# Rebase 5c98e44..82293df onto 5c98e44 (2 commands)
# Commands 表示这是个命令
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
保存并退出,会进入另一个交互界面
modify css and js
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author: yanghuisheng <62yanghuisheng@163.com>
# Date: Mon May 23 10:06:22 2022 +0800
#
# interactive rebase in progress; onto 5c98e44
# Last command done (1 command done):
# reword 487b2ae modify one css and one js
# Next command to do (1 remaining command):
# pick 82293df rename README.md to README
# You are currently editing a commit while rebasing branch 'master' on '5c98e44'.
#
# Changes to be committed:
# modified: js/script.js
# modified: styles/style.css
#
修改message
modify one css and one js
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author: yanghuisheng <62yanghuisheng@163.com>
# Date: Mon May 23 10:06:22 2022 +0800
#
# interactive rebase in progress; onto 5c98e44
# Last command done (1 command done):
# reword 487b2ae modify one css and one js
# Next command to do (1 remaining command):
# pick 82293df rename README.md to README
# You are currently editing a commit while rebasing branch 'master' on '5c98e44'.
#
# Changes to be committed:
# modified: js/script.js
# modified: styles/style.css
#
修改之后保存并退出
# detached HEAD 分离头指针
[detached HEAD a852854] modify one css and one js
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
2 files changed, 7 insertions(+), 2 deletions(-)
# 变基并且修改了头指针
Successfully rebased and updated refs/heads/master.
git先分离头指针,然后再进行调整,调整完之后再最新的commit产生的指针指向master
yanghuisheng@192 git_learning % git log -n3
commit 868eb810c8c12676eba84163764ad08094b3bf3a (HEAD -> master)
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:27:55 2022 +0800
rename README.md to README
commit a852854fcaf2005035b3fceabe434d18cc06a14a
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 10:06:22 2022 +0800
modify one css and one js
commit 5c98e444eb0c80631009cb58465a469ed29364dd
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:57:10 2022 +0800
add css and js
查看日志,可以看到message已经被修改了,HEAD的指向也从82293dfa7f4df7bea352778e4c8623fa031d25ee变成了
868eb810c8c12676eba84163764ad08094b3bf3a
合并commit
把连续的多个commit整理成一个
yanghuisheng@192 git_learning % git log --graph
* commit 868eb810c8c12676eba84163764ad08094b3bf3a (HEAD -> master)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 10:27:55 2022 +0800
|
| rename README.md to README
|
* commit a852854fcaf2005035b3fceabe434d18cc06a14a
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 10:06:22 2022 +0800
|
| modify one css and one js
|
* commit 5c98e444eb0c80631009cb58465a469ed29364dd
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:57:10 2022 +0800
|
| add css and js
|
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
将86cfc3e,5c98e444,a852854,三个commit合并成一个
git rebase -i 88cb2de6e
进入git rebase的策略设置界面
pick 86cfc3e add index and image
pick 5c98e44 add css and js
pick a852854 modify one css and one js
pick 868eb81 rename README.md to README
# Rebase 88cb2de..868eb81 onto 88cb2de (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
必须要有一个pick,基于哪一个commit去合并,这里选择最早的一个86cfc3e,修改之后保存提交
pick 86cfc3e add index and image
s 5c98e44 add css and js
s a852854 modify one css and one js
pick 868eb81 rename README.md to README
# Rebase 88cb2de..868eb81 onto 88cb2de (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
# This is a combination of 3 commits.
# 这是一个3个commit合并的操作,可以增加一个新的注释
Create a complete web page
# This is the 1st commit message:
add index and image
# This is the commit message #2:
add css and js
# This is the commit message #3:
modify one css and one js
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author: yanghuisheng <62yanghuisheng@163.com>
# Date: Mon May 23 09:48:02 2022 +0800
#
# interactive rebase in progress; onto 88cb2de
# Last commands done (3 commands done):
# s 5c98e44 add css and js
# s a852854 modify one css and one js
# Next command to do (1 remaining command):
# pick 868eb81 rename README.md to README
# You are currently rebasing branch 'master' on '88cb2de'.
#
# Changes to be committed:
# new file: images/gh-branch.png
# new file: images/gh-logo.png
# new file: index.html
# new file: js/script.js
# new file: styles/style.css
#
修改之后保存
[detached HEAD a13c7b1] Create a complete web page
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:48:02 2022 +0800
5 files changed, 161 insertions(+)
create mode 100644 images/gh-branch.png
create mode 100644 images/gh-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.
查看日志
yanghuisheng@192 git_learning % git log --graph
* commit 20c15c6ae929f4b434a401ebf0f4fcef3a6981cc (HEAD -> master)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 10:27:55 2022 +0800
|
| rename README.md to README
|
* commit a13c7b18fc5c3f45bc4612940831b9e85136360a
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
把间隔的commit合并成一个
把readme相关的代码融合
git rebase -i 88cb2de6e
pick a13c7b1 Create a complete web page
pick 20c15c6 rename README.md to README
# Rebase 88cb2de..20c15c6 onto 88cb2de (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
可以看到这个里面的pick是不够的,需要自己添加最初的add readme 的commit
pick 88cb2de6e
pick a13c7b1 Create a complete web page
pick 20c15c6 rename README.md to README
# Rebase 88cb2de..20c15c6 onto 88cb2de (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
然后将要合并的commit放在一起,无关的commit不动
pick 88cb2de6e
s 20c15c6 rename README.md to README
pick a13c7b1 Create a complete web page
# Rebase 88cb2de..20c15c6 onto 88cb2de (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# 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.
#
# Note that empty commits are commented out
保存后退出
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 reset'
interactive rebase in progress; onto 88cb2de
Last command done (1 command done):
pick 88cb2de
Next commands to do (2 remaining commands):
s 20c15c6 rename README.md to README
pick a13c7b1 Create a complete web page
You are currently rebasing branch 'master' on '88cb2de'.
nothing to commit, working tree clean
Could not apply 88cb2de...
查看git状态
yanghuisheng@192 git_learning % git status
interactive rebase in progress; onto 88cb2de
Last command done (1 command done):
pick 88cb2de
Next commands to do (2 remaining commands):
s 20c15c6 rename README.md to README
pick a13c7b1 Create a complete web page
(use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on '88cb2de'.
(all conflicts fixed: run "git rebase --continue")
nothing to commit, working tree clean
根据提示,执行git rebase --continue
git rebase --continue
出现了修改界面
# This is a combination of 2 commits.
add ReadMe
# This is the 1st commit message:
Add readme
# This is the commit message #2:
rename README.md to README
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Author: yanghuisheng <62yanghuisheng@163.com>
# Date: Mon May 23 09:35:19 2022 +0800
#
# interactive rebase in progress; onto 88cb2de
# Last commands done (2 commands done):
# pick 88cb2de
# s 20c15c6 rename README.md to README
# Next command to do (1 remaining command):
# pick a13c7b1 Create a complete web page
# You are currently rebasing branch 'master' on '88cb2de'.
#
#
# Initial commit
#
# Changes to be committed:
# new file: README
#
保存退出
[detached HEAD e81779d] add ReadMe
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
1 file changed, 93 insertions(+)
create mode 100644 README
Successfully rebased and updated refs/heads/master.
查看log,可以看到两个commit合并到一起了
yanghuisheng@192 git_learning % git log --graph
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b (HEAD -> master)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
查看所有分支信息
yanghuisheng@192 git_learning % git log --all --graph
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b (HEAD -> master)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 14:41:06 2022 +0800
|
| Add test
|
* commit 5c98e444eb0c80631009cb58465a469ed29364dd
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:57:10 2022 +0800
|
| add css and js
|
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
可以看到,master和temp分支已经被拆成了两个单独的树
比较暂存区和HEAD所含文件的差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
nothing to commit, working tree clean
# 修改index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
# 想修改后的文件加入暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git add index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
# --cached 表示暂存区和HEAD的差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff --cached
diff --git a/index.html b/index.html
index 1f834a2..c017fd3 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>A Git Demo</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
@@ -73,4 +73,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
# 再次修改
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git add index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff --cached
diff --git a/index.html b/index.html
index 1f834a2..9ad1f8f 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>A Git Demo from Github</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
@@ -73,4 +73,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git commit -m'change title'
[master fe75c75] change title
1 file changed, 2 insertions(+), 2 deletions(-)
工作区与暂存区差异比较
# 修改index
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
# 加入到暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git add index.html
# 修改style.css
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim styles/style.css
# git diff 默认比较工作区与暂存区的差异,index文件已经加入到了暂存区,所以工作区和暂存区中index文件是一样的
# 暂存区的中background-color: #1D2031,工作区中变成了background-color: green
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff
diff --git a/styles/style.css b/styles/style.css
index 3feca3a..03d8df3 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -2,7 +2,7 @@
body{
- background-color: #1D2031;
+ background-color: green;
font-family: 'Ubuntu', sans-serif;
color: white;
}
# 继续修改README文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim README
# 查看工作区与暂存区的差异,可以看到有两个文件差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff
# README差异
diff --git a/README b/README
index ed70a02..c85f590 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
+HELLO GIT
A quick GitHub Demo to show the students at SMU
![gh-logo](https://user-images.githubusercontent.com/15793521/28505229-7317ae18-6fe7-11e7-8665-ae3539a72266.png)
# style.css差异
diff --git a/styles/style.css b/styles/style.css
index 3feca3a..03d8df3 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -2,7 +2,7 @@
body{
- background-color: #1D2031;
+ background-color: green;
font-family: 'Ubuntu', sans-serif;
color: white;
}
git diff 命令会将所有工作区与暂存区的差异文件都显示出来,比如只想看某一个或某几个文件的改动,可以指定文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff -- README
diff --git a/README b/README
index ed70a02..c85f590 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
+HELLO GIT
A quick GitHub Demo to show the students at SMU
![gh-logo](https://user-images.githubusercontent.com/15793521/28505229-7317ae18-6fe7-11e7-8665-ae3539a72266.png)
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff -- styles/style.css
diff --git a/styles/style.css b/styles/style.css
index 3feca3a..03d8df3 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -2,7 +2,7 @@
body{
- background-color: #1D2031;
+ background-color: green;
font-family: 'Ubuntu', sans-serif;
color: white;
}
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff -- styles/style.css README
diff --git a/README b/README
index ed70a02..c85f590 100644
--- a/README
+++ b/README
@@ -1,3 +1,4 @@
+HELLO GIT
A quick GitHub Demo to show the students at SMU
![gh-logo](https://user-images.githubusercontent.com/15793521/28505229-7317ae18-6fe7-11e7-8665-ae3539a72266.png)
diff --git a/styles/style.css b/styles/style.css
index 3feca3a..03d8df3 100644
--- a/styles/style.css
+++ b/styles/style.css
@@ -2,7 +2,7 @@
body{
- background-color: #1D2031;
+ background-color: green;
font-family: 'Ubuntu', sans-serif;
color: white;
}
让暂存区恢复成和HEAD一样
# 查看git状态,暂存区有3个文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
# git reset HEAD 后面可以跟指定的文件,将指定的文件恢复成和HEAD一样,不指定就是全部恢复
(use "git reset HEAD <file>..." to unstage)
modified: README
modified: index.html
modified: styles/style.css
# 全部恢复到HEAD
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset HEAD
Unstaged changes after reset:
M README
M index.html
M styles/style.css
# 查看git状态,发现3个文件都恢复到了工作区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % 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: README
modified: index.html
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
# 查看工作区与暂存区差异,返回为空,说明工作区与暂存区完全一直
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff --cached
yanghuisheng@yanghuishengdeMacBook-Pro git_learning %
让工作区文件恢复为和暂存区一样
# 查看git状态,可以看到工作区有3个文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % 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: README
modified: index.html
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
# 修改index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
# 加入暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git add index.html
# 查看git状态,index.html已经被加入了暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: README
modified: styles/style.css
# 再次修改index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
# 查看git状态,发现工作区和暂存区都有一个index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: README
modified: index.html
modified: styles/style.css
# 查看index.html工作区和暂存区的差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff -- index.html
diff --git a/index.html b/index.html
index 681425d..46df43e 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>一个Git Demo</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
# 此时,如果觉的工作区的修改不如暂存区的修改,想要回退,可以使用 git checkout -- <file>...
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git checkout -- index.html
# 查看index.html的工作区和暂存区的差异,发现没有差异了,说明工作区和暂存区相同了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff -- index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning %
# 查看git状态,发现工作区的index.html已经没有了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: index.html
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: README
modified: styles/style.css
# 也可以查看文件本身,是否已经回退到了暂存区的状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
取消暂存区部分文件的更改
# 查看git状态,暂存区有三个文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README
modified: index.html
modified: styles/style.css
# 将styles/style.css恢复到HEAD状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset HEAD -- styles/style.css
Unstaged changes after reset:
M styles/style.css
# 查看Git状态,styles/style.css已经恢复到工作区了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README
modified: index.html
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: styles/style.css
# 将README,index.html恢复到HEAD状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset HEAD -- README index.html
Unstaged changes after reset:
M README
M index.html
M styles/style.css
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % 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: README
modified: index.html
modified: styles/style.css
no changes added to commit (use "git add" and/or "git commit -a")
消除最近几次的提交
# 查看当前所在分支,为temp分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -v
master fe75c75 change title
* temp 5c6fb65 Add test
# 查看temp分支log
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --graph
* commit 5c6fb656a0b9733e4576dbbae36e4176c9650c88 (HEAD -> temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 14:41:06 2022 +0800
|
| Add test
|
* commit 5c98e444eb0c80631009cb58465a469ed29364dd
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:57:10 2022 +0800
|
| add css and js
|
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
# 将暂存区和工作区恢复到add index and image这次提交
# --hard 将工作区和暂存区都恢复到指定的commit
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset --hard 86cfc3efeb
HEAD is now at 86cfc3e add index and image
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --graph
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (HEAD -> temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
查看不同提交的指定文件差异
# 查看所有分支日志
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (HEAD -> temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
# 查看temp分支和master分支的差异,会显示所有差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff temp master
diff --git a/README.md b/README
similarity index 100%
rename from README.md
rename to README
diff --git a/index.html b/index.html
index 1f834a2..9ad1f8f 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>A Git Demo from Github</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
@@ -73,4 +73,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
diff --git a/js/script.js b/js/script.js
new file mode 100644
index 0000000..85554e9
--- /dev/null
+++ b/js/script.js
@@ -0,0 +1,16 @@
+var acc = document.getElementsByClassName("accordion");
+var i;
...
# 查看temp分支和master分支的指定文件的差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff temp master -- index.html
diff --git a/index.html b/index.html
index 1f834a2..9ad1f8f 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>A Git Demo from Github</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
@@ -73,4 +73,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
# 查看指定commit之间的指定文件的差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git diff 86cfc3ef fe75c754 -- index.html
diff --git a/index.html b/index.html
index 1f834a2..9ad1f8f 100644
--- a/index.html
+++ b/index.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Git Demo</title>
+ <title>A Git Demo from Github</title>
<link rel="icon" href="images/gh-branch.png">
<link href="https://fonts.googleapis.com/css?family=Exo:700i" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
@@ -73,4 +73,4 @@
<script src="js/script.js"></script>
</body>
-</html>
\ No newline at end of file
+</html>
正确删除文件的方式
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % ls
README.md images index.html
# 删除README.md
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % rm README.md
# 查看git状态,工作区已经删除了README.md
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: README.md
no changes added to commit (use "git add" and/or "git commit -a")
# 将要清理的文件添加到暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git rm README.md
rm 'README.md'
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: README.md
# 将暂存区还原
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset --hard HEAD
HEAD is now at 86cfc3e add index and image
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
nothing to commit, working tree clean
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % ls
README.md images index.html
# 执行git rm 加文件名,git就会直接被删除的文件放入到暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git rm README.md
rm 'README.md'
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: README.md
开发中临时加塞了紧急任务怎么处理
比如正在开发新的功能,临时需要解决线上bug
# 比如现在正在编写一个新功能,此时,需要临时修复线上bug,需要修改这个文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash
Saved working directory and index state WIP on temp: 86cfc3e add index and image
# 类似于一个堆栈,执行pop会将最新的弹出来
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash list
stash@{0}: WIP on temp: 86cfc3e add index and image
# 查看git状态,工作区和暂存区已经清空了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch temp
nothing to commit, working tree clean
# git stash apply的作用:将存在stash里的东西弹出来,把里面的东西放到工作区;stash堆栈里的数据还在,可以重复使用
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash apply
On branch temp
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash list
stash@{0}: WIP on temp: 86cfc3e add index and image
# git stash pop,pop的时候提示本地有修改,可能会被覆盖
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash pop
error: Your local changes to the following files would be overwritten by merge:
README.md
Please commit your changes or stash them before you merge.
Aborting
# 将之前apply出来的工作区还原
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git reset --hard HEAD
HEAD is now at 86cfc3e add index and image
# 重新执行git stash pop,可以看到,pop命令删掉了stash里的数据 Dropped refs/stash@{0}
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash pop
On branch temp
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: README.md
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (b3b748f0db9353e4b50dcdb365800f51e5e402e2)
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git stash list
yanghuisheng@yanghuishengdeMacBook-Pro git_learning %
指定不需要git管理的文件
在.gitignore
文件中指定不需要被git管理的文件规则,支持通配符。
文件名必须为.gitignore
,且放置在git项目的根目录。
如果只需要控制目录,需要在相关配置末尾增加/,例如*.xyz
表示以.xyz
结尾的文件和目录都会被排除调,如果配置为*.xyz/
,那么近排除.xyz
结尾的目录
将git仓库备份到本地
常用传输协议
常用协议 | 语法格式 | 说明 |
---|---|---|
本地协议(1) | /path/to/repo.git | 哑协议 |
本地协议(2) | file:///path/to/repo.git | 智能协议 |
http/https协议 | http://git-server.com:port/path/to/repo.git https://git-server.com:port/path/to/repo.git |
平时接触到的都是智能协议 |
ssh协议 | user@git-server.com:path/to/repo.git | 工作中常用的智能协议 |
哑协议与智能协议
直观区别:哑协议传输进度不可见;智能协议传 输可见
传输速度:智能传输协议比哑传输速度快
创建一个备份目录:666-backup,在这里进行备份操作
yanghuisheng@yanghuishengdeMacBook-Pro git % mkdir 666-backup
yanghuisheng@yanghuishengdeMacBook-Pro git % cd 666-backup
# 协议为哑协议,没有进度;--bare 不带工作区;
yanghuisheng@yanghuishengdeMacBook-Pro 666-backup % git clone --bare /Users/yanghuisheng/Desktop/study/git/git_learning ya.git
Cloning into bare repository 'ya.git'...
done.
# 智能协议,有进度;
yanghuisheng@yanghuishengdeMacBook-Pro 666-backup % git clone --bare file:///Users/yanghuisheng/Desktop/study/git/git_learning zhineng.git
Cloning into bare repository 'zhineng.git'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 20 (delta 2), reused 0 (delta 0)
Receiving objects: 100% (20/20), 18.31 KiB | 18.31 MiB/s, done.
Resolving deltas: 100% (2/2), done.
# 进入git_learning项目
# 关联远端地址
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git remote add zhineng file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git
# 查看远端分支
yanghuisheng@yanghuishengdeMacBook-Pro zhineng.git % git branch -v
master fe75c75 change title
* temp 86cfc3e add index and image
# 查看本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -v
master fe75c75 change title
* temp 86cfc3e add index and image
# 创建本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git checkout -b young temp
M README.md
Switched to a new branch 'young'
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -v
master fe75c75 change title
temp 86cfc3e add index and image
* young 86cfc3e add index and image
# git push remote
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push zhineng
fatal: The current branch young has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream zhineng young
# 根据提示重新推送
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push --set-upstream zhineng young
Total 0 (delta 0), reused 0 (delta 0)
To file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git
* [new branch] young -> young
Branch 'young' set up to track remote branch 'young' from 'zhineng'.
# 查看远端分支,可以看到在zhineng.git中,young分支已经被推送过来了
yanghuisheng@yanghuishengdeMacBook-Pro zhineng.git % git branch -v
master fe75c75 change title
* temp 86cfc3e add index and image
young 86cfc3e add index and image
创建公私钥
github
https://docs.github.com/cn/authentication/connecting-to-github-with-ssh
创建公私钥
# 根据自己的github邮箱创建公公私钥,一路回车即可
[root@101 app]# ssh-keygen -t rsa -b 4096 -C "62yanghuisheng@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:eJ4WHeMfMJyEmLdzCXLA0inHPSzh4qlTGLd2EEl4ne0 62yanghuisheng@163.com
The key's randomart image is:
+---[RSA 4096]----+
| oo*o@ .. |
| . =o& Xo . |
| ..++.* +B. |
| = = .Eoo= |
| . * o Soo . |
| + . o o . . |
| o + . |
| . . |
| |
+----[SHA256]-----+
[root@101 app]#
点击github页面左上角头像附近的倒三角,进入Settings,然后点击左边菜单的SSH and GPG keys,进入SSH key的设置页面
上面生成公私钥的时候,显示了公私钥的路径
# 私钥
Your identification has been saved in /root/.ssh/id_rsa.
# 公钥
Your public key has been saved in /root/.ssh/id_rsa.pub.
执行命令,获取公钥,粘贴到key这一栏中,title可以描述这个key的作用
cat /root/.ssh/id_rsa.pub
然后点击Add SSH key将SSH key添加进去
gitee
https://gitee.com/help/articles/4191#article-header0
点击Gitee页面右上角头像,进入设置页面,查看邮箱管理中的提交邮箱
,生成公钥的方式与github一致,但是邮箱需要使用这里的提交邮箱,从左侧菜单进入SSH公钥页面,将公钥写入
gitlab
点击gitlab页面左上角的头像,选择Settings,然后从左边菜单的SSH keys进入配置页面,生成公钥的方式与github相同
本地配置多个SSH Key
如果我们在工作中使用的是gitlab,同时又有自己的gitee或者github项目,这事需要在本地同时配置多个SSH key,以方便我们的代码提交和更新。
首先,生成公私钥的时候,需要指定生成的私钥的名称。
生成github的公私钥
ssh-keygen -t rsa -b 4096 -C "62yanghuisheng@163.com" -f ~/.ssh/id_rsa_github
生成gitee的公私钥匙
ssh-keygen -t rsa -b 4096 -C "62yanghuisheng@163.com" -f ~/.ssh/id_rsa_gitee
查看~/.ssh
下是否有config文件,如果没有则创建一个
host gitee.com
Hostname gitee.com
User yanghuisheng
IdentityFile ~/.ssh/id_rsa_gitee
host 10.10.200.204
Hostname 10.10.200.204
User yanghuisheng
IdentityFile ~/.ssh/id_rsa
host github.com
Hostname github.com
User yanghuisheng
IdentityFile ~/.ssh/id_rsa_github
# Host : Host可以看作是一个你要识别的模式,对识别的模式,进行配置对应的的主机名和ssh文件
# HostName : 要登录主机的主机名
# User : 登录名
# IdentityFile : 指明上面User对应的identityFile路径
本地仓库同步到远程仓库
# 查看git远端配置,可以看到只有本地的备份地址
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git remote -v
zhineng file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git (fetch)
zhineng file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git (push)
在github上创建一个项目
可以看到github的默认分支名已经从以前的master变为了main,我们再创建一个master分支
复制项目的ssh地址
进入本地的git_learning项目
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git remote github git@github.com:yanghuisheng/git_learning.git
error: Unknown subcommand: github
usage: git remote [-v | --verbose]
# 需要使用 git remote add name url 的形式添加远端站点
or: git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>
or: git remote rename <old> <new>
# 如果以后不想要某个远端站点了,可以通过name进行移除
or: git remote remove <name>
or: git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
or: git remote [-v | --verbose] show [-n] <name>
or: git remote prune [-n | --dry-run] <name>
or: git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]
or: git remote set-branches [--add] <name> <branch>...
or: git remote get-url [--push] [--all] <name>
or: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>
-v, --verbose be verbose; must be placed before a subcommand
# 添加github的远端站点,并且命名为github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git remote add github git@github.com:yanghuisheng/git_learning.git
# 查看项目关联的远端关联,往远端推送是push,从远端拉取是fetch
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git remote -v
github git@github.com:yanghuisheng/git_learning.git (fetch)
github git@github.com:yanghuisheng/git_learning.git (push)
zhineng file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git (fetch)
zhineng file:///Users/yanghuisheng/Desktop/study/git/666-backup/zhineng.git (push)
# 向github推送所有diamante
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github --all
Counting objects: 20, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (20/20), 18.31 KiB | 9.16 MiB/s, done.
Total 20 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
# temp分支和young分支都推送成功了,master分支没有推送成功,因为远端的master下面有一个创建项目的时候生成的LICENSE文件
# 如果我们没有创建master分支,github上模式只有一个main分支,则可以推送成功
# 也就是当远端分支上有我们本地没有的commit时,这个分支就是push失败,需要先把远端的变更拉下来
To github.com:yanghuisheng/git_learning.git
* [new branch] temp -> temp
* [new branch] young -> young
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 查看git log,有两棵树
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (zhineng/master, github/master, master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (HEAD -> young, zhineng/young, zhineng/temp, github/young, github/temp, temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
# fetch github的master分支,fetch只拉取,不merge
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git fetch github master
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yanghuisheng/git_learning
* branch master -> FETCH_HEAD
+ fe75c75...65f751d master -> github/master (forced update)
# 此时再查看git log 发现多了一个独立树
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit 65f751dce60969820e7391677335f53f664a2648 (github/master)
Author: yanghuisheng <34906359+yanghuisheng@users.noreply.github.com>
Date: Thu May 26 12:01:59 2022 +0800
Initial commit
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (zhineng/master, master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (HEAD -> young, zhineng/young, zhineng/temp, github/young, github/temp, temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
#查看所有分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -av
master fe75c75 change title
temp 86cfc3e add index and image
* young 86cfc3e add index and image
remotes/github/master 65f751d Initial commit
remotes/github/temp 86cfc3e add index and image
remotes/github/young 86cfc3e add index and image
remotes/zhineng/master fe75c75 change title
remotes/zhineng/temp 86cfc3e add index and image
remotes/zhineng/young 86cfc3e add index and image
# 切换到master分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git checkout master
Switched to branch 'master'
# 执行合并,但是报错,因为这两个分支目前是独立状态,没有父子关系
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git merge github/master
fatal: refusing to merge unrelated histories
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git merge -h
usage: git merge [<options>] [<commit>...]
or: git merge --abort
or: git merge --continue
-n do not show a diffstat at the end of the merge
--stat show a diffstat at the end of the merge
--summary (synonym to --stat)
--log[=<n>] add (at most <n>) entries from shortlog to merge commit message
--squash create a single commit instead of doing a merge
--commit perform a commit if the merge succeeds (default)
-e, --edit edit message before committing
--ff allow fast-forward (default)
--ff-only abort if fast-forward is not possible
--rerere-autoupdate update the index with reused conflict resolution if possible
--verify-signatures verify that the named commit has a valid GPG signature
-s, --strategy <strategy>
merge strategy to use
-X, --strategy-option <option=value>
option for selected merge strategy
-m, --message <message>
merge commit message (for a non-fast-forward merge)
-v, --verbose be more verbose
-q, --quiet be more quiet
--abort abort the current in-progress merge
--continue continue the current in-progress merge
# 允许历史上不相干的进行merge
--allow-unrelated-histories
allow merging unrelated histories
--progress force progress reporting
-S, --gpg-sign[=<key-id>]
GPG sign commit
--overwrite-ignore update ignored files (default)
--signoff add Signed-off-by:
--verify verify commit-msg hook
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git merge --allow-unrelated-histories github/master
# 出现窗口,让你描述为什么要做这次merge
Merge remote-tracking branch 'github/master'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
# 保存并提交,出现以下信息,merger采用了recursive的策略,有一个文件变化
Merge made by the 'recursive' strategy.
LICENSE | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100644 LICENSE
# 查看git log,可以看到github/master已经合并到了master上了,HEAD执行本地的master分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit 2263a99acffcc32c97b6d0b240dbe20f8784b525 (HEAD -> master)
|\ Merge: fe75c75 65f751d
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 12:22:46 2022 +0800
| |
| | Merge remote-tracking branch 'github/master'
| |
| * commit 65f751dce60969820e7391677335f53f664a2648 (github/master)
| Author: yanghuisheng <34906359+yanghuisheng@users.noreply.github.com>
| Date: Thu May 26 12:01:59 2022 +0800
|
| Initial commit
|
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (zhineng/master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (zhineng/young, zhineng/temp, github/young, github/temp, young, temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
此时的github上,可以看到,有两个分支已经推送上来了,并且master分支只有一个commit,就是LICENSE文件
此时在去推送master到github上
# push master
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github master
Counting objects: 18, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (15/15), done.
Writing objects: 100% (18/18), 18.13 KiB | 18.13 MiB/s, done.
Total 18 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To github.com:yanghuisheng/git_learning.git
65f751d..2263a99 master -> master
# 查看日志,可以看到HEAD指向了 master和github/master
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit 2263a99acffcc32c97b6d0b240dbe20f8784b525 (HEAD -> master, github/master)
|\ Merge: fe75c75 65f751d
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 12:22:46 2022 +0800
| |
| | Merge remote-tracking branch 'github/master'
| |
| * commit 65f751dce60969820e7391677335f53f664a2648
| Author: yanghuisheng <34906359+yanghuisheng@users.noreply.github.com>
| Date: Thu May 26 12:01:59 2022 +0800
|
| Initial commit
|
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (zhineng/master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (zhineng/young, zhineng/temp, github/young, github/temp, young, temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
刷新github页面,可以看到master分支下已经有了文件,并且commit变成了5
冲突解决
不同的人修改不同的文件
在github上,基于master创建分支feature/add-git-command,在本地git_learning项目的父目录,从github上clone一个项目到本地
# 默认会创建与项目相同的文件夹,但是如果改路径下已经有了一个同名的文件夹,文件夹不为空,就会报错,所以可以指定拉取的项目要放在哪个文件夹下
yanghuisheng@yanghuishengdeMacBook-Pro git % git clone git@github.com:yanghuisheng/git_learning.git git_learning_02
Cloning into 'git_learning_02'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 25 (delta 3), reused 22 (delta 3), pack-reused 0
Receiving objects: 100% (25/25), 19.74 KiB | 255.00 KiB/s, done.
Resolving deltas: 100% (3/3), done.
yanghuisheng@yanghuishengdeMacBook-Pro git % ls
666-backup git_learning git_learning_02 unforGITtable-master watch_objects
yanghuisheng@yanghuishengdeMacBook-Pro git % cd git_learning_02
# 添加local配置,模拟第二个人
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git config --add --local user.name yhs
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git config --add --local user.email 346111869@qq.com
# 查看local配置
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git config --local --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=git@github.com:yanghuisheng/git_learning.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
branch.feature/add_git_commands.remote=origin
branch.feature/add_git_commands.merge=refs/heads/feature/add_git_commands
user.name=yhs
user.email=346111869@qq.com
# 查看分支信息
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -av
* main 65f751d Initial commit
remotes/origin/HEAD -> origin/main
remotes/origin/feature/add_git_commands 2263a99 Merge remote-tracking branch 'github/master'
remotes/origin/main 65f751d Initial commit
remotes/origin/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/origin/temp 86cfc3e add index and image
remotes/origin/young 86cfc3e add index and image
# 基于远端分支创建本地分支,git会将本地分支与远端分支关联
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % 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'
# 查看本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -v
* feature/add_git_commands 2263a99 Merge remote-tracking branch 'github/master'
main 65f751d Initial commit
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % ls
LICENSE README images index.html js styles
# 修改README文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % vim README
# 将改动文件添加到暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git add -u
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git status
On branch feature/add_git_commands
Your branch is up to date with 'origin/feature/add_git_commands'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README
# 提交暂存区
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git commit -m'Add git commands description in readme'
[feature/add_git_commands 1139ad4] Add git commands description in readme
1 file changed, 2 insertions(+)
# 检出的时候git将本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
Counting objects: 3, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 343 bytes | 343.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
2263a99..1139ad4 feature/add_git_commands -> feature/add_git_commands
进入git_learning项目
# fetch 远程目录
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yanghuisheng/git_learning
* [new branch] feature/add_git_commands -> github/feature/add_git_commands
* [new branch] main -> github/main
# 查看本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -v
* master 2263a99 Merge remote-tracking branch 'github/master'
temp 86cfc3e add index and image
young 86cfc3e add index and image
# 查看所有分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -av
* master 2263a99 Merge remote-tracking branch 'github/master'
temp 86cfc3e add index and image
young 86cfc3e add index and image
remotes/github/feature/add_git_commands 1139ad4 Add git commands description in readme
remotes/github/main 65f751d Initial commit
remotes/github/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/github/temp 86cfc3e add index and image
remotes/github/young 86cfc3e add index and image
remotes/zhineng/master fe75c75 change title
remotes/zhineng/temp 86cfc3e add index and image
remotes/zhineng/young 86cfc3e add index and image
# 基于远端分支创建本地分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % 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'
# 查看所有分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -av
* feature/add_git_commands 1139ad4 Add git commands description in readme
master 2263a99 Merge remote-tracking branch 'github/master'
temp 86cfc3e add index and image
young 86cfc3e add index and image
remotes/github/feature/add_git_commands 1139ad4 Add git commands description in readme
remotes/github/main 65f751d Initial commit
remotes/github/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/github/temp 86cfc3e add index and image
remotes/github/young 86cfc3e add index and image
remotes/zhineng/master fe75c75 change title
remotes/zhineng/temp 86cfc3e add index and image
remotes/zhineng/young 86cfc3e add index and image
# 修改index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git add -u
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git commit -m'change index.html file'
[feature/add_git_commands 235835a] change index.html file
1 file changed, 1 insertion(+), 1 deletion(-)
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit 235835aa6bcdfbb456f29102171c0d1decce6539 (HEAD -> feature/add_git_commands)
| Author: young <62yanghuisheng@163.com>
| Date: Thu May 26 16:47:20 2022 +0800
|
| change index.html file
|
* commit 1139ad4150551edac94cde24943270a78594eca1 (github/feature/add_git_commands)
| Author: yhs <346111869@qq.com>
| Date: Thu May 26 14:00:29 2022 +0800
|
| Add git commands description in readme
|
* commit 2263a99acffcc32c97b6d0b240dbe20f8784b525 (github/master, master)
|\ Merge: fe75c75 65f751d
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 12:22:46 2022 +0800
| |
| | Merge remote-tracking branch 'github/master'
| |
| * commit 65f751dce60969820e7391677335f53f664a2648 (github/main)
| Author: yanghuisheng <34906359+yanghuisheng@users.noreply.github.com>
| Date: Thu May 26 12:01:59 2022 +0800
|
| Initial commit
|
* commit fe75c754f9b03ba4ece7a412b8ba72b53353d103 (zhineng/master)
| Author: young <62yanghuisheng@163.com>
| Date: Wed May 25 16:30:36 2022 +0800
|
| change title
|
* commit c9e345e1a682cf73e9af9a55938e78b369e6662b
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| Create a complete web page
|
| add index and image
|
| add css and js
|
| modify one css and one js
|
* commit e81779d263720bb46158861bb17c61a1cb53d0ab
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
add ReadMe
Add readme
rename README.md to README
* commit 86cfc3efebca5381c2944ed7c97c2d98e67293c0 (zhineng/young, zhineng/temp, github/young, github/temp, young, temp)
| Author: yanghuisheng <62yanghuisheng@163.com>
| Date: Mon May 23 09:48:02 2022 +0800
|
| add index and image
|
* commit 88cb2de6e12e2ca0b2584d23446518fb0c15d661
Author: yanghuisheng <62yanghuisheng@163.com>
Date: Mon May 23 09:35:19 2022 +0800
Add readme
进入git_learning_02项目
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -v
* feature/add_git_commands 1139ad4 Add git commands description in readme
main 65f751d Initial commit
# 修改READ
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % vim README
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git commit -am'add a line'
[feature/add_git_commands 9abae9b] add a line
1 file changed, 2 insertions(+)
# 推送到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
Counting objects: 3, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 286 bytes | 286.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
1139ad4..9abae9b feature/add_git_commands -> feature/add_git_commands
进入git_learning项目
# 查看当前分支
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -v
* feature/add_git_commands 235835a [ahead 1] change index.html file
master 2263a99 Merge remote-tracking branch 'github/master'
temp 86cfc3e add index and image
young 86cfc3e add index and image
# 推送之前的提交,出现错误,远端和我们本地之间不是fast-forwards
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 将远端的数据fetch下来
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git fetch github
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yanghuisheng/git_learning
1139ad4..9abae9b feature/add_git_commands -> github/feature/add_git_commands
# 查看分支,feature/add_git_commands 的状况是[ahead 1, behind 1],本地有1个远端没有的commit,远端有一个本地没有的commit
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git branch -av
* feature/add_git_commands 235835a [ahead 1, behind 1] change index.html file
master 2263a99 Merge remote-tracking branch 'github/master'
temp 86cfc3e add index and image
young 86cfc3e add index and image
remotes/github/feature/add_git_commands 9abae9b add a line
remotes/github/main 65f751d Initial commit
remotes/github/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/github/temp 86cfc3e add index and image
remotes/github/young 86cfc3e add index and image
remotes/zhineng/master fe75c75 change title
remotes/zhineng/temp 86cfc3e add index and image
remotes/zhineng/young 86cfc3e add index and image
# 和远端分支合并,一个提交是修改index.html,一个提交是修改README文件,不会出现冲突
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git merge github/feature/add_git_commands
Merge made by the 'recursive' strategy.
README | 2 ++
1 file changed, 2 insertions(+)
# 推送到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
Counting objects: 5, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 579 bytes | 579.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To github.com:yanghuisheng/git_learning.git
9abae9b..b7853bd feature/add_git_commands -> feature/add_git_commands
不同的人修改了相同文件的不同区域
同步git_learning_02
# 同步远端文件到本地
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git pull
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 3), reused 5 (delta 3), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:yanghuisheng/git_learning
9abae9b..b7853bd feature/add_git_commands -> origin/feature/add_git_commands
Updating 9abae9b..b7853bd
Fast-forward
index.html | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
修改git_learning_02和git_learning的index.html,但是区域不同
比如git_learning_02中,我在header标签下加了一行注释,在git_learning中,在body标签下加了一行注释。
将git_learning的中改动提交并push到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
Counting objects: 3, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
b7853bd..8e13f84 feature/add_git_commands -> feature/add_git_commands
将git_learning_02中的改动提交并push到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yanghuisheng/git_learning
b7853bd..8e13f84 feature/add_git_commands -> origin/feature/add_git_commands
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -av
* feature/add_git_commands b90cd87 [ahead 1, behind 1] modify first space
main 65f751d Initial commit
remotes/origin/HEAD -> origin/main
remotes/origin/feature/add_git_commands 8e13f84 modify second space
remotes/origin/main 65f751d Initial commit
remotes/origin/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/origin/temp 86cfc3e add index and image
remotes/origin/young 86cfc3e add index and image
# 执行merge,如果弹出了merge message的框,说明git已经帮我们处理好了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git merge origin/feature/add_git_commands
Auto-merging index.html
Merge made by the 'recursive' strategy.
index.html | 1 +
1 file changed, 1 insertion(+)
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git log --all --graph
* commit 0f2a8ea6d3a7e067aad0e4d89e9e78e322cff478 (HEAD -> feature/add_git_commands)
|\ Merge: b90cd87 8e13f84
| | Author: yhs <346111869@qq.com>
| | Date: Thu May 26 20:47:55 2022 +0800
| |
| | Merge remote-tracking branch 'origin/feature/add_git_commands' into feature/add_git_commands
| |
| * commit 8e13f8450fb4e4dfba434c78cd75182d290b2e76 (origin/feature/add_git_commands)
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 20:41:41 2022 +0800
| |
| | modify second space
| |
* | commit b90cd876fcf4351d4db633b9ae651aefca7fed31
|/ Author: yhs <346111869@qq.com>
| Date: Thu May 26 20:40:38 2022 +0800
|
| modify first space
|
* commit b7853bd334757cada3eac70260646b7d21ccf8e2
|\ Merge: 235835a 9abae9b
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 20:28:21 2022 +0800
| |
| | Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
| |
| * commit 9abae9b2b096551e8473c3dc4c19d7043275c7df
| | Author: yhs <346111869@qq.com>
| | Date: Thu May 26 16:55:04 2022 +0800
| |
| | add a line
| |
* | commit 235835aa6bcdfbb456f29102171c0d1decce6539
|/ Author: young <62yanghuisheng@163.com>
| Date: Thu May 26 16:47:20 2022 +0800
|
| change index.html file
|
* commit 1139ad4150551edac94cde24943270a78594eca1
| Author: yhs <346111869@qq.com>
| Date: Thu May 26 14:00:29 2022 +0800
|
| Add git commands description in readme
|
* commit 2263a99acffcc32c97b6d0b240dbe20f8784b525 (origin/master)
|\ Merge: fe75c75 65f751d
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 12:22:46 2022 +0800
| |
| | Merge remote-tracking branch 'github/master'
| |
| * commit 65f751dce60969820e7391677335f53f664a2648 (origin/main, origin/HEAD, main)
| Author: yanghuisheng <34906359+yanghuisheng@users.noreply.github.com>
| Date: Thu May 26 12:01:59 2022 +0800
|
| Initial commit
|
# merge完成之后,分支状况编程了 ahead 2
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -av
* feature/add_git_commands 0f2a8ea [ahead 2] Merge remote-tracking branch 'origin/feature/add_git_commands' into feature/add_git_commands
main 65f751d Initial commit
remotes/origin/HEAD -> origin/main
remotes/origin/feature/add_git_commands 8e13f84 modify second space
remotes/origin/main 65f751d Initial commit
remotes/origin/master 2263a99 Merge remote-tracking branch 'github/master'
remotes/origin/temp 86cfc3e add index and image
remotes/origin/young 86cfc3e add index and image
# 推送到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
Counting objects: 6, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 612 bytes | 612.00 KiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
8e13f84..0f2a8ea feature/add_git_commands -> feature/add_git_commands
不同的人修改了相同文件的同一区域
保持git_learning和git_learing_02与远端提交记录同步
修改git_learing_02的index.html,并提交push到github
修改git_learing的index.html,修改的文件位置与git_learing_02的位置一致(所在行数一直),并提交push到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 拉取远端文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:yanghuisheng/git_learning
0f2a8ea..24f5f9c feature/add_git_commands -> github/feature/add_git_commands
# 自动merge index.html文件
Auto-merging index.html
# 内容冲突
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
查看index.html文件
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % vim index.html
<!DOCTYPE html>
<html>
# HDEA的修改
<<<<<<< HEAD
<!-- modify first space??? -->
<!-- ok -->
=======
# 远端的修改
<!-- modify first space!!! -->
<!-- empty space -->
>>>>>>> 24f5f9c603b235174399ddff148237f81d433229
<head>
<title>A Git Demo from Github!!!</title>
处理冲突的区域,处理完之后,将git生成的冲突信息删除,比如我们希望所有的数据都被保留,并且修改一下顺序,修改之后为
<!DOCTYPE html>
<html>
<!-- modify first space??? -->
<!-- modify first space!!! -->
<!-- ok -->
<!-- empty space -->
<head>
<title>A Git Demo from Github!!!</title>
保存修改的内容
# 查看git状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
# 当前所在分支
On branch feature/add_git_commands
# 本地分支和github/feature/add_git_commands分支有不一样的地方
Your branch and 'github/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)
# 存在没有merge的路径,需要修改这些不一样的地方,并且执行git commit将它提交
# 如果不想提交,可以使用git merge --abort ,就恢复merge之前的状态
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
# 提交合并
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git commit -am'merger index.html'
[feature/add_git_commands 71b19dc] merger index.html
# 查看git状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git status
On branch feature/add_git_commands
Your branch is ahead of 'github/feature/add_git_commands' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
# 查看git log
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git log --all --graph
* commit 71b19dc96e0937d7fb8b215f50e1e2e6e73ec442 (HEAD -> feature/add_git_commands, github/feature/add_git_commands)
|\ Merge: 9e35a8a 24f5f9c
| | Author: young <62yanghuisheng@163.com>
| | Date: Fri May 27 00:25:38 2022 +0800
| |
| | merger index.html
| |
| * commit 24f5f9c603b235174399ddff148237f81d433229
| | Author: yhs <346111869@qq.com>
| | Date: Thu May 26 22:43:20 2022 +0800
| |
| | add empty space
| |
* | commit 9e35a8aafebae0d04742e92294ffc213a14550e7
|/ Author: young <62yanghuisheng@163.com>
| Date: Thu May 26 22:44:46 2022 +0800
|
| addok
|
* commit 0f2a8ea6d3a7e067aad0e4d89e9e78e322cff478
|\ Merge: b90cd87 8e13f84
| | Author: yhs <346111869@qq.com>
| | Date: Thu May 26 20:47:55 2022 +0800
| |
| | Merge remote-tracking branch 'origin/feature/add_git_commands' into feature/add_git_commands
| |
| * commit 8e13f8450fb4e4dfba434c78cd75182d290b2e76
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 20:41:41 2022 +0800
| |
| | modify second space
| |
* | commit b90cd876fcf4351d4db633b9ae651aefca7fed31
|/ Author: yhs <346111869@qq.com>
| Date: Thu May 26 20:40:38 2022 +0800
|
| modify first space
|
* commit b7853bd334757cada3eac70260646b7d21ccf8e2
|\ Merge: 235835a 9abae9b
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 20:28:21 2022 +0800
| |
| | Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
| |
| * commit 9abae9b2b096551e8473c3dc4c19d7043275c7df
| | Author: yhs <346111869@qq.com>
| | Date: Thu May 26 16:55:04 2022 +0800
| |
| | add a line
| |
* | commit 235835aa6bcdfbb456f29102171c0d1decce6539
|/ Author: young <62yanghuisheng@163.com>
| Date: Thu May 26 16:47:20 2022 +0800
|
| change index.html file
|
* commit 1139ad4150551edac94cde24943270a78594eca1
| Author: yhs <346111869@qq.com>
| Date: Thu May 26 14:00:29 2022 +0800
|
| Add git commands description in readme
|
* commit 2263a99acffcc32c97b6d0b240dbe20f8784b525 (github/master, master)
|\ Merge: fe75c75 65f751d
| | Author: young <62yanghuisheng@163.com>
| | Date: Thu May 26 12:22:46 2022 +0800
| |
# push到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
Counting objects: 6, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 596 bytes | 596.00 KiB/s, done.
Total 6 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
24f5f9c..71b19dc feature/add_git_commands -> feature/add_git_commands
同时变更了文件名和文件内容
保持git_learning和git_learning_02与远端同步
在git_learning
# 将index.html改名为index.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git mv index.html index.htm
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git commit -am'mv index.html to index.htm'
[feature/add_git_commands 239d816] mv index.html to index.htm
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.html => index.htm (100%)
# push到远端
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
Counting objects: 2, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 228 bytes | 228.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yanghuisheng/git_learning.git
71b19dc..239d816 feature/add_git_commands -> feature/add_git_commands
在git_learning_02
# 修改index.html
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % vim index.html
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git commit -am'change first space'
[feature/add_git_commands 7da1121] change first space
1 file changed, 1 insertion(+), 1 deletion(-)
# push到远端
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 执行git pull,弹出merge message窗口,修改message,保存并退出
yanghuisheng@yanghuishengdeMacBook-Pro 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), done.
From github.com:yanghuisheng/git_learning
71b19dc..239d816 feature/add_git_commands -> origin/feature/add_git_commands
Merge made by the 'recursive' strategy.
index.html => index.htm | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.html => index.htm (100%)
# 查看本地文件,文件名已经被修改了,查看内容,发现我们修改的内容也被保存了
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % ls
LICENSE README images index.htm js styles
# 再次推送到远端
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
Counting objects: 5, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 572 bytes | 572.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), completed with 2 local objects.
To github.com:yanghuisheng/git_learning.git
239d816..e2f75e2 feature/add_git_commands -> feature/add_git_commands
把同一文件改成了不同的名字
保持git_learning和git_learning_02与远端同步
git_learning
# 将 index.htm改名为index2.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git mv index.htm index2.htm
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git commit -am'mv index.htm to index2.htm'
[feature/add_git_commands a3a7477] mv index.htm to index2.htm
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.htm => index2.htm (100%)
# push到远端
yanghuisheng@yanghuishengdeMacBook-Pro git_learning % git push github
Counting objects: 2, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 231 bytes | 231.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yanghuisheng/git_learning.git
e2f75e2..a3a7477 feature/add_git_commands -> feature/add_git_commands
git_learning_02
# index.htm改名为index1.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git mv index.htm index1.htm
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git commit -am'mv index.htm to index1.htm'
[feature/add_git_commands 56ab7f2] mv index.htm to index1.htm
1 file changed, 0 insertions(+), 0 deletions(-)
rename index.htm => index1.htm (100%)
# 推送到github
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (fetch first)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 拉取远端文件
yanghuisheng@yanghuishengdeMacBook-Pro 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), done.
From github.com:yanghuisheng/git_learning
e2f75e2..a3a7477 feature/add_git_commands -> origin/feature/add_git_commands
# 提示冲突
CONFLICT (rename/rename): Rename "index.htm"->"index1.htm" in branch "HEAD" rename "index.htm"->"index2.htm" in "a3a7477c7b45b1c03be3b11c22a9866379531df5"
Automatic merge failed; fix conflicts and then commit the result.
# 查看文件差异
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % diff index1.htm index2.htm
# 查看状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % 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)
# 都删除了 index.htm
both deleted: index.htm
# 我们增加了 index1.htm
added by us: index1.htm
# 其他人增加了 index2.htm
added by them: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
# 比如我们决定保留文件名为index1.htm
# 移除 index.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git rm index.htm
index.htm: needs merge
rm 'index.htm'
# 查看状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % 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 <file>..." to mark resolution)
added by us: index1.htm
added by them: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
# 保留的index1.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git add index1.htm
# 查看状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % 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 <file>..." to mark resolution)
added by them: index2.htm
no changes added to commit (use "git add" and/or "git commit -a")
# 移除 index2.htm
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git rm index2.htm
index2.htm: needs merge
rm 'index2.htm'
# 查看状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % 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)
# 所有冲突都被解决了,但是现在还是merge状态
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
# 提交
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git commit -am'decide to mv index to index1'
[feature/add_git_commands 7acc8d5] decide to mv index to index1
# 查看状态
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git status
On branch feature/add_git_commands
Your branch is ahead of 'origin/feature/add_git_commands' by 2 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git branch -v
# 比远端多了2个commit
* feature/add_git_commands 7acc8d5 [ahead 2] decide to mv index to index1
main 65f751d Initial commit
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git log --all --graph
* commit 7acc8d51cc043be9b7de8cdc00232055c774fcde (HEAD -> feature/add_git_commands)
|\ Merge: 56ab7f2 a3a7477
| | Author: yhs <346111869@qq.com>
| | Date: Fri May 27 08:50:47 2022 +0800
| |
| | decide to mv index to index1
| |
| * commit a3a7477c7b45b1c03be3b11c22a9866379531df5 (origin/feature/add_git_commands)
| | Author: young <62yanghuisheng@163.com>
| | Date: Fri May 27 08:34:17 2022 +0800
| |
| | mv index.htm to index2.htm
| |
* | commit 56ab7f22f8f2fb2646bd44700fffb47c9769b3ed
|/ Author: yhs <346111869@qq.com>
| Date: Fri May 27 08:33:18 2022 +0800
|
| mv index.htm to index1.htm
|
* commit e2f75e2d21a1acd5447fd4a3d7416b3c8c0f2060
|\ Merge: 7da1121 239d816
| | Author: yhs <346111869@qq.com>
| | Date: Fri May 27 00:36:55 2022 +0800
| |
| | Merge branch 'feature/add_git_commands' of github.com:yanghuisheng/git_learning into feature/add_git_commands
| |
# push
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
Counting objects: 3, done.
Delta compression using up to 10 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 408 bytes | 408.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:yanghuisheng/git_learning.git
a3a7477..7acc8d5 feature/add_git_commands -> feature/add_git_commands
禁止操作
禁止向集成分支执行push -f
现在远端有21个提交
# 查看本地提交记录
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git log --oneline
7acc8d5 (HEAD -> feature/add_git_commands, origin/feature/add_git_commands) decide to mv index to index1
a3a7477 mv index.htm to index2.htm
56ab7f2 mv index.htm to index1.htm
e2f75e2 Merge branch 'feature/add_git_commands' of github.com:yanghuisheng/git_learning into feature/add_git_commands
7da1121 change first space
239d816 mv index.html to index.htm
71b19dc merger index.html
9e35a8a addok
24f5f9c add empty space
0f2a8ea Merge remote-tracking branch 'origin/feature/add_git_commands' into feature/add_git_commands
8e13f84 modify second space
b90cd87 modify first space
b7853bd Merge remote-tracking branch 'github/feature/add_git_commands' into feature/add_git_commands
9abae9b add a line
235835a change index.html file
1139ad4 Add git commands description in readme
2263a99 (origin/master) Merge remote-tracking branch 'github/master'
65f751d (origin/main, origin/HEAD, main) Initial commit
fe75c75 change title
c9e345e Create a complete web page
e81779d add ReadMe
# 将指针指向fe75c75
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git reset --hard fe75c75
HEAD is now at fe75c75 change title
# 查看日志
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git log --oneline
fe75c75 (HEAD -> feature/add_git_commands) change title
c9e345e Create a complete web page
e81779d add ReadMe
# 此时直接push是会被拒绝的
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push
To github.com:yanghuisheng/git_learning.git
! [rejected] feature/add_git_commands -> feature/add_git_commands (non-fast-forward)
error: failed to push some refs to 'git@github.com:yanghuisheng/git_learning.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
# 但是如果使用push -f 就可以直接推送给远端
yanghuisheng@yanghuishengdeMacBook-Pro git_learning_02 % git push -f
Total 0 (delta 0), reused 0 (delta 0)
To github.com:yanghuisheng/git_learning.git
+ 7acc8d5...fe75c75 feature/add_git_commands -> feature/add_git_commands (forced update)
此时可以看到远端的commit仅为3,后续的所有提交记录都没有了
所以在公共分支上,应该禁止使用push -f 命令,防止远端的commit丢失
禁止在集成分支上执行变更历史操作
如果对公共分支进行rebase操作,会改变公共分支上的commit指向,这时,由于其他人员本地的commit与远端的commit无法对应,会导致push失败,所以应该禁止在公共分支上进行rebase操作