前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学习笔记:04_Git的增、删、改和查看日志

学习笔记:04_Git的增、删、改和查看日志

作者头像
全栈程序员站长
发布2022-06-30 10:44:19
2620
发布2022-06-30 10:44:19
举报
文章被收录于专栏:全栈程序员必看

使用git之前需要先配置user.nameuser.email

配置单的地方:

  • ~/.gitconfig –global
  • .git/config –local

which 命令

代码语言:javascript
复制
Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ which vim
/usr/bin/vim

Administrator@kevin MINGW32 ~/desktop/myGit (master)

echo命令

代码语言:javascript
复制
$ echo 'hello git'
hello git

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ echo 'hello git 2' > a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 2
-rw-r--r-- 1 Administrator 197121 12 八月   21 20:18 a.txt
-rw-r--r-- 1 Administrator 197121 14 八月   20 22:24 test.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$

光标在前和后的快捷键:ctrl+A / ctrl+E

1.删除版本库的文件

git rm 文件名

代码语言:javascript
复制
$ git add a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   a.txt


Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git commit -m '测试a.txt'
[master 807aa69] 测试a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
nothing to commit, working tree clean

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git rm a.txt
rm 'a.txt'

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ls
test.txt

staged 缓存区

代码语言:javascript
复制
Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git reset HEAD a.txt  
Unstaged changes after reset:
D       a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    a.txt

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

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git checkout -- a.txt  // checkout -- :表示放弃前面的操作 reset Head 综合使用 才能把删除的文件恢复过来

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ls
a.txt  test.txt //删除的文件又回来的

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$

删除了文件不要提交,提交之后就不能进行恢复。

git rm

代码语言:javascript
复制
Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git rm test.txt
rm 'test.txt'

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    test.txt


Administrator@kevin MINGW32 ~/desktop/myGit (master)
$
删除了一个文件,并且将删除的文件纳入到暂存区中(stage)
$ git commit -m 'delete test.txt'

如果想恢复删除的文件,需要有两个动作,

一个是:git reset HEAD test.txt

将删除的文件从缓存区恢复到工作区

另一个操作时候:git checkout – test.txt

rm

test.txt之间删除掉了.这时候被删除的文件并未纳入暂存区当中。这个时候提交是不能的。

工作区—-缓存区—版本库

3.git重命名

git mv 移动重命名

代码语言:javascript
复制
Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ echo 'h'>a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git mv a.txt b.txt
fatal: not under version control, source=a.txt, destination=b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git mv a.txt b.txt
fatal: not under version control, source=a.txt, destination=b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git mv a.txt b.txt
fatal: not under version control, source=a.txt, destination=b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
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)

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git add a.txt
warning: LF will be replaced by CRLF in a.txt.
The file will have its original line endings in your working directory

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git commit
[master 79085b4] a.txt`
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git mv a.txt b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage) 
        renamed:    a.txt -> b.txt


Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git restore --staged a.txt //从版本库到缓存区

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 1
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git checkout -- a.txt //从缓存区到工作区

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ ll
total 2
-rw-r--r-- 1 Administrator 197121 3 八月   21 20:54 a.txt
-rw-r--r-- 1 Administrator 197121 2 八月   21 20:51 b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ rm b.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ cat a.txt
h

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   b.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    b.txt


Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git add a.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git commit -m 'git mv a.txt恢复'
[master 89acc6c] git mv a.txt恢复
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

git add . //点表示所有的文件包括当前文件下的子文件

git commit –amend -m ‘’ //上次提交的信息进行修正

4.git log

git log

  • -p 展开显示每次提交的内容差异
  • -n 仅显示最近的n次更新
  • –stat 仅显示简要的增改数统计
  • –pretty=oneline
  • –pretty=format:”%h – %an, %ar:%s”
代码语言:javascript
复制
$ git log --pretty=oneline
89acc6c51ca03c946dc5df3a544f052942ae72c2 (HEAD -> master) git mv a.txt恢复
79085b4ef43596322f78e7cf0c47943257f690ae a.txt`
05a4902d45a48933433a3a600bb9aa08690b8446 remove test.txt
f198cb6b4def9b5ebef319db5c09866a73bce97d delete a.txt
807aa691dc3d086530b8b36b5ad8a9ff91732b55 测试a.txt
9b79eb743639cfd7b6e62966d6012403ce3b3fd0 再次修改为 iloveyou to
ba82bafa862d0cccdd105e725f2e17f711a321eb 测试

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git log 3
fatal: ambiguous argument '3': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git log -3
commit 89acc6c51ca03c946dc5df3a544f052942ae72c2 (HEAD -> master)
Author: zx <xxx@qq.com>
Date:   Wed Aug 21 20:56:12 2019 +0800

    git mv a.txt恢复

commit 79085b4ef43596322f78e7cf0c47943257f690ae
Author: zx <xxx@qq.com>
Date:   Wed Aug 21 20:52:41 2019 +0800

    a.txt`

commit 05a4902d45a48933433a3a600bb9aa08690b8446
Author: zx <xxx@qq.com>
Date:   Wed Aug 21 20:41:52 2019 +0800

    remove test.txt

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$ git log --pretty=format:"%h - %an,%ar:%s"
89acc6c - zx,15 minutes ago:git mv a.txt恢复
79085b4 - zx,19 minutes ago:a.txt`
05a4902 - zx,29 minutes ago:remove test.txt
f198cb6 - zx,38 minutes ago:delete a.txt
807aa69 - zx,50 minutes ago:测试a.txt
9b79eb7 - zx,23 hours ago:再次修改为 iloveyou to
ba82baf - zx,23 hours ago:测试

Administrator@kevin MINGW32 ~/desktop/myGit (master)
$

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/100779.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.删除版本库的文件
  • 3.git重命名
  • 4.git log
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档