Git使用1:Git基本命令
发布日期:2021-07-01 05:19:52 浏览次数:2 分类:技术文章

本文共 9595 字,大约阅读时间需要 31 分钟。

环境:Windows 10

一、Git基本命令

  • git init 初始化git仓库
  • git add 将工作目录下的文件添加到暂存区
  • git commit 将缓存区内容添加到仓库中。
  • git config 配置相关信息
  • git log 查看提交历史
  • git status 查看在你上次提交之后是否有修改。

二、Git的结构和状态

Git的3层结构
  • working directory 工作区
  • staging index 暂存区
  • git directory(Repository) 版本库
Git中文件的4种状态
  • Untracked 未被追踪
  • Modified 表示工作区修改了某个文件但是还没有添加到暂存区
  • Staged 表示把工作区修改的文件添加到了暂存区但是没有提交到版本库
  • Committed 表示数据被安全的存储在本地库中

三:Git基本命令介绍

git status

git status 以查看在你上次提交之后是否有修改。加 -s 参数,以获得简短的结果输出。

git log

在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看。


假设有一个Demo项目,目录结构如下:

└─Demo        index.html        style.css

git init

Git 使用 git init 命令来初始化一个 Git 仓库,Git 的很多命令都需要在 Git 的仓库中运行,所以 git init 是使用 Git 的第一个命令。

在执行完成 git init 命令后,Git 仓库会生成一个 .git 目录,该目录包含了资源的所有元数据,其他的项目目录保持不变,所有 Git 需要的数据和资源都存放在这个目录中。。

进入Demo目录,使用git init命令将Demo文件夹初始化为git仓库,以让git对这个文件夹进行版本控制

git init

或者使用指定目录作为Git仓库:

git init ~/Desktop/Demo

我们使用第一种方式实际操作一下:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo$ pwd/c/Users/ONEFINE/Desktop/DemoONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo$ ls -altotal 12drwxr-xr-x 1 ONEFINE 197609 0 2月   8 09:03 ./drwxr-xr-x 1 ONEFINE 197609 0 2月   8 08:50 ../-rw-r--r-- 1 ONEFINE 197609 0 2月   8 08:51 index.html-rw-r--r-- 1 ONEFINE 197609 0 2月   8 08:51 style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo$ git initInitialized empty Git repository in C:/Users/ONEFINE/Desktop/Demo/.git/ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ ls -altotal 16drwxr-xr-x 1 ONEFINE 197609 0 2月   8 09:04 ./drwxr-xr-x 1 ONEFINE 197609 0 2月   8 08:50 ../drwxr-xr-x 1 ONEFINE 197609 0 2月   8 09:04 .git/-rw-r--r-- 1 ONEFINE 197609 0 2月   8 08:51 index.html-rw-r--r-- 1 ONEFINE 197609 0 2月   8 08:51 style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

这样就初始化了一个空的git仓库。


git add

