Git基本使用

当我们需要使用git进行版本管理时,要接触的第一个概念就是"仓库",你可以把"仓库"理解成一个目录,只有这个目录中的文件才能被git管理,换句话说就是,如果你想要对某个文件进行版本管理,你就需要把这个文件放入到一个带有git功能的目录中,这个带有git功能的目录就是所谓的git仓库,git仓库的英文为"git repository",后文中所提到"仓库"、“版本库”、“repository”、“repo"其实都是一种东西,我们会不加区分的使用这些名词,它们都表示"仓库”,当你把一个文件加入到某个git仓库以后,你对这个文件的所有操作都可以被git记录,从而实现版本管理的目的。

所以,为了使用git进行版本管理,我们首先要做的,就是创建一个git repository。我们可以直接创建一个空的git仓库,也可以将一个已经存在目录转化成git仓库,使用git init 命令即可,我们先来看看怎样创建一个新的、空的git仓库。

##创建一个名为test2-project的空目录,进入该目录并初始化git仓库。
[root@centos7 git-project]# mkdir test2-project
[root@centos7 git-project]# cd test2-project/
[root@centos7 test2-project]# git init
初始化空的 Git 版本库于 /data/git-project/test2-project/.git/

##初始化完成后,会在该目录下生成一个.git的隐藏目录
[root@centos7 test2-project]# ls -a
.  ..  .git

.git目录非常重要,git会将版本管理所需的相关信息转化成git对象存放到这个目录中,其实,这个".git"目录才是真正的git仓库,我们依靠它对test2-project目录中的文件和目录结构进行版本管理,".git"目录赋予了test2-project目录进行版本管理的能力。
注意,在任何时候都不要手动的修改或删除".git"目录中的文件,因为这样会破坏仓库,仓库被破坏以后,就无法进行版本管理了。

[root@centos7 test2-project]# tree .git/
.git/
├── branches
├── config
├── description
├── HEAD
├── hooks  ##钩子,与代码构建与检出有关,可以理解为触发器
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── info
   └── exclude
├── objects
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

9 directories, 13 files

git init命令是把当前目录转化成git repo

插入图片

在工作目录下创建两个新文件,分别是test.html和README。

[root@centos7 test2-project]# ls
README  test.html
[root@centos7 test2-project]# cat README 
Project: test project
[root@centos7 test2-project]# cat test.html 
<h1>test page1</h1>

使用git add命令把文件放入暂存区,也可以指定目录,则表示一次性把目录下所有文件放入暂存区。

[root@centos7 test2-project]# git add README test.html

使用git add命令之后,可以看到.git/objects目录下多了两个文件对象。这两个文件对象其实就是文件内容,文件类型,文件大小的哈希码

[root@centos7 test2-project]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── index
├── info
   └── exclude
├── objects
   ├── 63
      └── 452156647080f0f17e0d0cb14d7c1f1f90e22e
   ├── 95
      └── d7a61e3564d841a6dff37c4abc5e492da5e417
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

修改README文件,然后再次添加暂存区。

[root@centos7 test2-project]# vim README 

Project: test project
Version: v 0.1
             
[root@centos7 test2-project]# git add README

此时可以看到文件对象变为3个,因为文件内容不同了,对象就发生了改变。其实每一次git add都相当于在当前工作目录做一次快照。

[root@centos7 test2-project]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── index
├── info
   └── exclude
├── objects
   ├── 42
      └── 2ae817a0fc0996bbdc6bf51b8ac3326a8add72
   ├── 63
      └── 452156647080f0f17e0d0cb14d7c1f1f90e22e
   ├── 95
      └── d7a61e3564d841a6dff37c4abc5e492da5e417
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

在目录下创建一个空的子目录docs,然后再次使用git add,但是此处.git/objects目录下并不会生成新的对象。其实,对git而言不会追踪空目录和空文件,因为文件没有内容就无法生成哈希码。

[root@centos7 test2-project]# ls
docs  README  test.html
[root@centos7 test2-project]# git add .
[root@centos7 test2-project]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── index
├── info
   └── exclude
├── objects
   ├── 42
      └── 2ae817a0fc0996bbdc6bf51b8ac3326a8add72
   ├── 63
      └── 452156647080f0f17e0d0cb14d7c1f1f90e22e
   ├── 95
      └── d7a61e3564d841a6dff37c4abc5e492da5e417
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

在子目录下创建一个空文件(如果是空文件,则/objects目录下只会记录子目录的tree对象,并不记录空文件对象)

[root@centos7 test2-project]# touch docs/changelogs
[root@centos7 test2-project]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── index
├── info
   └── exclude
