Git基本用法1

本文提供Git的一些基本操作。

预备阶段

下载和安装Git

在Git test文件夹中新建两个文件web1.html和web2.html用于测试


web1.html


web2.html

创建本地仓库

在git bash中打开到项目文件夹(Git test),使用git init初始化当前文件夹为本地仓库

hp@pc MINGW64 ~

$ cd ~/Desktop/Blog/Git\ test/


hp@pc MINGW64 ~/Desktop/Blog/Git test
$ ls
web1.html  web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test
$ git init
Initialized empty Git repository in C:/Users/hp/Desktop/Blog/Git test/.git/

hp@pc MINGW64 ~/Desktop/Blog/Git test
$ ls
web1.html  web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test
$ git init
Initialized empty Git repository in C:/Users/hp/Desktop/Blog/Git test/.git/

查看当前状态

使用git status查看当前项目状态,特别是文件修改情况

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master


No commits yet


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


        web1.html
        web2.html


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

添加文件到缓存区

使用git add filename添加指定文件到缓存,包括新增的文件,被修改的文件和被删除的文件

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web1.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master

No commits yet

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

        new file:   web1.html

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

        web2.html


hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$

此时文件web1.html已经添加到缓存,显示changes to committed可以提交。文件web2.html没有添加到缓存中,仍然属于untracked file

使用git add . 可以将当前文件夹下所有文件提交到缓存

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add .

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master

No commits yet

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

        new file:   web1.html
        new file:   web2.html


hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$


提交到仓库

提交前需要先指定当前的用户名和邮箱,用于后续日志中记录是谁提交的

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git config --global user.name 'Jason'

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git config --global user.email test@123.com

使用git commit可以将当前缓存区的内容提交到仓库, -m代表为当前提交添加信息,类似备注

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git commit -m 'Version one'
[master (root-commit) 0ed3e44] Version one
 2 files changed, 10 insertions(+)
 create mode 100644 web1.html
 create mode 100644 web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
nothing to commit, working tree clean

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$


查看日志

使用git log可以看历史的提交信息

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git log
commit 0ed3e441adc41f2edfe14e4361af84134c4ececc (HEAD -> master)
Author: Jason <test@123.com>
Date:   Wed Mar 14 01:08:48 2018 -0700

    Version one

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$

下一篇:Git基本操作2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值