杨记

碎片化学习令人焦虑,系统化学习使人进步

0%

git基本教程

学习自菜鸟教程https://www.runoob.com/git/git-tutorial.html,有修改

其它学习链接:http://marklodato.github.io/visual-git-guide/index-zh-cn.html

git安装

Git 各平台安装包下载地址为:http://git-scm.com/downloads

Ubuntu

1
apt-get install git

git配置

Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量。

这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:

  • /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件。
  • ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件。
  • 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效。每一个级别的配置都会覆盖上层的相同配置,所以 .git/config 里的配置会覆盖 /etc/gitconfig 中的同名变量。

在 Windows 系统上,Git 会找寻用户主目录下的 .gitconfig 文件。主目录即 $HOME 变量指定的目录,一般都是 C:\用户\你的用户名

此外,Git 还会尝试找寻 /etc/gitconfig 文件,只不过看当初 Git 装在什么目录,就以此作为根目录来定位。

用户信息

配置个人的用户名称和电子邮件地址:

1
2
$ git config --global user.name "runoob"
$ git config --global user.email test@runoob.com
  • user.name github的用户名
  • user.email github的注册邮箱

如果用了 —global 选项,那么更改的配置文件就是位于你用户主目录下的那个,以后你所有的项目都会默认使用这里配置的用户信息。

如果要在某个特定的项目中使用其他名字或者电邮,只要去掉 —global 选项重新配置即可,新的设定保存在当前项目的 .git/config 文件里。

image-20220328155821836

查看配置信息

要检查已有的配置信息,可以使用 git config —list 命令:

1
2
3
4
$ git config --list
http.postbuffer=2M
user.name=runoob
user.email=test@runoob.com

有时候会看到重复的变量名,那就说明它们来自不同的配置文件(比如 /etc/gitconfig 和 ~/.gitconfig),不过最终 Git 实际采用的是最后一个。

这些配置我们也可以在 ~/.gitconfig/etc/gitconfig 看到,如下所示:

1
vim ~/.gitconfig 

显示内容如下所示:

1
2
3
4
5
[http]
postBuffer = 2M
[user]
name = runoob
email = test@runoob.com

也可以直接查阅某个环境变量的设定,只要把特定的名字跟在后面即可,像这样:

1
2
$ git config user.name
runoob

工作区、index、HEAD

  • 工作区Work Space:就是你在电脑里能看到的目录。
  • 暂存区:英文叫 stage 或 index。一般存放在 .git 目录下的 index 文件(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
  • 版本库:工作区有一个隐藏目录 .git,这个不算工作区,而是 Git 的版本库。

image-20220327192220570

  • 图中左侧为工作区,右侧为版本库。在版本库中标记为 “index” 的区域是暂存区(stage/index),标记为 “master” 的是 master 分支所代表的目录树。
  • 图中我们可以看出此时 “HEAD” 实际是指向 master 分支的一个”游标”。所以图示的命令中出现 HEAD 的地方可以用 master 来替换。
  • 图中的 objects 标识的区域为 Git 的对象库,实际位于 “.git/objects” 目录下,里面包含了创建的各种对象及内容。
  • 当对工作区修改(或新增)的文件执行 git add 命令时,暂存区的目录树被更新,同时工作区修改(或新增)的文件内容被写入到对象库中的一个新的对象中,而该对象的ID被记录在暂存区的文件索引中。
  • 当执行提交操作(git commit)时,暂存区的目录树写到版本库(对象库)中,master 分支会做相应的更新。即 master 指向的目录树就是提交时暂存区的目录树。
  • 当执行 git reset HEAD 命令时,暂存区的目录树会被重写,被 master 分支指向的目录树所替换,但是工作区不受影响。
  • 当执行 git rm —cached 命令时,会直接从暂存区删除文件,工作区则不做出改变。
  • 当执行 git checkout . 或者 git checkout — 命令时,会用暂存区全部或指定的文件替换工作区的文件。这个操作很危险,会清除工作区中未添加到暂存区中的改动。
  • 当执行 git checkout HEAD . 或者 git checkout HEAD 命令时,会用 HEAD 指向的 master 分支中的全部或者部分文件替换暂存区和以及工作区中的文件。这个命令也是极具危险性的,因为不但会清除工作区中未提交的改动,也会清除暂存区中未提交的改动。

创建仓库

使用当前目录作为 Git 仓库,我们只需使它初始化。

1
git init

该命令执行完后会在当前目录生成一个 .git 目录(是隐藏文件夹)。

使用我们指定目录作为Git仓库。

1
git init newrepo

初始化后,会在 newrepo 目录下会出现一个名为 .git 的目录,所有 Git 需要的数据和资源都存放在这个目录中。

添加和提交

如果当前目录下有几个文件想要纳入版本控制,需要先用 git add 命令告诉 Git 开始对这些文件进行跟踪,然后提交:

1
2
3
$ git add *.c
$ git add README
$ git commit -m '初始化项目版本'

以上命令将目录下以 .c 结尾及 README 文件提交到仓库中。

git add * 把当前目录的所有文件和文件夹都添加到暂存区

注: 在 Linux 系统中,commit 信息使用单引号 ,Windows 系统,commit 信息使用双引号 。所以在 git bash 中 git commit -m ‘提交说明’ 这样是可以的,在 Windows 命令行中就要使用双引号 git commit -m “提交说明”

git clone

克隆远端仓库

1
2
3
git clone <repo>
git clone <repo> <directory>
git clone -b branch_name <repo> # 拉取指定分支,branch_name分支名

参数说明:

  • repo:Git 仓库,可以是远端仓库,也可以是本地仓库
  • directory:本地目录,目录可以不为空(最好是空目录),目录不存在会创建

示例:

1、克隆远端仓库到mytest目录

1
$ git clone git@github.com:yanglinqi107/LifeGame.git mytest 

2、克隆克隆远端仓库到当前目录

1
$ git clone https://github.com/yanglinqi107/LifeGame.git

克隆本地仓库:同样是上面的命令git clone <repo> <directory>

例:要将E:\code里面的所有文件克隆到另一个目录mytest

法一:直接将E:\code里面的内容手动复制过去

法二:

  • 1)将E:\code设为仓库git init E:/code
  • 2)将E:\code的所有文件添加到暂存区 git add *
  • 3)将暂存区的所有文件添加到HEAD git commit -m "本地克隆测试"
  • 4)将E:\code克隆过去 git clone e:/code mytest
  • 注意:前3步必要,不然克隆不过去