├── objects
   ├── 42
      └── 2ae817a0fc0996bbdc6bf51b8ac3326a8add72
   ├── 63
      └── 452156647080f0f17e0d0cb14d7c1f1f90e22e
   ├── 95
      └── d7a61e3564d841a6dff37c4abc5e492da5e417
   ├── a0
      └── 13e607682067a860c30d133c427ae83f051cb2
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

文件中添加内容

[root@centos7 test2-project]# vim docs/changelogs

author: chenwj
              

此时.git/objects目录下记录了对象

[root@centos7 test2-project]# tree .git
.git
├── branches
├── config
├── description
├── HEAD
├── hooks
   ├── applypatch-msg.sample
   ├── commit-msg.sample
   ├── post-update.sample
   ├── pre-applypatch.sample
   ├── pre-commit.sample
   ├── prepare-commit-msg.sample
   ├── pre-push.sample
   ├── pre-rebase.sample
   └── update.sample
├── index
├── info
   └── exclude
├── objects
   ├── 42
      └── 2ae817a0fc0996bbdc6bf51b8ac3326a8add72
   ├── 63
      └── 452156647080f0f17e0d0cb14d7c1f1f90e22e
   ├── 95
      └── d7a61e3564d841a6dff37c4abc5e492da5e417
   ├── a0
      └── 13e607682067a860c30d133c427ae83f051cb2
   ├── e6
      └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391
   ├── info
   └── pack
└── refs
    ├── heads
    └── tags

查看git仓库的状态,显示这是“初始提交”,如果某个文件想从暂存区撤销执行git rm --cached 即可。若工作目录中的某个文件已经提交到暂存区了,此时使用rm删除了该文件,也可以恢复)

[root@centos7 test2-project]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#	新文件:    README
#	新文件:    docs/changelogs
#	新文件:    test.html
#

git commit把暂存区中的文件添加到版本库中。提交后会生成一个commit对象。

[root@centos7 test2-project]# git commit  ##如果后面没有根提示信息,则会打开一个编辑窗口


# 请为您的变更输入提交说明。以 '#' 开始的行将被忽略,而一个空的提交
# 说明将会终止提交。
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#       新文件:    README
#       新文件:    docs/changelogs
#       新文件:    test.html
#
Initial Version
Version 0.1
~              
[root@centos7 test2-project]# git commit
[master(根提交) aae8e16] Initial Version Version 0.1
 3 files changed, 3 insertions(+)
 create mode 100644 README
 create mode 100644 docs/changelogs
 create mode 100644 test.html

创建新的文件夹,然后把原先工作目录下的.git文件复制到新的目录下。

