Git教程

本文是一篇详尽的Git教程,涵盖了Git的基本操作,包括版本库创建、时光机穿梭、远程仓库管理、分支管理和标签管理。在分支管理部分,详细介绍了如何创建、合并分支以及解决冲突。此外,还讨论了多人协作场景下如何推送和拉取分支,以及如何创建和删除标签。最后,文章提到了自定义Git配置,如忽略特殊文件、设置别名,甚至搭建Git服务器。
摘要由CSDN通过智能技术生成
1.Git简介
1)安装Git
yum install -y git
2)创建版本库

版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改、删除,Git都能跟踪,以便任何时刻都可以追踪历史,或者在将来某个时刻可以“还原”。
step1:创建一个空目录

[root@foundation8 ~]# mkdir KatyGit
[root@foundation8 ~]# cd KatyGit/
[root@foundation8 KatyGit]# pwd
/root/KatyGit

step2:把这个目录变成Git可以管理的仓库

[root@foundation8 KatyGit]# git init
Initialized empty Git repository in /root/KatyGit/.git/
#瞬间Git就把仓库建好了,而且告诉你是一个空的仓库(empty Git repository),细心的读者可以发现当前目录下多了一个.git的目录,这个目录是Git来跟踪管理版本库的,没事千万不要手动修改这个目录里面的文件,不然改乱了,就把Git仓库给破坏了。

[root@foundation8 KatyGit]# ls -a
.  ..  .git

step3:编写一个纯文本文件

#在Git仓库(/root/KatyGit/当前目录或其子目录)中编写一个readme.txt文件,内容如下:

[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件

step4:把一个文件放到Git仓库中

第一步,用命令git add告诉Git,把文件添加到仓库:
[root@foundation8 KatyGit]# git add readme.txt 

第二步,用命令git commit告诉Git,把文件提交到仓库:
[root@foundation8 KatyGit]# git commit -m "wrote a readme file"
[master (root-commit) 40d7cf2] wrote a readme file
 Committer: root <root@foundation8.ilt.example.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt
#简单解释一下git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。
2.时光机穿梭
1)版本回退

首先我们先把之前的readme.txt进行修改:

[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件
这是Git仓库中的一个纯文本文件 第二版

然后尝试提交:

[root@foundation8 KatyGit]# git add readme.txt 
[root@foundation8 KatyGit]# git commit -m "readme.txt 第二版"
[master 9f1c3eb] readme.txt 第二版
 Committer: root <root@foundation8.ilt.example.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)

在Git中,我们用git log命令查看历史记录:

[root@foundation8 KatyGit]# git log
commit 9f1c3ebd24f5b3144c49f7e06a48b47763c9e706
Author: root <root@foundation8.ilt.example.com>
Date:   Sun Feb 4 23:51:07 2018 +0800

    readme.txt 第二版

commit 40d7cf210cf3081b9042988a0d261a0c00a0abbf
Author: root <root@foundation8.ilt.example.com>
Date:   Sun Feb 4 23:46:13 2018 +0800

    wrote a readme file

#如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:
[root@foundation8 KatyGit]# git log --pretty=oneline
9f1c3ebd24f5b3144c49f7e06a48b47763c9e706 readme.txt 第二版
40d7cf210cf3081b9042988a0d261a0c00a0abbf wrote a readme file

把readme.txt回退到上一个版本:
首先,Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100

[root@foundation8 KatyGit]# git reset --hard HEAD^
HEAD is now at 40d7cf2 wrote a readme file

如果想要在回去readme.txt第二版的该怎么做呢?
首先当前窗口不要关闭,找对应版本的ID,不需要全部写出来,前几个就可以了

[root@foundation8 KatyGit]# git reset --hard 9f1c
HEAD is now at 9f1c3eb readme.txt 第二版
[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件
这是Git仓库中的一个纯文本文件 第二版

同时,Git提供了一个命令git reflog用来记录你的每一次命令:

[root@foundation8 KatyGit]# git reflog
9f1c3eb HEAD@{0}: reset: moving to 9f1c
40d7cf2 HEAD@{1}: reset: moving to HEAD^
9f1c3eb HEAD@{2}: commit: readme.txt 第二版
40d7cf2 HEAD@{3}: commit (initial): wrote a readme file
2)工作区和暂存区
  • 名词解释
    工作区:终端操作的地方:比如我们这里的KatyGit
    暂存区:git add命令执行之后,会被放到版本库中的stage中进行暂存,即就是它了
    Git的版本库:.git
    这里写图片描述
    我们创建Git版本库时,Git自动为我们创建了唯一一个master分支,所以,现在,git commit就是往master分支上提交更改。
3)管理修改
  • 首先明确一个概念:Git管理的是修改,而不是文件
  • 何谓修改:比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改。
4)撤销修改
  • git checkout -- file可以丢弃工作区的修改
[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件
这是Git仓库中的一个纯文本文件 第二版
[root@foundation8 KatyGit]# git checkout -- readme.txt 
[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件
这是Git仓库中的一个纯文本文件 第二版
这是Git仓库中的一个纯文本文件 第二版
这是Git仓库中的一个纯文本文件 第二版
这是Git仓库中的一个纯文本文件 第二版
这是Git仓库中的一个纯文本文件 第二版
  • git reset HEAD file可以把暂存区的修改撤销掉
[root@foundation8 KatyGit]# cat readme.txt 
这是Git仓库中的一个纯文本文件
这是Git仓库中的一个纯文本文件 第二版
这是Git仓库中的一个纯文本文件 第三版
[root@foundation8 KatyGit]# git add readme.txt 
[root@foundation8 KatyGit]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   readme.txt
#
[root@foundation8 KatyGit]# git reset HEAD readme.txt 
Unstaged changes after reset:
M   readme.txt
5)删除文件

首先,先添加一个新文件test.txt到Git并且提交

[root@foundation8 KatyGit]# git add test.txt 
[root@foundation8 KatyGit]# git commit -m "add test.txt"
[master 7f5e03e] add test.txt
 Committer: root <root@foundation8.ilt.example.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt

一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

rm -rf test.txt

这个时候,Git知道你删除了文件,因此,工作区和版本库就不一致了,git status命令会立刻告诉你哪些文件被删除了:

[root@foundation8 KatyGit]# 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)
#
#   modified:   readme.txt
#   deleted:    test.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

现在你有两个选择,一是确实要从版本库中删除该文件,那就用命令git rm删掉,并且git commit:

[root@foundation8 KatyGit]# git rm test.txt
rm 'test.txt'
[root@foundation8 KatyGit]# git commit -m "remove test.txt"
[master 15d7313] remove test.txt
 Committer: root <root@foundation8.ilt.example.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 test.txt

现在,文件就从版本库中被删除了。
另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

[root@foundation8 KatyGit]# git checkout -- test.txt
[root@foundation8 KatyGit]# ls
readme.txt  test.txt
3.远程仓库
1)添加远程库

第一步:获得Git远程仓库(注册一个Github帐号)
第二步:使用SSH进行通讯

  • 创建SSH Key
//在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell,创建SSH Key:
 ssh-keygen -t rsa -C "youremail@example.com"
  • 登陆GitHub
    这里写图片描述
    然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容,点“Add Key”,你就应该看到已经添加的Key:
    这里写图片描述
    第三步:向远程仓库进行推送
    创建一个远程仓库和本地的Git仓库名字一样
    这里写图片描述
    在本地的KatyGit仓库下运行命令:
git remote add origin git@github
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值