git基本操作

Git 常用的是以下 6 个命令:git clonegit pushgit addgit commitgit checkoutgit pull

image-20220327210917927

说明:

  • workspace:工作区
  • staging area:暂存区/缓存区
  • local repository:版本库或本地仓库
  • remote repository:远程仓库

一个简单的操作步骤:

1
2
3
$ git init    
$ git add .
$ git commit
  • git init - 初始化仓库。
  • git add . - 添加文件到暂存区。
  • git commit - 将暂存区内容添加到仓库中。

提交与修改

Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比。

下表列出了有关创建与提交你的项目的快照的命令:

命令 说明
git add 添加文件到暂存区
git status 查看仓库当前的状态,显示有变更的文件。
git diff 比较文件的不同,即暂存区和工作区的差异。
git commit 提交暂存区到本地仓库。
git reset 回退版本。
git rm 删除工作区文件。
git mv 移动或重命名工作区文件。

提交日志

命令 说明
git log 查看历史提交记录
git blame 以列表形式查看指定文件的历史修改记录

远程操作

命令 说明
git remote 远程仓库操作
git fetch 从远程获取代码库
git pull 下载远程代码并合并
git push 上传远程代码并合并

git add

添加一个或多个文件到暂存区:

1
git add [file1] [file2] ...

添加指定目录到暂存区,包括子目录:

1
git add [dir]

添加当前目录下的所有文件到暂存区:

1
git add .

可以使用git statusgit status -s查看状态

git status

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
MLZ107@YangLinQi MINGW64 /e/git (master)
$ touch 测试5.txt

MLZ107@YangLinQi MINGW64 /e/git (master)
$ touch 测试6.txt

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git add .

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git status -s
A "\346\265\213\350\257\2255.txt" # 这是乱码,看最后的5和6
A "\346\265\213\350\257\2256.txt"

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: "\346\265\213\350\257\2255.txt"
new file: "\346\265\213\350\257\2256.txt"


MLZ107@YangLinQi MINGW64 /e/git (master)
$ vi 测试6.txt # 修改 测试6.txt 文件,也可以外部修改

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git status -s
A "\346\265\213\350\257\2255.txt"
AM "\346\265\213\350\257\2256.txt"

AM 状态的意思是这个文件在我们将它添加到缓存之后又有改动。改动后我们再执行 git add . 命令将其添加到缓存中。

git diff

git diff 命令比较文件的不同,即比较文件在暂存区和工作区的差异。

