Git使用1:Git基本命令

环境: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/Demo

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo
$ ls -al
total 12
drwxr-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.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo
$ git init
Initialized empty Git repository in C:/Users/ONEFINE/Desktop/Demo/.git/

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ ls -al
total 16
drwxr-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.css

ONEFINE@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 status
On branch master

No commits yet

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

        index.html
        style.css

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git add index.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master

No commits yet

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

        new file:   index.html

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

        style.css


ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

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

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

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git config --global user.name onefine

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git config --global user.email 188302531@qq.com

# git config --list
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git config --list
core.symlinks=true
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslcainfo=D:/Git/mingw64/ssl/certs/ca-bundle.crt
http.sslbackend=openssl
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
credential.helper=manager
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
user.name=onefine
user.email=188302531@qq.com
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.ignorecase=true

# 地址~/.gitconfig
ONEFINE@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.com

ONEFINE@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.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ 

# 提交之后:
ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        style.css

nothing 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 log
commit 039d4a604b2d79d0da13779e791ba13eff1a6988 (HEAD -> master)
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:19:30 2019 +0800

    first commit

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

同样,对style.css文件:

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        style.css

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git add style.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   style.css


ONEFINE@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.css

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git log
commit 13d63f291269f18c663eed633a5651ed8d2ddd33 (HEAD -> master)
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:35:57 2019 +0800

    add style.css

commit 039d4a604b2d79d0da13779e791ba13eff1a6988
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:19:30 2019 +0800

    first commit

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
nothing to commit, working tree clean

ONEFINE@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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello world</title>
</head>
<body>
    <p>onefine</p>
</body>
</html>
:wq!

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   index.html

no 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.html

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   index.html


ONEFINE@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 status
On branch master
nothing to commit, working tree clean

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git log
commit e922fd769b4127dba0682892a5aa0b1ccc72c383 (HEAD -> master)
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 10:08:02 2019 +0800

    modify index.html

commit 13d63f291269f18c663eed633a5651ed8d2ddd33
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:35:57 2019 +0800

    add style.css

commit 039d4a604b2d79d0da13779e791ba13eff1a6988
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:19:30 2019 +0800

    first commit

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

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

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

.{

}
:wq

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

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

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   style.css

no 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 status
On branch master
nothing to commit, working tree clean

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$ git log
commit 1d6647a03772dc0f155040585117437b9714d1e4 (HEAD -> master)
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 10:17:14 2019 +0800

    modify style.css

commit e922fd769b4127dba0682892a5aa0b1ccc72c383
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 10:08:02 2019 +0800

    modify index.html

commit 13d63f291269f18c663eed633a5651ed8d2ddd33
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:35:57 2019 +0800

    add style.css

commit 039d4a604b2d79d0da13779e791ba13eff1a6988
Author: onefine <188302531@qq.com>
Date:   Fri Feb 8 09:19:30 2019 +0800

    first commit

ONEFINE@ONE-FINE MINGW64 ~/Desktop/Demo (master)
$

参考:
Git 教程 http://www.runoob.com/git/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值