如果当前目录下有几个文件想要纳入版本控制,需要先用 git add 命令告诉 Git 开始对这些文件进行跟踪,然后提交:

  • git add filename 将指定文件添加到暂存区
  • git add . 将工作目录下的所有修改的文件添加到暂存区
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterNo commits yetUntracked files:  (use "git add 
..." to include in what will be committed) index.html style.cssnothing added to commit but untracked files present (use "git add" to track)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterNo commits yetChanges to be committed: (use "git rm --cached
..." to unstage) new file: index.htmlUntracked files: (use "git add
..." to include in what will be committed) style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

可以看到index.html变成了待提交状态(暂存区),而style.css依然处于未被跟踪的状态。

暂存区的文件提交到版本库

Git 为你的每一个提交都记录你的名字与电子邮箱地址,所以第一步需要配置用户名和邮箱地址:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git config --global user.name onefineONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git config --global user.email 188302531@qq.com# git config --listONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git config --listcore.symlinks=truecore.autocrlf=truecore.fscache=truecolor.diff=autocolor.status=autocolor.branch=autocolor.interactive=truehelp.format=htmlrebase.autosquash=truehttp.sslcainfo=D:/Git/mingw64/ssl/certs/ca-bundle.crthttp.sslbackend=openssldiff.astextplain.textconv=astextplainfilter.lfs.clean=git-lfs clean -- %ffilter.lfs.smudge=git-lfs smudge -- %ffilter.lfs.process=git-lfs filter-processfilter.lfs.required=truecredential.helper=managerfilter.lfs.clean=git-lfs clean -- %ffilter.lfs.smudge=git-lfs smudge -- %ffilter.lfs.process=git-lfs filter-processfilter.lfs.required=trueuser.name=onefineuser.email=188302531@qq.comcore.repositoryformatversion=0core.filemode=falsecore.bare=falsecore.logallrefupdates=truecore.ignorecase=true# 地址~/.gitconfigONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ cat ~/.gitconfig[filter "lfs"]        clean = git-lfs clean -- %f        smudge = git-lfs smudge -- %f        process = git-lfs filter-process        required = true[user]        name = onefine        email = 188302531@qq.comONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

git commit

使用git commit命令一次性将暂存区的文件提交到版本库

  • git commit -m 'description' 将暂存区的文件提交到版本库
  • git commit -am 'description' 跳过git add添加到暂存区命令,直接将工作区所有已跟踪的文件提交
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -m 'first commit'[master (root-commit) 039d4a6] first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ # 提交之后:ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterUntracked files:  (use "git add 
..." to include in what will be committed) style.cssnothing added to commit but untracked files present (use "git add" to track)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

使用git log命令查看提交的信息:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit 039d4a604b2d79d0da13779e791ba13eff1a6988 (HEAD -> master)Author: onefine <188302531@qq.com>Date:   Fri Feb 8 09:19:30 2019 +0800    first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

同样,对style.css文件:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterUntracked files:  (use "git add 
..." to include in what will be committed) style.cssnothing added to commit but untracked files present (use "git add" to track)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed: (use "git reset HEAD
..." to unstage) new file: style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -m 'add style.css'[master 13d63f2] add style.css 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 style.cssONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit 13d63f291269f18c663eed633a5651ed8d2ddd33 (HEAD -> master)Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:35:57 2019 +0800 add style.csscommit 039d4a604b2d79d0da13779e791ba13eff1a6988Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:19:30 2019 +0800 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

当项目中文件很多时,可以使用.。如

一次性将工作目录下的所有修改的文件添加到暂存区:

git add .

或者将目录下以 .c 结尾的文件提交到仓库中(或纳入版本控制或告诉 Git 开始对这些文件进行跟踪)。

git add *.c

接下来我们修改index.html文件,然后用git status命令查看:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$  vim index.html    
hello world

onefine

:wq!ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit: (use "git add
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: index.htmlno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

提示index.html已修改。

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git add index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges to be committed:  (use "git reset HEAD 
..." to unstage) modified: index.htmlONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -m 'modify index.html'[master e922fd7] modify index.html 1 file changed, 10 insertions(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit e922fd769b4127dba0682892a5aa0b1ccc72c383 (HEAD -> master)Author: onefine <188302531@qq.com>Date: Fri Feb 8 10:08:02 2019 +0800 modify index.htmlcommit 13d63f291269f18c663eed633a5651ed8d2ddd33Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:35:57 2019 +0800 add style.csscommit 039d4a604b2d79d0da13779e791ba13eff1a6988Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:19:30 2019 +0800 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

接下来我们修改style.css文件:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ vim style.css.{
}:wqONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

然而,这一次将使用命令git commit -am工作区的文件一次性直接提交到版本库中:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masterChanges not staged for commit:  (use "git add 
..." to update what will be committed) (use "git checkout --
..." to discard changes in working directory) modified: style.cssno changes added to commit (use "git add" and/or "git commit -a")ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git commit -am 'modify style.css'warning: LF will be replaced by CRLF in style.css.The file will have its original line endings in your working directory[master 1d6647a] modify style.css 1 file changed, 3 insertions(+)ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git statusOn branch masternothing to commit, working tree cleanONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$ git logcommit 1d6647a03772dc0f155040585117437b9714d1e4 (HEAD -> master)Author: onefine <188302531@qq.com>Date: Fri Feb 8 10:17:14 2019 +0800 modify style.csscommit e922fd769b4127dba0682892a5aa0b1ccc72c383Author: onefine <188302531@qq.com>Date: Fri Feb 8 10:08:02 2019 +0800 modify index.htmlcommit 13d63f291269f18c663eed633a5651ed8d2ddd33Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:35:57 2019 +0800 add style.csscommit 039d4a604b2d79d0da13779e791ba13eff1a6988Author: onefine <188302531@qq.com>Date: Fri Feb 8 09:19:30 2019 +0800 first commitONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)$

参考:

Git 教程


转载地址:https://onefine.blog.csdn.net/article/details/86773455 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Git使用2:Git撤销操作
下一篇:Git:版本控制工具简单使用

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月10日 20时11分59秒