git diff 命令显示已写入暂存区和已经被修改但尚未写入暂存区文件的区别。

git diff 有两个主要的应用场景。

  • 尚未缓存的改动(工作区和暂存区):git diff
  • 查看已缓存的改动(暂存区和版本库): git diff —cached
  • 查看已缓存的与未缓存的所有改动:git diff HEAD
  • 显示摘要而非整个 diff:git diff —stat

显示暂存区和工作区的差异:

1
$ git diff [file]

示例:测试6.txt文件中追加内容,并和暂存区的测试6.txt比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
MLZ107@YangLinQi MINGW64 /e/git (master)
$ vi 测试6.txt

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git diff 测试6.txt
warning: LF will be replaced by CRLF in 测试6.txt.
The file will have its original line endings in your working directory
diff --git a/测试6.txt b/测试6.txt
index e69de29..ecb7222 100644
--- a/测试6.txt
+++ b/测试6.txt
@@ -0,0 +1,2 @@
+这是测试
+git diff 测试

显示暂存区和上一次提交(commit)的差异:

1
2
3
$ git diff --cached [file]

$ git diff --staged [file]

示例:将修改后的测试6.txt检入暂存区,并和HEAD的测试6.txt比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
MLZ107@YangLinQi MINGW64 /e/git (master)
$ git add .
warning: LF will be replaced by CRLF in 测试6.txt.
The file will have its original line endings in your working directory

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git diff --cached
diff --git a/测试5.txt b/测试5.txt
new file mode 100644
index 0000000..e69de29
diff --git a/测试6.txt b/测试6.txt
new file mode 100644
index 0000000..265e175
--- /dev/null
+++ b/测试6.txt
@@ -0,0 +1,2 @@
+这是测试
+git diff --cached 测试

显示两次提交之间的差异:

1
$ git diff [first-branch]...[second-branch]

git commit

git commit 命令将暂存区内容添加到本地仓库中。

提交暂存区到本地仓库中:

1
git commit -m [message]
  • message现在是必要的,可以写一些备注信息,如git commit -m "这是一次测试"

提交暂存区的指定文件到仓库区:

1
$ git commit [file1] [file2] ... -m [message]

-a 参数设置修改文件后不需要执行 git add 命令,直接来提交

1
$ git commit -a

暂存区的文件全部提交后,使用git status查看显示working tree clean

1
2
3
4
MLZ107@YangLinQi MINGW64 /e/git (master)
$ git status
On branch master
nothing to commit, working tree clean

设置提交代码时的用户信息

开始前我们需要先设置提交的用户信息,包括用户名和邮箱:

1
2
$ git config --global user.name 'runoob'
$ git config --global user.email test@runoob.com
  • 如果去掉 --global 参数只对当前仓库有效

git reset

git reset 命令用于回退版本,可以指定退回某一次提交的版本。

git reset 命令语法格式如下:

1
git reset [--soft | --mixed | --hard] [HEAD]

1、—mixed 为默认,可以不用带该参数,用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变

1
git reset [HEAD] 

注意:直接使用git reset HEADgit reset版本库并不会回退

实例:

1
2
3
$ git reset HEAD^            # 回退所有内容到上一个版本  
$ git reset HEAD^ hello.php # 回退 hello.php 文件的版本到上一个版本
$ git reset 052e # 回退到指定版本

2、—soft 参数用于回退到某个版本:工作区和暂存区不变

1
git reset --soft HEAD

实例:

1
$ git reset --soft HEAD~3 # 回退上上上一个版本

3、—hard 参数撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交:

1
git reset --hard HEAD

实例:

1
2
3
$ git reset –hard HEAD~3  # 回退上上上一个版本  
$ git reset –hard bae128 # 回退到某个版本回退点之前的所有信息。
$ git reset --hard origin/master # 将本地的状态回退到和远程的一样
**注意:**谨慎使用 –hard 参数,它会删除回退点之前的所有信息

4、HEAD 说明:

  • HEAD 表示当前版本
  • HEAD^ 上一个版本
  • HEAD^^ 上上一个版本
  • HEAD^^^ 上上上一个版本
  • 以此类推…

可以使用 ~数字表示

  • HEAD~0 表示当前版本
  • HEAD~1 上一个版本
  • HEAD^2 上上一个版本
  • HEAD^3 上上上一个版本
  • 以此类推…

5、git reset HEAD 或 git reset:用于取消已缓存的内容。(?暂存区)