[root@centos7 git-project]# mkdir my-project
[root@centos7 git-project]# cp -a test2-project/.git/ my-project/
[root@centos7 my-project]# ls .git
branches  COMMIT_EDITMSG  config  description  HEAD  hooks  index  info  logs  objects  refs
[root@centos7 my-project]# git status
# 位于分支 master
# 尚未暂存以备提交的变更:
#   (使用 "git add/rm <file>..." 更新要提交的内容)
#   (使用 "git checkout -- <file>..." 丢弃工作区的改动)
#
#	删除:      README
#	删除:      docs/changelogs
#	删除:      test.html
#
修改尚未加入提交(使用 "git add" / "git commit -a"

使用git status发现该目录下没有这两个文件,是如何发现的??其实git是根据最近一次提交来判断,因为最近一次提交中包含了这两个文件,而新建的目录下正好没有。若想要恢复,使用git checkout即可。

[root@centos7 my-project]# git checkout -- README
[root@centos7 my-project]# ls
README
[root@centos7 my-project]# git checkout -- docs/changelogs
[root@centos7 my-project]# git checkout -- test.html
[root@centos7 my-project]# ls
docs  README  test.html
[root@centos7 my-project]# git status
# 位于分支 master
无文件要提交,干净的工作区

给提交打上标签git tag。
使用git log查看提交记录,然后把某个commit提交打上标签。

[root@centos7 my-project]# git log
commit aae8e168427dfb063c4dd50b4f155e60d75aab1e
Author: root <root@example.com>
Date:   Tue Jun 9 19:33:43 2020 +0800

    Initial Version
    Version 0.1
[root@centos7 my-project]# git tag v0.1 aae8e1
[root@centos7 my-project]# git tag -l
v0.1

后面若想切换到某个提交,使用git checkout <TAG>即可。

[root@centos7 my-project]# git checkout v0.1
Note: checking out 'v0.1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD 目前位于 aae8e16... Initial Version Version 0.1

此时修改README文件,然后再次添加至暂存区

[root@centos7 my-project]# vim README 

Project: test project
Version: v 0.1
new line

[root@centos7 my-project]# git add README
        

然后切换到v0.1的提交上来,此时会提示README这个文件发生了改变。

[root@centos7 my-project]# git checkout v0.1
M	README
HEAD 目前位于 aae8e16... Initial Version Version 0.1
[root@centos7 my-project]# git status
# 头指针分离于 v0.1
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	修改:      README
#

再次git add添加到暂存区,并git commit提交。

[root@centos7 my-project]# git add .
[root@centos7 my-project]# git commit -m "version 0.2"
[分离头指针 c4cee72] version 0.2
 1 file changed, 1 insertion(+)
[root@centos7 my-project]# git log 
commit c4cee72dd0aea7baf3210dd6e7016cb449ee2df0
Author: root <root@example.com>
Date:   Tue Jun 9 21:23:18 2020 +0800

    version 0.2

commit aae8e168427dfb063c4dd50b4f155e60d75aab1e
Author: root <root@example.com>
Date:   Tue Jun 9 19:33:43 2020 +0800

    Initial Version
    Version 0.1

加上标签

[root@centos7 my-project]# git tag v0.2 c4cee72
[root@centos7 my-project]# git tag -l
v0.1
v0.2

再次切换到v0.1的提交上来,发现没有了第三行

[root@centos7 my-project]# git checkout v0.1
之前的 HEAD 位置是 c4cee72... version 0.2
HEAD 目前位于 aae8e16... Initial Version Version 0.1
[root@centos7 my-project]# cat README 
Project: test project
Version: v 0.1

有切换到v0.2的提交上来

[root@centos7 my-project]# git checkout v0.2
之前的 HEAD 位置是 aae8e16... Initial Version Version 0.1
HEAD 目前位于 c4cee72... version 0.2
[root@centos7 my-project]# cat README 
Project: test project
Version: v 0.1
new line

创建分支git branch或git checkout -b
列出当前分支

[root@centos7 my-project]# git branch --list
* (分离自 v0.2
  master

创建新的分支。

[root@centos7 my-project]# git branch dev
[root@centos7 my-project]# git branch --list
* (分离自 v0.2
  dev
  master

切换到新分支

[root@centos7 my-project]# git checkout dev
切换到分支 'dev'
[root@centos7 my-project]# git branch --list
* dev
  master

在dev创建一个新的提交

[root@centos7 my-project]# vim text.txt
[root@centos7 my-project]# ls
docs  README  test.html  text.txt
[root@centos7 my-project]# git add .
[root@centos7 my-project]# git commit -m "Version v0.2.1"
[dev 6d75e05] Version v0.2.1
 1 file changed, 1 insertion(+)
 create mode 100644 text.txt
[root@centos7 my-project]# git log
commit 6d75e05aa12fd69873e565d27b060add0f6a8b5c
Author: root <root@example.com>
Date:   Tue Jun 9 23:04:14 2020 +0800

    Version v0.2.1

commit c4cee72dd0aea7baf3210dd6e7016cb449ee2df0
Author: root <root@example.com>
Date:   Tue Jun 9 21:23:18 2020 +0800

    version 0.2

commit aae8e168427dfb063c4dd50b4f155e60d75aab1e
Author: root <root@example.com>
Date:   Tue Jun 9 19:33:43 2020 +0800

    Initial Version
    Version 0.1

然后切换到master分支上,进行分支合并(注意:分支合并时,如果产生冲突,还得手动解决)

[root@centos7 my-project]# git checkout master
切换到分支 'master'
[root@centos7 my-project]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@centos7 my-project]# git merge dev
更新 aae8e16..6d75e05
Fast-forward
 README   | 1 +
 text.txt | 1 +
 2 files changed, 2 insertions(+)
 create mode 100644 text.txt

分支合并之后master分支上就会产生新的提交

[root@centos7 my-project]# git log
commit 6d75e05aa12fd69873e565d27b060add0f6a8b5c
Author: root <root@example.com>
Date:   Tue Jun 9 23:04:14 2020 +0800

    Version v0.2.1

commit c4cee72dd0aea7baf3210dd6e7016cb449ee2df0
Author: root <root@example.com>
Date:   Tue Jun 9 21:23:18 2020 +0800

    version 0.2

commit aae8e168427dfb063c4dd50b4f155e60d75aab1e
Author: root <root@example.com>
Date:   Tue Jun 9 19:33:43 2020 +0800

    Initial Version
    Version 0.1
[root@centos7 my-project]# ls
docs  README  test.html  text.txt
[root@centos7 my-project]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@centos7 my-project]# cat README 
Project: test project
Version: v 0.1
new line

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值