Skip to content

Git命令-文件操作

1. 添加文件

主要指的是从工作目录向暂存区添加文件,语法:git add [选项] <文件>...

  • -n--dry-run:显示将要添加的文件,但不实际执行添加。
  • -v--verbose:在命令行中显示被添加的文件。
  • -p--patch:以补丁形式交互式添加选择的更新,允许你选择性地将部分更改加入暂存区。

1.1 添加指定的文件

创建a.txt文件,再次查看仓库状态:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "xixi" >> a.txt
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git status
On branch master

No commits yet

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

nothing added to commit but untracked files present (use "git add" to track)
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add a.txt
warning: in the working copy of 'a.txt', LF will be replaced by CRLF the next time Git touches it

1.2 添加所有更改的文件

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "1111" > 1.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "22222" > 2.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add .
warning: in the working copy of '1.txt', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of '2.txt', LF will be replaced by CRLF the next time Git touches it

1.3 添加特定类型的文件

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "select 1" > demo.sql

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "1,2,3" > data.csv

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add *.sql
warning: in the working copy of 'demo.sql', LF will be replaced by CRLF the next time Git touches it

1.4 添加整个目录

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ mkdir source

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add source

1.5 添加文件且显示详细信息

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add -v data.csv
warning: in the working copy of 'data.csv', LF will be replaced by CRLF the next time Git touches it
add 'data.csv'

2. 删除暂存区文件

基本语法: git rm --cached <file>...
查看当前暂存区所有的文件:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   1.txt
        new file:   2.txt
        new file:   a.txt
        new file:   data.csv
        new file:   demo.sql

删除暂存区1.txt:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   2.txt
        new file:   a.txt
        new file:   data.csv
        new file:   demo.sql

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

3. 文件提交

3.1 基本语法

git commit -m "提交信息"
用于将当前工作目录中的更改提交到本地代码库中。每次提交都会创建一个新的版本,便于代码的历史记录管理。
提交暂存区的文件:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git commit -m "提交a.txt"
[master (root-commit) 44d2043] 提交a.txt
 2 files changed, 3 insertions(+)
 create mode 100644 2.txt
 create mode 100644 a.txt
 mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git commit -v  -m "提交demo.sql"
[master 42cb7b0] 提交demo.sql
 1 file changed, 1 insertion(+)
 create mode 100644 demo.sql

3.2 修改文件

当文件修改后,查看暂存区状态:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ echo "heheh" >> 2.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   2.txt

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

no changes added to commit (use "git add" and/or "git commit -a")

git会检测到工作区有文件被修改,将修改的文件再次添加到暂存区:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add 2.txt
warning: in the working copy of '2.txt', LF will be replaced by CRLF the next time Git touches it

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git commit -m "Issue#1231 修改2.txt"
[master 9c45bae] Issue#1231 修改2.txt
 1 file changed, 1 insertion(+)

4. 查看提交文件日志

用于查看提交历史的命令, 返回信息包包括每次提交的SHA-1哈希值、作者、日期和提交信息。
基本用法: git log [选项]
常见选项有:

  • --oneline:以简洁的方式显示每次提交,每条记录仅包含提交的哈希值和提交信息。
  • -n <number>:限制查看历史日志的数量。
  • --graph: 以图形化方式展示提交历史,方便查看分支和合并情况。 查看当前提交的记录:
sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git log
commit 9c45bae32c4ef2f14bded1d28e1593e132860036 (HEAD -> master)
Author: test <test@qq.com>
Date:   Thu Aug 1 07:37:59 2024 +0800

    Issue#1231 修改2.txt

commit 42cb7b01bef656f0dbbba0d6ce427ba3afe406bc
Author: test <test@qq.com>
Date:   Thu Aug 1 01:37:30 2024 +0800

    提交demo.sql

commit 44d2043ec895d109cdfb48c1a8f35bf0e9356f64
Author: test <test@qq.com>
Date:   Thu Aug 1 01:36:44 2024 +0800

    提交a.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git log --oneline
9c45bae (HEAD -> master) Issue#1231 修改2.txt
42cb7b0 提交demo.sql
44d2043 提交a.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git log --oneline -n 2
9c45bae (HEAD -> master) Issue#1231 修改2.txt
42cb7b0 提交demo.sql

5. 误删文件

5.1 模拟场景

提交文件后不小心删除了工作目录中的文件a.txt,现在需要恢复它:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add .
warning: in the working copy of '1.txt', LF will be replaced by CRLF the next time Git touches it
warning: in the working copy of 'data.csv', LF will be replaced by CRLF the next time Git touches it

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git commit -m "Issue#543534 提交所有文件代码"
[master 2d1a3c0] Issue#543534 提交所有文件代码
 2 files changed, 2 insertions(+)
 create mode 100644 1.txt
 create mode 100644 data.csv