使用git resetgit reset HEAD会将暂存区的内容清除

image-20220328135315448

实例:1)创建目录e:/git并初始化为仓库,创建test1.txt

image-20220328121119244

2)track和commit test1.txt 到版本库

image-20220328121228020

3)同样的步骤提交 test2.txt到版本库

image-20220328121325131

4)当前版本库有test1.txt,test2.txt两个文件,回退上一版本有test1.txt一个文件;回退后克隆当前版本库至test3文件夹,可以看到确实只有test1.txt一个文件

image-20220328133821034

git rm

git rm 命令用于删除文件

1、如果工作目录有文件A添加到暂存区(没有add的话直接手动删除),再手工删除文件A的话,运行 git status 时就会在 Changes not staged for commit 的提示,但是我测试commit是成功的

image-20220328140517067

2、git rm <file> 将文件从暂存区和工作区中删除

如果文件A在暂存区后,A修改过且再git add更新暂存区,需要使用git rm -f A来删除工作区和暂存区的文件A

image-20220328141440592

3、如果想把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 —cached 选项即可:

1
git rm --cached <file>

git mv

git mv 命令用于移动或重命名一个文件、目录或软连接。

1
git mv [file] [newfile]
  • 注:源文件file必须add到版本控制中才能使用git mv

image-20220328143028516

如果新文件名已经存在,但还是要重命名它,可以使用 -f 参数:

1
git mv -f [file] [newfile]  # file同样要add到暂存区

image-20220328143955435

git log

git log:查看历史提交记录

image-20220328144805666

--oneline 选项来查看历史记录的简洁的版本

image-20220328144906544

--graph 选项,查看历史中什么时候出现了分支、合并

image-20220328145011081

--reverse 参数来逆向显示所有日志

image-20220328145108643

  • 如果只想查找指定用户的提交日志可以使用命令:git log --author , 例如,比方说我们要找 Git 源码中 Linus 提交的部分:

  • ```
    $ git log —author=Linus —oneline -5
    81b50f3 Move ‘builtin-*’ into a ‘builtin/‘ subdirectory
    3bb7256 make “index-pack” a built-in
    377d027 make “git pack-redundant” a built-in
    b532581 make “git unpack-file” a built-in
    112dd51 make “mktag” a built-in

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

    - 如果你要指定日期,可以执行几个选项:`--since` 和 `--before`,但是你也可以用 `--until` 和 `--after`。

    - 例如,如果我要看 Git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了 `--no-merges`选项以隐藏合并提交):

    - ```
    $ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
    5469e2d Git 1.7.1-rc2
    d43427d Documentation/remote-helpers: Fix typos and improve language
    272a36b Fixup: Second argument may be any arbitrary string
    b6c8d2d Documentation/remote-helpers: Add invocation section
    5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
    00b84e9 Documentation/remote-helpers: Rewrite description
    03aa87e Documentation: Describe other situations where -z affects git diff
    77bc694 rebase-interactive: silence warning when no commits rewritten
    636db2c t3301: add tests to use --format="%N"
  • git blame

  • 如果要查看指定文件的修改记录可以使用 git blame 命令,格式如下:

  • ```
    git blame

    1
    2
    3
    4
    5
    6
    7

    - git blame 命令是以列表形式显示修改记录,如下实例:

    - ```
    $ git blame README
    ^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # Runoob Git 测试
    db9315b0 (runoob 2020-08-25 16:00:23 +0800 2) # 菜鸟教程

git remote

显示所有远程仓库:

1
2
3
git remote -v

git remote

添加远程版本库:

1
git remote add origin git@github.com yourname/yourRepo
  • origin为远程地址的别名

显示某个远程仓库的信息:

1
2
3
4
5
git remote show [remote]

eg:
git remote show https://github.com/tianqixin/runoob-git-test
git remote show origin

删除和远程仓库的连接:

1
git remote rm repo_name

修改仓库别名:

1
git remote rename old_name new_name

git pull

git pull 命令用于从远程获取代码并合并本地的版本。

git pull 其实就是 git fetchgit merge FETCH_HEAD 的简写。 命令格式如下:

1
git pull <远程主机名> <远程分支名>:<本地分支名>

实例

将远程主机 origin 的 master 分支拉取过来,与本地的 brantest 分支合并。

1
git pull origin master:brantest

如果远程分支是与当前分支合并,则冒号后面的部分可以省略。

1
git pull origin master

上面命令表示,取回 origin/master 分支,再与本地的 brantest 分支合并。

git push

