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 <[email protected]>
Date:   Sun Jan 17 19:06:48 2016 -0800

    added tofu recipes

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

展示commit细节

复制代码
git show 9f955d85359fc8e4504d7220f13fad34f8f2c62b

输出例子:

复制代码
commit 9f955d85359fc8e4504d7220f13fad34f8f2c62b
Author: Sandra Upson <[email protected]>
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]
相关推荐
omage6 小时前
如何将带有LFS对象的git仓库推送到gitlab
git·gitlab·lfs
Linux运维技术栈6 小时前
从版本控制到协同开发:深度解析 Git、SVN 及现代工具链
git·svn
wusam7 小时前
Linux系统管理与编程23:巧用git资源一键部署LAMP
linux·运维·git·shell·lamp
不爱学英文的码字机器9 小时前
[Git] 如何进行版本回退
大数据·git·elasticsearch
白开水不加冰18 小时前
git初始化及操作指南
git
PyAIGCMaster20 小时前
vscode git push 记录
ide·git·vscode
君鼎21 小时前
Git企业级——进阶
git
*neverGiveUp*1 天前
本地分支git push 报错 fatal: The current branch XXXX has no upstream branch.
前端·git·gitea
℘团子এ1 天前
git中,给分支添加备注
git