5.2 提交前误删

删除1.txt文件,从git本地仓库恢复回来:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ rm -rf 1.txt
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ ll
total 4
-rw-r--r-- 1 mi 197121 12  8月  1 07:31 2.txt
-rw-r--r-- 1 mi 197121 15  8月  1 21:35 a.txt
-rw-r--r-- 1 mi 197121  6  7月 31 07:14 data.csv
-rw-r--r-- 1 mi 197121  9  7月 31 07:13 demo.sql
drwxr-xr-x 1 mi 197121  0  7月 31 07:15 source/
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git restore 1.txt
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ ll
total 5
-rw-r--r-- 1 mi 197121  6  8月  1 21:37 1.txt
-rw-r--r-- 1 mi 197121 12  8月  1 07:31 2.txt
-rw-r--r-- 1 mi 197121 15  8月  1 21:35 a.txt
-rw-r--r-- 1 mi 197121  6  7月 31 07:14 data.csv
-rw-r--r-- 1 mi 197121  9  7月 31 07:13 demo.sql
drwxr-xr-x 1 mi 197121  0  7月 31 07:15 source/

5.3 误删并提交

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ rm -rf 1.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ ll
total 4
-rw-r--r-- 1 mi 197121 12  8月  1 07:31 2.txt
-rw-r--r-- 1 mi 197121 15  8月  1 21:35 a.txt
-rw-r--r-- 1 mi 197121  6  7月 31 07:14 data.csv
-rw-r--r-- 1 mi 197121  9  7月 31 07:13 demo.sql
drwxr-xr-x 1 mi 197121  0  7月 31 07:15 source/

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git add 1.txt

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git commit -m "Issue354645 删除1.txt"
[master a8b28e7] Issue354645 删除1.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 1.txt

此时git仓库也会做删除处理,git仓库最新版本已经没有了1.txt的相关内容:

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git log --oneline
a8b28e7 (HEAD -> master) Issue354645 删除1.txt
c9ff1f3 Issue#3546349 修改文件a.txt
2d1a3c0 Issue#543534 提交所有文件代码
9c45bae Issue#1231 修改2.txt
42cb7b0 提交demo.sql
44d2043 提交a.txt
## 切换到之前版本
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git reset --hard 2d1a3c0
HEAD is now at 2d1a3c0 Issue#543534 提交所有文件代码

mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ ll
total 5
-rw-r--r-- 1 mi 197121  6  8月  1 22:10 1.txt
-rw-r--r-- 1 mi 197121 12  8月  1 07:31 2.txt
-rw-r--r-- 1 mi 197121 15  8月  1 21:35 a.txt
-rw-r--r-- 1 mi 197121  6  7月 31 07:14 data.csv
-rw-r--r-- 1 mi 197121  9  7月 31 07:13 demo.sql
drwxr-xr-x 1 mi 197121  0  7月 31 07:15 source/

此时我们发现1.txt文件又回来了,但是美中不足的是我们git日志历史丢失了从最新版本到当前版本之间的其他版本内容。

sh
mi@JieBaBa-PC MINGW64 /f/learn_git/git_demo1 (master)
$ git log --oneline
2d1a3c0 (HEAD -> master) Issue#543534 提交所有文件代码
9c45bae Issue#1231 修改2.txt
42cb7b0 提交demo.sql
44d2043 提交a.txt

可以看到提交日志少了2个,可以使用git还原命令:git revert <版本号>, 需要注意的是版本号需要传入还原到的版本的后续一个版本。

sh
$ git revert a8b28e7
[master c83d71d] Revert "Issue354645 删除1.txt"
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt

我们前面安装git的时候,配置编辑软件使用的notepad++, 输完命令回车会自动打开notepad++,如果不改生成的日志,直接关闭即可。 Alt text 查看日志信息,可以看到日志保留下来,还多了一个还原日志:

sh
$ ll
total 5
-rw-r--r-- 1 mi 197121  6  8月  1 22:54 1.txt
-rw-r--r-- 1 mi 197121 12  8月  1 07:31 2.txt
-rw-r--r-- 1 mi 197121 21  8月  1 22:16 a.txt
-rw-r--r-- 1 mi 197121  7  8月  1 22:14 data.csv
-rw-r--r-- 1 mi 197121  9  7月 31 07:13 demo.sql
drwxr-xr-x 1 mi 197121  0  7月 31 07:15 source/
$ git log --oneline
c83d71d (HEAD -> master) Revert "Issue354645 删除1.txt"
a8b28e7 Issue354645 删除1.txt
c9ff1f3 Issue#3546349 修改文件a.txt
2d1a3c0 Issue#543534 提交所有文件代码
9c45bae Issue#1231 修改2.txt
42cb7b0 提交demo.sql
44d2043 提交a.txt