git push 命用于从将本地的分支版本上传到远程并合并。

命令格式如下:

1
git push <远程主机名> <本地分支名>:<远程分支名>

如果本地分支名与远程分支名相同,则可以省略冒号:

1
git push <远程主机名> <本地分支名>

如果本地版本与远程版本有差异,但又要强制推送可以使用 —force 参数:

1
git push --force origin master

删除远程仓库的分支:使用 —delete 参数,以下命令表示删除 origin 主机的 master 分支:

1
git push origin --delete master
  • 如果github仓库只有master分支,删除不了

git分支管理

管理的是仓库本地分支

1、创建分支命令:

1
git branch branch_name

2、切换分支命令:

1
git checkout branchname
  • 当你切换分支的时候,Git 会用该分支的最后提交的快照替换你的工作目录的内容, 所以多个分支不需要多个目录
  • 当前分支下文件有修改但没commit时,切换分支会失败,除了创建新分支切换

3、合并分支命令:

1
git merge 
  • 可以多次合并到统一分支, 也可以选择在合并之后直接删除被并入的分支

4、列出分支:

1
git branch

创建分支实例:

手动创建一个分支testing

1
2
3
4
$ git branch testing
$ git branch
* master
testing

如你所见,我们创建了一个分支,在该分支上移除了一些文件 test.txt,并添加了 runoob.php 文件,然后切换回我们的主分支,删除的 test.txt 文件又回来了,且新增加的 runoob.php 不存在主分支中。

使用分支将工作切分开来,从而让我们能够在不同开发环境中做事,并来回切换。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ls
README
$ echo 'runoob.com' > test.txt
$ git add .
$ git commit -m 'add test.txt'
[master 3e92c19] add test.txt
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$ ls
README test.txt
$ git checkout testing
Switched to branch 'testing'
$ ls
README

当我们切换到 testing 分支的时候,我们添加的新文件 test.txt 被移除了。切换回 master 分支的时候,它们又重新出现了。

注意:分支关系测试(不仅仅是这样,有点问题)
  • 若在分支A创建分支B,则相当于将分支A复制了一份成分支B,即分支AB的HEAD一样。
  • 所有分支共享工作区暂存区,但当某个工作区的文件f1添加到分支A中后,其它分支就看不到该文件了

image-20220328192446225

实操:

image-20220328193248453

image-20220328193946293

image-20220328194338923

5、创建新分支并切换 git checkout -b branch_name

6、删除分支:在A分支下不能删除A分支,必须先切换到其它分支

1
2
3
git branch -d branch_name

git branch -D branch_name

7、分支合并

1
git merge branch_name
  • 不能合并没有历史关系的分支 fatal: refusing to merge unrelated histories

8、合并冲突

合并并不仅仅是简单的文件添加、移除的操作,Git 也会合并修改

  • 1)冲突要手动修改
  • 2)修改后要addcommit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
MLZ107@YangLinQi MINGW64 /e/git (master)
$ vi test2_t.txt # 修改test2_t.txt文件

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git checkout test2 # 切换已存在的分支test2报错,提示修改的内容未commit
error: Your local changes to the following files would be overwritten by checkout:
test2_t.txt
Please commit your changes or stash them before you switch branches.
Aborting

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git checkout -b test3 # 新建分支test3并切换,没有问题
Switched to a new branch 'test3'

MLZ107@YangLinQi MINGW64 /e/git (test3)
$ cat test2_t.txt # 查看test2_t.txt的内容
这是master下的修改

MLZ107@YangLinQi MINGW64 /e/git (test3)
$ git commit -am "test3" # 将test2_t.txt提交到test3分支中
warning: LF will be replaced by CRLF in test2_t.txt.
The file will have its original line endings in your working directory
[test3 077c5d7] test3
1 file changed, 1 insertion(+)

MLZ107@YangLinQi MINGW64 /e/git (test3)
$ git checkout master # 切换回master分支
Switched to branch 'master'

MLZ107@YangLinQi MINGW64 /e/git (master)
$ ls # 目录下test2_t.txt还存在,?
test1.txt test10.txt test2.txt test2_t.txt test3.txt

MLZ107@YangLinQi MINGW64 /e/git (master)
$ cat test2_t.txt # 但test2_t.txt回到修改前

