Git - 入门到熟悉_Git基本概念与核心命令

Posted 小小工匠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Git - 入门到熟悉_Git基本概念与核心命令相关的知识,希望对你有一定的参考价值。

文章目录


Git概述

Git 是一个开源的分布式版本控制系统 (VCS),最初由 Linus Torvalds(Linux 的创建者)于 2005 年开发,是目前最流行和最常用的版本控制工具。


Git vs SVN

总的来说,主要有以下三点不同

  • 存储方式不一样
  • 使用方式不一样
  • 管理模式不一样

存储方式

GIT把内容按元数据方式存储类似k/v数据库,而SVN是按文件(新版svn已改成元数据存储)。

举个例子, 我们打开Git Bash ,演示一下 【元数据】的概念

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo
$ git init artisan     # 初始化一个名为artisan的项目 
Initialized empty Git repository in D:/opt/gitdemo/artisan/.git/

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo
$ cd artisan  # 进入到artisan目录 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ ls

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ echo 'artisan test' > readme.mf  # 写数据到 readme.mf文件 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ cat readme.mf  # 查看写入的内容 
artisan test

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ git hash-object -w readme.mf   # 刷到git的存储里 
warning: LF will be replaced by CRLF in readme.mf.
The file will have its original line endings in your working directory
ca30c25824b24fede390877fcddf0489eef76787

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ git cat-file -p ca30c25824b24fede390877fcddf0489eef76787  # 根据上一步返回的版本号查看提交的内容 (可以根据这个唯一串进行回滚)
artisan test

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ echo 'artisan test 2222' >> readme.mf  #  新写入数据到文件

 
artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ git hash-object -w readme.mf    #  刷入git存储 ,返回的值与上一个不同 
warning: LF will be replaced by CRLF in readme.mf.
The file will have its original line endings in your working directory
eef835b51d5b4d802765e88cfb19832c11de169b

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$ git cat-file -p eef835b51d5b4d802765e88cfb19832c11de169b   # 查看
artisan test
artisan test 2222

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/artisan (master)
$

而 SVN的存储方式 ,类似于

readme.mf.version1
readme.mf.version2
........

使用方式

从本地把文件推送远程服务

  • SVN只需要commit一步
  • GIT需要 add、commit、push 三个步骤

管理模式

  • SVN是一个远程集中式的管理系统
  • git 是一个分布式的版本管理系统

简言之, SVN服务器宕了以后,谁也别提交了。 Git则不然,本地不影响,待服务器好了以后再push 也是妥妥的。


另外,Git支持多个远程服务器地址, 而SVN只有一个地址。

大家试想一下 linus 搞的这个linux 操作系统,全世界都有开发维护者,如果全世界都用一个Remote Git Repository ,那不得疯掉… 多个Git 也便于权限等的独立管理…


Git 核心命令

1. GIT客户端安装配置


2. GIT的基本使用

2.1 git 项目创建等操作

#当前目录初始化为git 本地仓库
git init  <directory>


创建一个.git 目录。 其中


常用命令列表

下面我们来看下基本的命令

本地添加/删除

#添加指定文件至暂存区
git add <fileName>
#添加指定目录至暂存区
git add <directory>
#添加所有
git add -A
#将指定目录及子目录移除出暂存区
git rm --cached target -r
#添加勿略配置文件 .gitignore


本地提交

#提交至本地仓库
git commit file -m '提交评论'
#快捷提交至本地仓库
git commit -am '快添加与提交'

我们来个演示

git add 到 local cache

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo
$ cd gitProject/   # 进入到项目目录 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ pwd  # 查看当前路径
/d/opt/gitdemo/gitProject

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ echo 'artisan git demo ' > README.MF   # 创建文件

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status    # 查看文件的状态   Untracked files
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git add README.MF     #  添加到local cache 
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status   # 查看文件的状态, 发现文件状态变更为   to be committed 
On branch master

No commits yet

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


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git rm --cached README.MF   #  从local cache中删除文件  (本地文件其实不会删除)
rm 'README.MF'

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status   # 查看文件的状态  又变成了  Untracked files
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git add -A   # 添加全部的文件  
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status    #  查看文件状态   
On branch master

No commits yet

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


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$

截止到这里 add 部分演示完了。

git commit 到 local repository

接下来 看看 commit 部分的操作

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status
On branch master

No commits yet

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


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git commit README.MF  -m 'init commit'  # 提交到 local repository
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory
[master (root-commit) 4ac59bd] init commit
 1 file changed, 1 insertion(+)
 create mode 100644 README.MF

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git status  #  查看状态,发现本地已经木有待提交的文件了
On branch master 
nothing to commit, working tree clean

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$


git push 到 remote git repository

这个时候需要到 github or gitee or gitlab上新建一个项目

我们以gitee为例


看文档

简易的命令行入门教程:
Git 全局设置:
git config --global user.name "小工匠"
git config --global user.email "815150141@qq.com"




创建 git 仓库:
mkdir gitProject
cd gitProject
git init 
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/yangshangwei/gitProject.git
git push -u origin "master"



已有仓库?
cd existing_git_repo
git remote add origin https://gitee.com/yangshangwei/gitProject.git
git push -u origin "master"

我们刚才已经 commit 到local repository了,接下来就 根据提示

执行

git remote add origin https://gitee.com/yangshangwei/gitProject.git
git push -u origin "master"


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git remote add origin https://gitee.com/yangshangwei/gitProject.git   # 提交到远程git 仓库 

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git push -u origin "master"   # 提交master 分支  
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 232 bytes | 232.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/yangshangwei/gitProject.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$

输入用户名和密码

到remote git 查看


OK, 大功告成。

这一部分我们也讲解完了。


2.2 git项目的克隆

#基于远程仓库克隆至本地
git clone <remote_url>

演示一把

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo
$ git clone https://gitee.com/yangshangwei/gitProject.git  gitProject2
Cloning into 'gitProject2'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 212 bytes | 30.00 KiB/s, done.

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo
$ cd gitProject2/

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject2 (master)
$ ls
README.MF


2.3 文件提交与推送

修改一下,然后重新push ,演示一把


artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject2 (master)
$ echo 'new modify content ' >> README.MF  # 追加内容  

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject2 (master)
$ git status   # 查看状态
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.MF

no changes added to commit (use "git add" and/or "git commit -a")

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject2 (master)
$ git add -A  ; git commit -am '第二次提交' ; git push   #  连写  
warning: LF will be replaced by CRLF in README.MF.
The file will have its original line endings in your working directory
[master 5e6d25b] 绗簩娆℃彁浜? 1 file changed, 1 insertion(+)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 301 bytes | 301.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/yangshangwei/gitProject.git
   4ac59bd..5e6d25b  master -> master

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject2 (master)
$

到gitee上去查看

我们回到 gitProject 这个工程,重新git pull 获取最新代码

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ git pull   # 获取最新代码 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 281 bytes | 17.00 KiB/s, done.
From https://gitee.com/yangshangwei/gitProject
   4ac59bd..5e6d25b  master     -> origin/master
Updating 4ac59bd..5e6d25b
Fast-forward
 README.MF | 1 +
 1 file changed, 1 insertion(+)

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ pwd
/d/opt/gitdemo/gitProject

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ ls
README.MF

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$ cat README.MF
artisan git demo
new modify content

artisan@LAPTOP-JF3RBRRJ MINGW64 /d/opt/gitdemo/gitProject (master)
$

成功

以上是关于Git - 入门到熟悉_Git基本概念与核心命令的主要内容,如果未能解决你的问题,请参考以下文章

Git - 入门到熟悉_日志管理

Git - 入门到熟悉_日志管理

Git - 入门到熟悉_远程仓库管理

Git - 入门到熟悉_远程仓库管理

Git - 入门到熟悉_TAG管理

Git - 入门到熟悉_TAG管理