git 完整使用整理
创建版本库
使用 `git init ` 进行初始化
$ git init
Initialized empty Git repository in E:/gitTUT/.git/
$ ls -alh
total 32K
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:46 ./
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:44 ../
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:46 .git/
查看状态
使用`git status` 查看状态,untracked, 表示未添加、提交
$ ls -alh
total 32K
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:51 ./
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:44 ../
drwxr-xr-x 1 Administrator 197121 0 八月 2 11:46 .git/
-rw-r--r-- 1 Administrator 197121 0 八月 2 11:51 hello.txt
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
上面表示文件夹中的文件未进行添加提交。
添加文件
使用`git add file or folder` 添加某个文件或是将某个文件夹中的未添加的文件全部添加。
$ git add hello.txt
$ git add .
提交改变
使用`git commit -m 'messages' ` 提交此次改变,其中 -m 是添加的附加提示信息。
$ git commit -m 'some message about this change here '
[master (root-commit) d1816f2] some change
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 hello.txt
create mode 100644 hello2.txt
基本流程:
好,上面是一些基本的操作使用,通常在工作学习中会经常用到下面的一个操作。
查看日志记录:
使用`git log` 会将近期做的一些操作记录下来, 记录包括操作者,日期,操作提示信息, 操作ID等。
$ git log
commit d1816f21677d1b68b9c27ff447a8e7a222916e4b
Author: hpulfc <hpulfc@yeah.net>
Date: Thu Aug 2 11:59:51 2018 +0800
some change
文件未添加时的比较:(unstaged)
使用`git diff` 将会将没有add的一些改变和上一次commit之后的进行比较,显示一些文件的修改内容增加,删除等!
$ git diff
diff --git a/hello2.txt b/hello2.txt
index e69de29..eebdf76 100644
--- a/hello2.txt
+++ b/hello2.txt
@@ -0,0 +1,2 @@
+insert 222
+
warning: LF will be replaced by CRLF in hello2.txt.
文件已添加,未提交的修改:(staged)
$ git diff --cached
diff --git a/hello2.txt b/hello2.txt
index e69de29..2c9b04a 100644
--- a/hello2.txt
+++ b/hello2.txt
@@ -0,0 +1,2 @@
+insert 222
+c=1
注意,git diff 可以查看 add过和没 add 过的不同!
上面这部分主要是对于一些修改的查看等操作。接下来是一些修改的操作。
合并提交:(主要是 漏了提交一些文件时的操作)
$ git commit -amend -no-edit
# 这样会把此次提交合并到上次提交里面去。
撤回add:(主要是在还没提交时的 但是却add了,又改错了文件,这时可以重置某个文件的add, 返回到unstaged 状态)
$ git reset hello2.txt
Unstaged changes after reset:
M hello2.txt
reset到commit之前:
通过`git log` 可以看到每次提交的信息,其中包括提交的ID,这时可以使用`git reset --hard commit_ID ` 就可以了,
如果需要返回到上次提交之前,可以简写为 `git reset --hard HEAD`
如果需要对单个文件 回到提交之前,并进行改写提交,可使用 checkout。 `git checkout commit_id -- 文件名` 就可以了,并不会撤销整个提交。
以上是一些基本的操作,接下来,下一篇会主要记录一些分支管理的内容,见链接: