通过git init
命令把这个目录变成Git可以管理的仓库:
用来跟踪新文件并提交到暂存区或者将已跟踪的文件提交到暂存区
git commit -m "本次提交注释内容"
git add .
git commit -m "本次提交注释内容"
合并成
git commit -am "本次提交注释内容"
下面是简化美化版
git log --oneline
e69864c (HEAD) understand how stage works
4c7e16c append GPL
e87eae0 add distributed
bcca25c wrote a readme file
可以省略使用git add命令将已跟踪文件放到暂存区的功能,删除的提交不了
$ git status
HEAD detached from bcca25c
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
Untracked files:
(use "git add ..." to include in what will be committed)
LICENSE.txt
no changes added to commit (use "git add" and/or "git commit -a")
Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked。
现在,使用两次命令git add,把readme.txt和LICENSE都添加后,用git status再查看一下:
$ git status
HEAD detached from bcca25c
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: LICENSE.txt
modified: readme.txt
现在,暂存区的状态就变成这样了:
所以,git add命令实际上就是把要提交的所有修改放到暂存区(Stage),然后,执行git commit就可以一次性把暂存区的所有修改提交到分支。
git remote add origin [email protected]:Aerozb/gitStudy.git
git远程仓库分支的各命令的具体解析(git remote add)
在这张图面里git init, git add 和git commit 都是前期的准备, 相当于将你本地的文件都上传到了本地仓库,但是还没有像远端仓库提交;
那么add什么呢? 蓝色的方框其实就是你为远端仓库所起的名字,一般都是叫origin,其实你也可以要Ceres 或者Earth,绿色方框就是你的远端仓库的真实地址;
如git push -u origin master
git push 的 -u 参数含义
第一次需要使用 -u 将远程仓库地址设置为上游,之后push pull,都可以不用加参数,直接git push
之类的即可
如果不加-u,之后每次命令都要加上远程分支名 本地分支名
git远程仓库分支的各命令的具体解析(git remote add),这篇文章详细解释了git push,简单易懂
我们创建dev分支,然后切换到dev分支,然后,用git branch命令查看当前分支:
$ git checkout -b dev
Switched to a new branch 'dev'
$ git branch
* dev
master
git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
$ git branch dev
$ git checkout dev
Switched to branch 'dev'
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下后保存:
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
<<<<<<< HEAD
Creating a new branch is quick & simple.
=======
Creating a new branch is quick AND simple.
>>>>>>> feature1
删除只保留新的
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Git tracks changes of files.
Creating a new branch is quick AND simple.
再次提交即可
$ git add readme.txt
$ git commit -m "conflict fixed"
现在主分支和dev分支都是一样的内容了
$ git log --graph --pretty=oneline --abbrev-commit
* 5d0ad8c (HEAD -> master) conflict fixed
|\
| * bff8feb (dev) branch test
| * e69864c understand how stage works
| * 4c7e16c append GPL
| * e87eae0 add distributed
* | e8fbed9 (origin/master) append GPL
|/
* bcca25c wrote a readme file
git switch 分支名
-c©:create
git switch -c 分支名
:已存在,则不会创建和切换
git switch -C 分支名
:切换到新分支,已存在也会切换,并且把要切换的分支指针指向最新的节点
1.原本在master分支,内容为
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Creating a new branch is quick.
2.现在切换到feature1分支
git switch -c feature1
3.现在末尾加一行
feature1-1
4.添加并提交
$ git commit -am "在fe分支第一次提交"
[feature1 cfa9c06] 在fe分支第一次提交
1 file changed, 2 insertions(+), 1 deletion(-)
现在fe分支是领先的
5.想要切换到master,发现已存在,切换失败
$ git switch -c master
fatal: A branch named 'master' already exists.
6.改用-C,强制切换,并把head指向最新节点
$ git switch -C master
Switched to and reset branch 'master'
Your branch is ahead of 'origin/master' by 6 commits.
(use "git push" to publish your local commits)
7.再次查看文本内容,发现跟fe分支一模一样
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.
Creating a new branch is quick.
feature1-1
8.查看当前状态和提交历史,发现确实切换到master主分支,且提交历史也是fe那次的提交,head同时指向master, feature1
$ git status
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git log
commit cfa9c06472bb98e8c7e8eaa270e15a59c5ed6e01 (HEAD -> master, feature1)
Author: Aerozb <847888663@qq.com>
Date: Fri Feb 11 10:24:55 2022 +0800
在fe分支第一次提交
commit 5d0ad8ccd397b5869ab88c9b14dc5caab057b98a
Merge: e8fbed9 bff8feb
Author: Aerozb <847888663@qq.com>
Date: Thu Feb 10 17:30:32 2022 +0800
conflict fixed
commit bff8feb4f4df210fd63fa0bc9b1d4bd5e077e48e (dev)
Author: Aerozb <847888663@qq.com>
Date: Thu Feb 10 17:27:35 2022 +0800
branch test
......
Git 合并时 --no-ff 的作用,简单易懂看这篇
Git 的 Fast Forward 和 no fast foward 合并模式对比
使用--no-ff
简单来说就是产生一个新的提交线,开启另一条线,否则就是顺着开启分支的那条路走,不会有两条不同的路
Git 合并两个分支时,如果顺着一个分支走下去可以到达另一个分支的话,那么 Git 在合并两者时,只会简单地把指针右移,叫做“快进”(fast-forward),比如下图:
A---B---C feature
/
D---E---F master
要把 feature 合并到 master 中,执行以下命令
$ git checkout master
$ git merge feature
结果就会变成
A---B---C feature
/ master
D---E---F
因为 feature 就在 master 的下游,所以直接移动了 master 的指针,master 和 feature 都指向了 C。而如果执行了 git merge --no-ff feature 的话,是下面的结果:
A---B---C feature
/ \
D---E---F-----------G master
由于 --no-ff 禁止了快进,所以会生成一个新的提交,master 指向 G。
1.切换到dev分支
git switch dev
2.在文本内容末添加一行
Switched to a new branch 'dev'
3.添加到暂存区并提交到本地master分支
git commit -am "切换到dev,添加一行内容"
4.再次添加第二次内容,然后添加提交
Switched to a new branch 'dev'-2
$ git commit -am "第二次添加一行内容"
5.查看历史记录
$ git log --oneline
253c00a (HEAD -> dev) 第二次添加一行内容
190353e 切换到dev,添加一行内容
bff8feb branch test
e69864c understand how stage works
4c7e16c append GPL
e87eae0 add distributed
bcca25c wrote a readme file
6.切换到主分支
$ git switch master
7.开始合并,非快进方式(Fast forward)
$ git merge --no-ff -m "merge with no-ff" dev
最后分支图效果类似下面左图
1.在dev新建一个bug.java文件,提交git add bug.java
$ git status
On branch dev
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: bug.java
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
2.这时候有bug要修复,我这bug.java文件才进行到一半,无法提交,此时用git stash
,来隐藏此次的内容
$ git stash
Saved working directory and index state WIP on dev: 253c00a 第二次添加一行内容
3.查看状态,发现干干净净,bug.java不见了
$ git status
On branch dev
nothing to commit, working tree clean
4.因此可以放心地创建分支来修复bug,首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支。
$ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git switch -c fixBranch
Switched to a new branch 'fixBranch'
5.现在在fixBranch分支上修复bug,需要把readme.txt文件内容“Git is free software …”改为“Git is a free software …”,然后提交:
$ git add readme.txt
$ git commit -m "fix bug"
[fixBranch 12be99c] fix bug
1 file changed, 1 insertion(+), 1 deletion(-)
6.修复完成后,切换到master分支,并完成合并,最后删除fixBranch分支:
$ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
$ git merge --no-ff -m "merged bug fix 101" fixBranch
Merge made by the 'recursive' strategy.
readme.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
7.修复完,接着回到dev干活,可以看到还是干净的状态,
用git stash list
查看之前隐藏的内容,然后用git stash pop
恢复,恢复的同时把stash内容也删了,
再用git stash list
查看,就看不到任何stash内容了:
$ git switch dev
Switched to branch 'dev'
$ git status
On branch dev
nothing to commit, working tree clean
$ git stash list
stash@{0}: WIP on dev: 253c00a 第二次添加一行内容
$ git stash pop
On branch dev
Changes to be committed:
(use "git restore --staged ..." to unstage)
new file: bug.java
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: readme.txt
Dropped refs/stash@{0} (89d6067f7f595af44681eafd4192ad407c219839)
$ git stash list
8.现在dev分支也要修复这个bug,不可能在执行一边之前的操作,为了方便操作,Git专门提供了一个cherry-pick
命令,让我们能复制一个特定的提交到当前分支
$ git cherry-pick 12be99c
error: your local changes would be overwritten by cherry-pick.
hint: commit your changes or stash them to proceed.
fatal: cherry-pick failed
发现失败,原来dev分支的内容还没提交,需要先提交才能复制过来
$ git commit -am "临时提交"
$ git status
On branch dev
nothing to commit, working tree clean
再次执行发现成功
$ git cherry-pick 12be99c
Auto-merging readme.txt
[dev 84df3d8] fix bug
Date: Mon Feb 14 16:03:49 2022 +0800
1 file changed, 1 insertion(+), 1 deletion(-)
9.所以这个bug可以等dev开发完成提交一次,在复制过来,如果怕到时候记不住直接复制过来也行
10.最后记得删除修复bug分支
$ git branch -D fixBranch
Deleted branch fixBranch (was 12be99c).
$ git branch
* dev
feature1
master