2、初步学习Git基本命令

1、下载安装Git

  • 在Git官网下载 下载Git
  • 安装好后,按win+R 输入cmd,打开命令行,输入git,出现以下命令,则完成安装


    完成Git安装.jpg

2、基本命令

2.1 全局配置用户名、邮箱:

在桌面右击选择Git Bash Here


Git Bash.jpg

输入:

  • git config --global user.name "Your Name"
  • git config --global user.email "Your Email"


    配置用户名、邮箱.jpg

2.2 初始化仓库

  • 非系统非中文路径下,建一个learngit文件夹


    新建learngit文件夹.jpg
  • 通过控制台命令行进入该文件夹,并用git init 命令初始化仓库


    初始化仓库.jpg

2.3 向仓库中添加文件

  • 在learngit文件夹中新建一个README.md文件


    新建README文件.jpg
  • 添加“个人简介”内容
#   1.个人简介
##  1.1基本情况
##  1.2联系方式
  • 通过git add将文件添加到仓库
D:\learngit>git add README.md
  • 通过git commit -m "说明文字" 命令 将文件提交到仓库
D:\learngit>git commit -m "Personal profile"
[master (root-commit) 8a40766] Personal profile
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
D:\learngit>

2.4 修改文件

  • 在README.md文件中添加“教育经历”
#   1.个人简介
##  1.1基本情况
##  1.2联系方式

#   2.教育经历
##  2.1学历教育
##  2.2继续教育
  • 通过 git add 和 git commit 命令添加并提交到仓库中去
D:\learngit>git add README.md
D:\learngit>git commit -m "Educational experience"
[master (root-commit) 8a40766] Personal profile
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
D:\learngit>

先添加后提交!!!

2.5 查看当前仓库状态

  • 通过git status 命令查看当前仓库状态(是否有待提交的文件)
    若无待提交文件,则出现以下情况:
D:\learngit>git status
On branch master
nothing to commit, working tree clean

2.6 查看版本历史记录

  • 通过git log命令查看历史记录
D:\learngit>git log
commit 8a407662d739f3cade254dcd504016b56545799e (HEAD -> master)
Author: Invader <1292689963@qq.com>
Date:   Fri Mar 1 08:55:19 2019 +0800

    Educational experience
commit 2b2224963f65e96f36cdcd53654fda25e69697e43
Author: Invader <1292689963@qq.com>
Date:   Fri Mar 1 08:53:36 2019 +0800

    Personal profile
  • 通过git log --pretty=oneline 命令单行显示记录
D:\learngit>git log --pretty=oneline
D:\learngit>git log --pretty=oneline
8a407662d739f3cade254dcd504016b56545799e (HEAD -> master) Educational experienxce
2b2224963f65e96f36cdcd53654fda25e69697e43 Personal profile

8a407662d739f3cade254dcd504016b56545799e : commit id

2.7穿越到指定版本

  • 通过git reset --hard commit_id 命令穿越到指定版本(commit_id较长,只写前几位就行,Git会自动寻找到的)
D:\learngit>git reset --hard 8a407662
HEAD is now at 8a40766 Personal profile
  • 打开README.md文件,如图


    TIM截图20190301091914.jpg
  • 点击是,恢复第一个版本

2.8 多个文件同时提交

  • 在learngit文件夹中新建文本文档log.txt,图片me.jpg


    新建多个文件jpg
  • 先后添加log.txt 、me.jpg文件
D:\learngit>git add log.txt

D:\learngit>git add me.jpg
  • 同时提交两个文件
D:\learngit>git commit -m "add log.txt and me.jpg files"
[master f6e6945] add log.txt and me.jpg files
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 log.txt
 create mode 100644 me.jpg
  • 再次查看版本信息
D:\learngit>git log --pretty=oneline
f6e6945d4ead3261589d59e51d8c23b433528dfc (HEAD -> master) add log.txt and me.jpg files
2b2224963f65e96f36cdcd53654fda25e69697e43 Educational experience
8a407662d739f3cade254dcd504016b56545799e Personal profile

3、课后小结

  • git:查看系统有没有安装Git
  • 设置全局属性,用户名和邮箱
    git config --global user.name "Your Name"
    git config --global user.email "Your email"
  • git init:初始化仓库
  • git add 文件名:将文件添加到仓库
  • git commit -m “说明文字”:把文件提交到仓库 (必须先add再commit !!!)
  • git status:查看当前仓库状态
  • git log:查看版本记录
    git log --pretty=online:单行显示
  • git reset --hard commit_id:穿越到指定版本
  • git reset --hard HEAD:回退到上一版本
  • git rm 文件名:删除文件

git config core.autocrlf true

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容