MLZ107@YangLinQi MINGW64 /e/git (master)
$ vi test2_t.txt, # 修改test2_t.txt, 这是master下第2次修改

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git commit -am "master" # 将test2_t.txt提交到master分支中
warning: LF will be replaced by CRLF in test2_t.txt.
The file will have its original line endings in your working directory
[master 43b7ee7] master
1 file changed, 1 insertion(+)

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git merge test3 # 合并test3分支,发生冲突
Auto-merging test2_t.txt
CONFLICT (content): Merge conflict in test2_t.txt
Automatic merge failed; fix conflicts and then commit the result.

MLZ107@YangLinQi MINGW64 /e/git (master|MERGING)
$ cat test2_t.txt # 查看test2_t.txt文件
<<<<<<< HEAD
这是master下第2次修改
=======
这是master下的修改
>>>>>>> test3

MLZ107@YangLinQi MINGW64 /e/git (master|MERGING)
$ vi test2_t.txt # 需要手动去修改

MLZ107@YangLinQi MINGW64 /e/git (master|MERGING)
$ cat test2_t.txt # 例子就随便改改
这是master下第2次修改
这是master下的修改

最后再add和commit

git 标签

如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 git tag 给它打上标签。

添加标签

比如说,我们想为我们的 runoob 项目发布一个”1.0”版本。 我们可以用 git tag -a v1.0 命令给最新一次提交打上(HEAD)”v1.0”的标签。

-a 选项意为”创建一个带注解的标签”。 不用 -a 选项也可以执行的,但它不会记录这标签是啥时候打的,谁打的,也不会让你添加个标签的注解。 我推荐一直创建带注解的标签。

1
$ git tag -a v1.0 

当你执行 git tag -a 命令时,Git 会打开你的编辑器,让你写一句标签注解,就像你给提交写注解一样。

使用git log --oneline --decorate --graph命令查看,可以看到标签

1
2
3
4
5
6
7
8
9
10
11
12
13
MLZ107@YangLinQi MINGW64 /e/git (master)
$ git log --oneline --decorate --graph
* 6098fd1 (HEAD -> master, tag: v1.0) Merge branch 'test3'
|\
| * 077c5d7 (test3) test3
* | 43b7ee7 master
|/
* dca4fd6 test2_t.txt
* b3d9b43 (origin/test2, origin/master, test2) Update test1.txt
* 4c190bd Update test1.txt
* 0f31cba 删除提交测试
* 2c81853 第3次commit
* c16838a 第1次commit

追加标签:如果我们忘了给某个提交打标签,又将它发布了,我们可以给它追加标签。

例如,我们要给上例dca4fd6追加标签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MLZ107@YangLinQi MINGW64 /e/git (master)
$ git tag -a v0.9 dca4fd6

MLZ107@YangLinQi MINGW64 /e/git (master)
$ git log --decorate --graph --oneline
* 6098fd1 (HEAD -> master, tag: v1.0) Merge branch 'test3'
|\
| * 077c5d7 (test3) test3
* | 43b7ee7 master
|/
* dca4fd6 (tag: v0.9) test2_t.txt
* b3d9b43 (origin/test2, origin/master, test2) Update test1.txt
* 4c190bd Update test1.txt
* 0f31cba 删除提交测试
* 2c81853 第3次commit
* c16838a 第1次commit

查看所有标签git tag

删除便签:git tag -d v1.0

查看此版本所修改的内容:git show v1.0

指定标签信息命令:

1
git tag -a <tagname> -m "runoob.com标签"

PGP签名标签命令:

1
git tag -s <tagname> -m "runoob.com标签"

git status出现乱码

git status 显示中文和解决中文乱码_铁乐与猫的博客-CSDN博客_git status 中文乱码

git status中文文件名乱码 | 木凡博客 (mounui.com)

git帮助

D:xxxx\Git\mingw64\share\doc\git-docgit安装路径下有html类型的帮助文档

image-20220328114745391

也可以使用--help打开,如git reset --help,但是是纯英文的,看的好难受

Git和Github

由于你的本地 Git 仓库和 GitHub 仓库之间的传输是通过SSH加密的,所以我们需要配置验证信息:

在本地创建SSH Key

1
ssh-keygen -t rsa -C "your_email@youremail.com"

后面的your_email@youremail.com改为你在github上注册的邮箱,之后会要求确认路径和输入密码,我们这使用默认的一路回车就行。成功的话会在~/下生成.ssh文件夹,进去,打开id_rsa.pub,复制里面的key。(Windows路径:C:\用户\自己的用户名\.ssh\

回到github上,进入 Settings(账户配置),左边选择SSH and GPG keys,右边:New SSH key,title随便填,粘贴在你电脑上生成的key。

image-20220328153809005

image-20220328153927384

为了验证是否成功,在git bash下输入:

1
$ ssh -T git@github.com

如果是第一次的会提示是否continue,输入yes就会看到:You've successfully authenticated, but GitHub does not provide shell access 。这就表示已成功连上github。

接下来我们要做的就是把本地仓库传到github上去,在此之前还需要设置usernameemail,因为github每次commit都会记录他们。

进入要上传的仓库,右键git bash,添加远程地址:

1
$ git remote add origin git@github.com:yourName/yourRepo.git

后面的yourNameyourRepo表示你在github的用户名和刚才新建的仓库,加完之后进入.git,打开config,这里会多出一个remote “origin”内容,这就是刚才添加的远程地址,也可以直接修改config来配置远程地址。

image-20220328160158267

然后的一般流程就是

  • git add . 将所有文件添加到暂存区
  • git commit -m "这是一次提交测试" 将改动添加到HEAD本地版本库
  • git push origin master 推送到github上,master是分支,可以换成别的分支(其它分支要先创建)

查看当前的远程库

要查看当前配置有哪些远程仓库,可以用命令:

1
git remote

实例

1
2
3
4
5
$ git remote
origin
$ git remote -v
origin git@github.com:yanglinqi107/test (fetch)
origin git@github.com:yanglinqi107/test (push)
  • 执行时加上 -v 参数,你还可以看到每个别名的实际链接地址

提取远程仓库

Git 有两个命令用来提取远程仓库的更新。

1、从远程仓库下载新分支与数据:

1
git fetch [alias]

该命令执行完后需要执行 git merge 远程分支到你所在的分支。

2、从远端仓库提取数据并尝试合并到当前分支:

1
git merge [alias]/[branch]
  • 该命令就是在执行 git fetch 之后紧接着执行 git merge 远程分支到你所在的任意分支

补充:

  • 执行 git fetch origin master 时,它的意思是从名为 origin 的远程上拉取名为 master 的分支到本地分支 origin/master 中。既然是拉取代码,当然需要同时指定远程名与分支名,所以分开写。
  • 执行 git merge origin/master 时,它的意思是合并名为 origin/master 的分支到当前所在分支。既然是分支的合并,当然就与远程名没有直接的关系,所以没有出现远程名。需要指定的是被合并的分支。
  • 执行 git push origin master 时,它的意思是推送本地的 master 分支到远程 origin,涉及到远程以及分支,当然也得分开写了。
  • 还可以一次性拉取多个分支的代码:git fetch origin master stable oldstable
  • 也还可以一次性合并多个分支的代码:git merge origin/master hotfix-2275 hotfix-2276 hotfix-2290

删除远程仓库

删除远程仓库你可以使用命令:

1
git remote rm [别名]

实例github上的仓库并不会被删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ git remote -v
origin git@github.com:tianqixin/runoob-git-test.git (fetch)
origin git@github.com:tianqixin/runoob-git-test.git (push)

# 添加仓库 origin2
$ git remote add origin2 git@github.com:tianqixin/runoob-git-test.git

$ git remote -v
origin git@github.com:tianqixin/runoob-git-test.git (fetch)
origin git@github.com:tianqixin/runoob-git-test.git (push)
origin2 git@github.com:tianqixin/runoob-git-test.git (fetch)
origin2 git@github.com:tianqixin/runoob-git-test.git (push)

# 删除仓库 origin2
$ git remote rm origin2
$ git remote -v
origin git@github.com:tianqixin/runoob-git-test.git (fetch)
origin git@github.com:tianqixin/runoob-git-test.git (push)

Git和Gitee

国内访问 Github 速度比较慢,使用国内的 Git 托管服务———Gitee(gitee.com)

1、我们先在 Gitee 上注册账号并登录后,然后上传自己的 SSH 公钥

设置-》SSH公钥-》将之前生成的SSH公钥~/.ssh/id_rsa.pub粘贴上去

2、验证是否能成功访问 ssh -T git@gitee.com 和连接github的操作差不多

3、在Gitee上创建仓库,复制SSH链接

4、将本地仓库和远程库连接

  • 在本地库上使用命令 git remote add 把它和 Gitee 的远程库关联:

    1
    git remote add origin git@gitee.com:imnoob/runoob-test.git
  • 在使用命令 git remote add 时报错 fatal: remote origin already exists.

  • 说明本地库已经关联了一个名叫 origin 的远程库,可以先用 git remote -v 查看远程库信息
  • 可以先删除已有的远程库连接 git remote rm origin

5、进行推送文件测试

1
2
3
4
touch README.md
git add .
git commit -m "first commit"
git push -u origin master

关联多个远程库

Q:一个本地库能不能既关联 GitHub,又关联 Gitee 呢?

A:可以,因为 git 本身是分布式版本控制系统

使用多个远程库时,git 给远程库起的默认名称是 origin,如果有多个远程库,我们需要用不同的名称来标识不同的远程库

1、关联 GitHub 的远程库:

1
git remote add github git@github.com:tianqixin/runoob-git-test.git
  • 注意,远程库的名称叫 github,不叫 origin

2、关联 Gitee 的远程库:

1
git remote add gitee git@gitee.com:imnoob/runoob-test.git
  • 远程库的名称叫 gitee,不叫 origin

3、验证是否连接成功

1
2
3
git remote -v
ssh -T git@gitee.com
ssh -T git@github.com

推送:

推送到 GitHub,使用命令:

1
git push github master

推送到 Gitee,使用命令:

1
git push gitee master

image-20220329113937775

Git服务器搭建

菜鸟教程Git服务器搭建https://www.runoob.com/git/git-server.html

前置条件:一台服务器,没有服务器在电脑上用虚拟机安装Centos或Ubuntu来试一下

1、在Centos安装git,然后创建一个git用户组和用户,用来运行git服务:

1
2
3
4
5
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git

$ groupadd git # 创建用户组git
$ useradd git -g git # Ubuntu: useradd -m git

2、创建证书登录

收集所有需要登录的用户的公钥,公钥位于id_rsa.pub文件中,把我们的公钥导入到/home/git/.ssh/authorized_keys文件里,一行一个。

如果没有该文件创建它:

1
2
3
4
5
$ cd /home/git/ 
$ mkdir .ssh # 创建.ssh文件夹,linux中 . 开头的文件夹是隐藏文件
$ chmod 755 .ssh # 赋予权限 rwxr-xr-x
$ touch .ssh/authorized_keys # 创建文件
$ chmod 644 .ssh/authorized_keys # 赋予权限

示例

image-20220329145611237

image-20220329145908985

3、初始化Git仓库

首先我们选定一个目录作为Git仓库,假定是/home/gitrepo/runoob.git,在/home/gitrepo目录下输入命令:

1
2
3
4
5
6
$ cd /home        
$ mkdir gitrepo # 创建gitrepo文件夹
$ chown git:git gitrepo/ # 设置 gitrepo 的 属主 为用户组git的用户git
$ cd gitrepo
$ git init --bare runoob.git # 创建一个裸库 runoob.git
Initialized empty Git repository in /home/gitrepo/runoob.git/

服务器上的Git仓库通常都以.git结尾。然后,把仓库所属用户改为git:

1
$ chown -R git:git runoob.git

image-20220329142903738

4、克隆仓库

1
2
3
4
$ git clone git@192.168.45.4:/home/gitrepo/runoob.git
Cloning into 'runoob'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

192.168.45.4 为 Git 所在服务器 ip ,你需要将其修改为你自己的 Git 服务 ip。这样我们的 Git 服务器安装就完成。

示例:

image-20220329150319147

image-20220329150745566

image-20220329152319559

注:使用 --bare 参数初始化的仓库,我们一般称之为裸仓库, 因为这样创建的仓库并不包含 工作区 , 也就是说,我们并不能在这个目录下执行我们一般使用的 Git 命令。https://www.cnblogs.com/irockcode/p/8761954.html

push起冲突

git stash 暂存修改(当本地有代码修改时需要暂存,否则不需要暂存,直接拉取代码即可)

git pull 拉取远端代码

git stash pop

  • 1如果本地没有代码修改,此操作会报错,无需理会
  • 2如果本地代码与远端代码有冲突,会在这一步显示冲突,需修改冲突后再提交代码)

git add file_name 新增文件

git add dir_name 新增文件夹下所有文件(可以cd到具体目录下然后git add .)

git commit -am “提交说明” -a表示提交所有修改(只提交某个文件的话就指定文件名) -m后带提交说明

git push 推送代码到远端

报错

1、Connection closed by ::1 port 22

重新生成密钥或网络问题,因为国内访问Github可能很慢

2、fatal: refusing to merge unrelated histories

git pullgit pushgit merge等命令可能会遇到,

在操作命令后面加--allow-unrelated-histories

--force

欢迎关注我的其它发布渠道