软件介绍

git 知识汇总——文件操作

git为我们的项目提供分布式版本控制系统,创建一个project目录,了解常用的git文件操作命令。

1. git init
创建project项目空目录,并初始化git。

root [~] mkdir project
root [~] cd project/
root [~] git init
Initialized empty Git repository in /root/project/.git/

2. git status
列出当前目录所有还没有被git管理的文件和被git管理且被修改但还未提交的文件,注意空文件夹git是会忽略的。

root [~] mkdir empty_folder
root [~] git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
root [~] touch empty_file
root [~] git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       empty_file
nothing added to commit but untracked files present (use "git add" to track)

3. .gitkeep文件
这是git最初设计时候导致的,大家觉得不需要解决。不过对于刚创建一个项目框架,可能会有空目录的需求,解决办法是,在空目录下放置.gitkeep文件。

root [~] touch empty_folder/.gitkeep
root [~] git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       empty_file
#       empty_folder/
nothing added to commit but untracked files present (use "git add" to track)

4. .gitignore文件
有时候我们想忽略一些特定的文件提交,可以通过编辑.gitignore文件,它的规则很简单:
/ : 表示目录
* : 通配多个字符
? : 通配单个字符
[] : 包含单个字符的匹配列表
! : 表示不忽略匹配到的文件或目录
举例:

root [~] cat .gitignore
/*       # 忽略根目录下所有内容,可以不加符号
!/empty  # 但根目录下empty开头的除外
*.txt    # 忽略所有txt后缀文件
build/   # 忽略build/ 目录下的所有文件
me/*.md  # 会忽略me/info.md,但不包括me/copy/info.md

5. git add
添加文件到仓库,这个时候并没有真正提交,我们可以分批添加多个文件。

root [~] git add empty_file
root [~] git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   empty_file
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       empty_folder/
root [~] git add ./*
root [~] git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   empty_file
#       new file:   empty_folder/.gitkeep
#

6. git commit
把所有git文件操作最终提交到仓库,需要注意的是这个命令必须加-m,-m参数表示每次提交的备注,良好的编程规范,每次都会写清楚备注。

root [~] git commit -m 'my first git commit'
[master (root-commit) 3194857] my first git commit
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 empty_file
 create mode 100644 empty_folder/.gitkeep

7. git rm
删除文件,和linux的rm不同,git rm是删除git仓库的文件。也就是说,如果一个文件已经提交到git仓库了,这个时候如果用linux的rm删除该文件,这个文件并没有在git中删除,我们必须使用git rm才能从仓库进行删除。

root [~] rm empty_file
root [~] git status
# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    empty_file
#
no changes added to commit (use "git add" and/or "git commit -a")
root [~] git rm empty_file
rm 'empty_file'
root [~] git st
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    empty_file
#

另外,对于git的文件操作,如果没有commit,那么仓库就没有变化。所以,当我们在提交之前,可以取消之前的文件操作,只需要使用git rm --cached命令。

root [~] touch err.txt
root [~] git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       err.txt
nothing added to commit but untracked files present (use "git add" to track)
root [~] git add err.txt
root [~] git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   err.txt
#
root [~] git rm -r --cached err.txt
rm 'err.txt'
root [~] git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       err.txt
nothing added to commit but untracked files present (use "git add" to track)

8. git diff
git提供了查看版本之间区别的命令,这里提供两个case。通常我们会使用git status来查看改动,并用git diff查看具体的改动信息。
git diff filename:提交到缓存前的文件和仓库比较。
git diff --cached:提交到缓存的文件和仓库比较。

root [~] echo 'hello world' > a.txt
root [~] git add a.txt
root [~] git commit a.txt -m'just for test'
[master 038c1a2] just for test
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 a.txt
root [~] echo 'Hello World' > a.txt
root [~] git diff a.txt
diff --git a/a.txt b/a.txt
index 3b18e51..557db03 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-hello world
+Hello World
root [~] git diff --cached
root [~] git add a.txt
root [~] git diff --cached
diff --git a/a.txt b/a.txt
index 3b18e51..557db03 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1 @@
-hello world
+Hello World

1 Comment

  1. 你根本不会玩

Leave a Reply

黑ICP备15001596号