• Git常用命令大全


    一、Git是什么?

    Git是目前世界上最先进的分布式版本控制系统。
    工作原理 / 流程:
    在这里插入图片描述
    Workspace:工作区
    Index / Stage:暂存区
    Repository:仓库区(或本地仓库)
    Remote:远程仓库

    二、SVN与Git的最主要的区别?

    SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以首先要从中央服务器哪里得到最新的版本,然后干活,干完后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,如果在局域网还可以,带宽够大,速度够快,如果在互联网下,如果网速慢的话,就纳闷了。
    Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,这样,工作的时候就不需要联网了,因为版本都是在自己的电脑上。既然每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

    三、Git的安装

    官方地址:**https://git-scm.com/

    在这里插入图片描述
    选择下载64-bit Git for Windows Setup在这里插入图片描述
    Git Bash:Unix与Linux风格的命令行,使用最多,推荐最多
    安装后配置用户名邮箱(必要)

    > git config --global user.name "yxm"   #配置用户名
    > git config --global user.email "yxm@163.com"   #配置邮箱
    
    • 1
    • 2

    查询用户名邮箱

    > git config --system --list   #查询全局配置信息
    > git config --global user.name  #查看用户名
    > git config --global user.email #查看邮箱
    
    • 1
    • 2
    • 3

    四:Bash基本操作命令

    • cd 改变目录。
    • cd … 回退到上一个目录。
    • pwd 显示当前所在的目录路径。
    • ls(ll) 都是列出当前目录中的所有文件,只不过ll(两个ll)列出的内容更为详细。
    • touch 新建一个文件 如 touch index.js 就会在当前目录下新建一个index.js文件。
    • rm 删除一个文件, rm index.js 就会把index.js文件删除。
    • mkdir 新建一个目录,就是新建一个文件夹。
    • rm -r 删除一个文件夹, rm -r src 删除src目录
    • mv 移动文件, mv index.html src index.html 是我们要移动的文件, src 是目标文件夹,当然, 这样写,必须保证文件和目标文件夹在同一目录下。
    • reset 重新初始化终端/清屏。
    • clear 清屏。
    • history 查看命令历史。
    • help 帮助。
    • exit 退出。
    • cat 显示文件内容

    五、Git理论基础

    git管理的文件有三种状态:

    已提交(committed)、已修改(modified) 和 已暂存(staged)。

    • 已修改表示修改了文件,但还没保存到数据库中。
    • 已暂存表示对一个已修改文件的当前版本做了标记,使之包含在下次提交的快照中。
    • 已提交表示数据已经安全地保存在本地数据库中。

    工作区、暂存区以及 Git 仓库目录

    工作区域:就是你平时存放项目代码的地方
    暂存区:保存了下次将要提交的文件列表信息,一般在 Git 仓库目录中。
    仓库区(或本地仓库):就是安全存放数据的位置,这里面有你提交到所有版本的数据。

    六:Git操作

    获得Git仓库

    创建本地仓库的方法有两种:一种是创建全新的仓库,另一种是克隆远程仓库。
    创建全新仓库 需要在管理的项目根目录执行,执行成功后会出现.git目录

    git init
    
    • 1

    克隆远程仓库

    git clone [url]
    
    • 1

    例如:

    git clone https://gitee.com/yangxumin/js.git
    
    • 1

    Git文件操作

    文件4种状态

    • Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.

    • Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件

    • Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改

    • Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified

    查看文件状态

    git status [filename]
    
    • 1

    创建一个文件并查看状态

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ touch readme.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ echo hello yxm > readme.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat readme.txt
    hello yxm
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git status readme.txt
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add ..." to include in what will be committed)
            readme.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    文件状态Untracked files:未跟踪,说明文件没有参与版本控制,只是存放在文件夹中

    将文件添加到暂存区

    git add 文件名   #添加单个文件
    git add .       #添加所有文件
    git add readme.txt
    
    • 1
    • 2
    • 3

    移除文件与目录(撤销add)

    git rm --cached 文件名    #直接从暂存区删除文件,工作区则不做出改变
    git rm -f 文件名          #不但从暂存中删除,同时删除物理文件
    rm 文件名                 #删除文件
    
    • 1
    • 2
    • 3

    查看文件修改后的差异(显示工作区中的文件和暂存区文件的差异)

    git diff 文件名
    
    • 1

    工作区创建b.txt文件,提交到暂存区,修改工作区b.txt文件的内容,查询文件修改后的差异

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ touch b.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ echo hello 123 >b.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git add b.txt
    warning: in the working copy of 'b.txt', LF will be replaced by CRLF the next time Git touches it
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ echo hello 456 >b.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git diff b.txt
    warning: in the working copy of 'b.txt', LF will be replaced by CRLF the next time Git touches it
    diff --git a/b.txt b/b.txt
    index 257299b..3a3c3e2 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -1 +1 @@
    -hello 123
    +hello 456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    签出

    如果仓库中已经存在文件b.txt,在工作区中对b.txt修改了,如果想撤销可以使用checkout,签出覆盖

    签出命令git checkout是git最常用的命令之一,同时也是一个很危险的命令,因为这条命令会重写工作区

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git checkout b.txt
    Updated 1 path from the index
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat b.txt
    hello 123
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    提交

    通过add只是将文件或目录添加到了index暂存区,使用commit可以实现将暂存区的文件提交到本地仓库。

    git commit 文件名 -m "注释信息"  #提交单个文件
    git commit . -m "注释信息"      #提交所有文件
    
    • 1
    • 2
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git commit b.txt -m "first commit"
    [master (root-commit) 0cf58fb] first commit
     1 file changed, 1 insertion(+)
     create mode 100644 b.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git status b.txt
    On branch master
    nothing to commit, working tree clean
    、、、
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看提交日志

    git log
    
    • 1
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git log
    commit 7142a00af8d147747f7031a3712fc51b1ecac688 (HEAD -> master)
    Author: yxm <yxm3568@163.com>
    Date:   Thu Sep 8 15:23:00 2022 +0800
    
        yxmcommit
    
    commit 0cf58fb6dead7aec39f732df3810a94a8dcb5676
    Author: yxm <yxm3568@163.com>
    Date:   Thu Sep 8 15:20:29 2022 +0800
    
        first commit
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    撤销提交、版本回退

    git reset --hard HEAD~1
    
    • 1

    在这里插入图片描述
    创建r.txt,文件,写入内容111第一次提交,修改r.txt文件,写入内容222添加222后提交,修改r.txt文件。写入333后提交,查看提示,回退。

    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ touch r.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ echo 111 >r.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git add r.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git commit r.txt -m 'r.txt submit'
    [master a762292] r.txt submit
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 r.txt
    
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git add r.txt
    warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git commit r.txt -m "增加222提交"
    warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
    [master c31134c] 增加222提交
     1 file changed, 2 insertions(+), 1 deletion(-)
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ echo 333 >> r.txt
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git add r.txt
    warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git commit r.txt -m "增加333提交"
    warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
    [master 371c99a] 增加333提交
     1 file changed, 1 insertion(+)
    
    
    • 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

    使用git log查看提交日志
    在这里插入图片描述
    r.txt文件内容为
    在这里插入图片描述
    回退到上一个版本222添加

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git reset --hard HEAD^
    HEAD is now at c31134c 增加222提交
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat r.txt
    111
    222
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    再恢复回退到最新的版本到添加333
    使用命令方法如下:
    git reset --hard 版本号

    查看版本号
    git reflog

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git reflog
    c31134c (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
    371c99a HEAD@{1}: commit: 增加333提交
    c31134c (HEAD -> master) HEAD@{2}: commit: 增加222提交
    b6f59bf HEAD@{3}: commit: r.txt submit
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git reset --hard 371c99a
    HEAD is now at 371c99a 增加333提交
    
    • 1
    • 2
    • 3

    再次查看r.txt内容

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat r.txt
    111
    222
    333
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Git分支

    git branch                  #列出所有分支
    git checkout -b [分支名字]   #创建一个分支并切换到该分支
    git merge [分支名字]          # 合并指定分支到当前分支
    git branch -d [分支名字]  # 删除分支
      
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建dev分支,然后切换到dev分支上

     git checkout -b dev
    
    • 1

    在dev分支操作r.txt,合并到master主分支

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
    $ cat r.txt
    111
    222
    333
    444
    555
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
    $ git add r.txt
    warning: in the working copy of 'r.txt', LF will be replaced by CRLF the next time Git touches it
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
    $ git commit -m "dev branch"
    [dev 4f3b785] dev branch
     1 file changed, 2 insertions(+)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    切换主分支并合并

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (dev)
    $ git checkout master
    Switched to branch 'master'
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat r.txt
    111
    222
    333
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git merge dev
    Updating cf7e35d..4f3b785
    Fast-forward
     r.txt | 2 ++
     1 file changed, 2 insertions(+)
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ cat r.txt
    111
    222
    333
    444
    555
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    删除dev分支

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git branch -d dev
    Deleted branch dev (was 4f3b785).
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit (master)
    $ git branch
    * master
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    七、远程仓库

    托管平台

    Git代码托管平台,首先推荐的是GitHub,好多好的开源项目都来自GitHub,但是GitHub只能新建公开的Git仓库,私有仓库要收费,有时候访问比较卡,如果你做的是一个开源项目,可以首选GitHub。下面推荐几个比较好的Git代码托管平台:

    • GitHub 地址: https://github.com/
    • Gitlab 地址:https://about.gitlab.com/
    • 开源中国代码托管开 地址:http://git.oschina.net/
    • coding.net 地址: https://coding.net/
    • Gitee 地址:https://gitee.com/

    本地Git仓库推送到Gitee远程仓库

    在这里插入图片描述
    已存在仓库

     git remote add origin https://gitee.com/yangxumin/test.git
     git push -u origin "master"
    
    • 1
    • 2

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

    yxm@USER-20200712WE MINGW64 /d/yxm/testgit2
    $ git init
    Initialized empty Git repository in D:/yxm/testgit2/.git/
    
    yxm@USER-20200712WE MINGW64 /d/yxm/testgit2 (master)
    $ git pull https://gitee.com/yangxumin/test.git
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

  • 相关阅读:
    基于微信小程序电影交流平台源码成品(微信小程序毕业设计)
    Pod和容器设计模式
    华为云服务器安装Linux并实现本地连接访问
    hexo+github手把手教你部署个人博客
    步入TypeScript中的其它类型,js要认怂了吗
    C++ Builder 6.0 消息重载的处理
    基于Python的网络爬虫开发与实现
    超好用的数据库检索工具介绍——Bean Searcher
    BOOST学习:BOOST_FOREACH+boost::assign处理正则表达式(特殊字符$需要单独处理)
    栈和队列的应用 —— 循环队列
  • 原文地址:https://blog.csdn.net/weixin_44226883/article/details/120202441