您的位置:首页 > 其它

Git基本用法1

2018-03-15 02:24 239 查看
本文提供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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: