git入门笔记

TIPS:

仅作个人学习用途,文章逻辑很乱

Git分支:

Git仓库由分支组成,每个commit版本构成分支的一个结点(每次commit相当于在当前分支末端插入一个节点),主分支通常命名为main或master, 通常使用HEAD指向当前分支

创建分支:

复制代码
$ git branch [new-branch-name]

在当前结点创建出一支新的分支,名字叫new-branch-name

转到其他分支:

复制代码
$ git checkout [destination-branch]

将HAED指向destination-branch

创建并转到该分支:

复制代码
$ git checkout -b [new-branch-name]

删除分支:

复制代码
$ git branch -d [branch-to-delete]

查看当前所处分支:

复制代码
$ git branch -v

合并分支:

假设需要合并分支master与fixing-ai-heuristics,那么我们先checkout到分支master,然后将fixing-ai-heuristics合并到master

复制代码
$ git checkout master
$ git merge fixing-ai-heuristics

合并效果如图所示:

合并前:

合并后:

创建了新结点C6,C5是C6的第一父亲,C4是C6的第二父亲

此次合并会将两个分支中修改过的文件都在C6呈现,倘若有同一个文件的同一个代码块(函数,代码行)既在master修改过,又在fixing-ai-heuristics修改过,那么就会发生合并冲突

合并冲突示例:

合并冲突的代码会包含如下代码块:

复制代码
<<<<<<< HEAD
for (int i = 0; i < results.length; i++) {
    println(results[i]);
    println("FIX ME!");
}
=======
int[] final = int[results.length];
for (int i = 0; i < results.length - 1; i++) {
    final[i] = results[i] + 1;
    println(final[i]);
}
>>>>>>> fixing-ai-heuristics

第一个代码块是当前HEAD指向的分支的代码

第二个代码块是fixing-ai-heruistice的代码

我们需要人为进行修改,只保留一份,从而解决合并冲突

初始化仓库:

先定位到需要创建仓库的目标目录,然后输入命令git init

复制代码
cd D:\Documents\CS61B
git init

这里只是创建空仓库,仓库里什么都没有

添加文件为待提交状态:

复制代码
git add .

.是缺省符号,意味着添加目录下所有文件,也可以输入具体的文件路径

这里仍然没有将文件添加到仓库,只是设置为待提交(tracked)

查看仓库状态:

复制代码
git status

输出例子:

复制代码
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   tofu/kung_pao_tofu.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    seitan/
    tofu/basil_ginger_tofu.txt

Changes to be committed:

列举所有被tracked的文件(将要被放入仓库的文件)

Untracked files:

未被使用git add跟踪的文件

将文件拷贝到仓库

复制代码
git commit -m "first commit"

-m:允许添加commit的注释

后面紧跟着的字符串就是注释

查询git记录

复制代码
git log

输出例子:

复制代码
commit 9f955d85359fc8e4504d7220f13fad34f8f2c62b
Author: Sandra Upson <sandra@Sandras-MacBook-Air.local>
Date:   Sun Jan 17 19:06:48 2016 -0800

    added tofu recipes

commit后面紧跟的是本次commit的哈希值

展示commit细节

复制代码
git show 9f955d85359fc8e4504d7220f13fad34f8f2c62b

输出例子:

复制代码
commit 9f955d85359fc8e4504d7220f13fad34f8f2c62b
Author: Sandra Upson <sandra@Sandras-MacBook-Air.local>
Date:   Sun Jan 17 19:06:48 2016 -0800

    added tofu recipes

diff --git a/tofu/basil_ginger_tofu.txt b/tofu/basil_ginger_tofu.txt
new file mode 100644
index 0000000..9a56e7a
--- /dev/null
+++ b/tofu/basil_ginger_tofu.txt
@@ -0,0 +1,3 @@
+basil
+ginger
+tofu
diff --git a/tofu/kung_pao_tofu.txt b/tofu/kung_pao_tofu.txt
new file mode 100644 index
0000000..dad9bd9
--- /dev/null
+++ b/tofu/kung_pao_tofu.txt
@@ -0,0 +1,3 @@
+szechuan peppers
+tofu
+peanuts
+kung
+pao

修改文件并commit到仓库:

修改文件后,需要再将其add一遍再commit(即使之前该文件已经被add过),否则该文件将是Changes not staged for commit的类型

修改后未add的git status例子:

复制代码
On branch master
Changes not staged for commit:
    modified:   tofu/kung_pao_tofu.txt

Untracked files:
    seitan/

no changes added to commit

回档至某次commit刚实行之后:

命令:git checkout commit_hash_val directory

复制代码
git checkout 9f955d85359fc8e4504d7220f13fad34f8f2c62b ./recipes/tofu

只有在这次commit及之前被trakced了的文件才会被回溯

命令不会撤销指定的commit记录,只是回溯到本次操作刚实行之后,同时撤销需要commit生效

checkout自动调用git add,所以不用手动git add,直接commit即可

commit本次撤销:

复制代码
git commit -m "went back to the original recipe with no bok choy"

撤销git add:

想要撤销某个文件的待提交状态

复制代码
git reset HEAD [file]

这里只撤销待提交状态,但会保留file_name在当前目录的修改

将本次修改合并到上次的commit中:

复制代码
git commit --amend

会更新上次的commit记录,不会生成本次的commit记录

回溯到上次commit操作刚完成后:

复制代码
git checkout -- [file]
相关推荐
上邪o_O6 小时前
Git 与 GitHub 协作
git·github
fly五行20 小时前
Git基础玩法简单描述
大数据·git·搜索引擎·gitee
JavaDog程序狗21 小时前
【软件环境】Windows安装Git
git
第四维度41 天前
【Debian】4-‌1 Gitea简介以及与其他git方案差异
git·gitea
wdfk_prog1 天前
实战指南:如何将Git仓库中的特定文件夹及其历史完整迁移到另一个仓库
大数据·linux·运维·笔记·git·学习·elasticsearch
乌萨奇也要立志学C++2 天前
【Linux】linux基础开发工具(三) 版本控制器Git、调试器 - gdb/cgdb使用、一些实用的调试技巧
linux·git·elasticsearch
Doris_LMS2 天前
Git、Gitee、GitHub、GitLab完整讲解:从基础到进阶
git·gitee·gitlab·github
CV资深专家2 天前
常用git命令
git
bug和崩溃我都要2 天前
Git提交代码完整流程
git
逢考必过@k2 天前
git的使用
git