黑帽联盟

标题: Git之旅(13):常见场景与技巧(一) [打印本页]

作者: 定位    时间: 2020-4-3 21:26
标题: Git之旅(13):常见场景与技巧(一)
之前,为了文章之间的逻辑连贯性,好多零散的知识点没有总结,不过这并不代表着这些零散的知识点没有用,相反,这些点在某些场景下非常好用,这篇文章就先来总结一些吧。

为了方便演示,我们先来创建一个测试仓库,命令如下:
/d/workspace/git
$ git init test_repo
Initialized empty Git repository in D:/workspace/git/test_repo/.git/

/d/workspace/git
$ cd test_repo/

/d/workspace/git/test_repo (master)
$ echo 11 > testfile1

/d/workspace/git/test_repo (master)
$ cat testfile1
11

/d/workspace/git/test_repo (master)
$ git add testfile1

/d/workspace/git/test_repo (master)
$ git commit -m "init commit, testfile1"
[master (root-commit) 2feec01] init commit, testfile1
1 file changed, 1 insertion(+)
create mode 100644 testfile1

/d/workspace/git/test_repo (master)
$ git log --oneline
2feec01 (HEAD -> master) init commit, testfile1
如上例所示,我们在测试仓库中创建了一个测试文件testfile1,然后创建了一个初始化的提交。

现在,我继续修改testfile1文件,命令如下:
/d/workspace/git/test_repo (master)
$ echo 12 >> testfile1

/d/workspace/git/test_repo (master)
$ cat testfile1
11
12
如上所示,我在testfile1中追加了一行新内容,如果我想要将新的修改创建为提交,那么应该先将这次的修改添加到暂存区,然后再创建提交,此时,如果你使用"git status"命令查看状态,会看到我们还没有将testfile1的最新修改添加到暂存区,如下所示
/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   testfile1

no changes added to commit (use "git add" and/or "git commit -a")
正常来说,我们应该先执行"git add"命令,然后再执行"git commit"命令创建提交,其实,我们还有更加方便的方法,就是直接执行如下命令:
git commit -am "second commit"
上述命令的作用就是将暂存操作和提交操作一次性完成,其效果相当于我们先执行了"git add testfile1"命令,然后又执行了git commit -m "second commit"命令,我们来执行一下,看一下效果,如下:
/d/workspace/git/test_repo (master)
$ git commit -am "second commit"
[master ac238f9] second commit
1 file changed, 1 insertion(+)

/d/workspace/git/test_repo (master)
$ git log --oneline
ac238f9 (HEAD -> master) second commit
2feec01 init commit, testfile1
如上例所示,执行git commit -am "second commit"命令后,第二个提交就被创建了,省去了手动执行git add命令的步骤。
需要注意的是,如果你的工作区中存在完全新创建的文件(从未被git跟踪过,刚刚从工作区中新建出来),那么"git commit -am"命令并不会将新建的文件添加到暂存区。

当你觉得,目前工作区中的所有变更(被跟踪过的文件的变更)适合创建在同一个提交中时,使用"git commit -am"命令还是很方便的,这样可以省略一步git add的操作。

在前文中,我们一直在添加文件,修改文件,从来没有删除过文件,现在我们来测试一下删除文件的操作,就拿删除testfile1文件为例吧, 示例操作如下:
/d/workspace/git/test_repo (master)
$ rm testfile1

/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    testfile1

no changes added to commit (use "git add" and/or "git commit -a")
如上例所示,我执行了删除命令,删除了testfile1文件,执行删除命令后,在文件系统中已经看不到testfile1文件了,此时执行"git status"命令,会发现git提示我们,有一个变更还没有暂存,也就是说,删除文件的操作,也会被git理解成一个变更,既然是变更,就要从工作区添加到暂存区以后,才能够创建在提交中,从"git status"命令的提示信息中可以看到,我们可以使用"git add"命令或者"git rm"命令将这个删除文件的变更添加到暂存区,那么我们一起来操作一下,如下所示:
/d/workspace/git/test_repo (master)
$ git add testfile1

/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    testfile1
执行git add testfile1命令后,删除testfile1文件的变更就添加到了暂存区,之后,我们就能够直接进行提交了。
总结一下,上述删除testfile1文件的步骤一共执行了两步操作:
第一步:在文件系统中删除testfile1文件。
第二步:在git中将删除文件的变更添加到暂存区。

其实,上述两步操作可以通过一条命令来完成,这条命令就是刚才提到过的"git rm"命令
我们可以在没有从文件系统中删除testfile1的情况下,直接执行如下命令
git rm testfile1
执行完上述命令后,你会发现文件系统中的testfile1被删除了,而且使用"git status"命令查看,会发现变更已经自动添加到了暂存区,相当于一条命令完成了两步操作(同样需要注意,我们所说的被删除的文件,也是被git跟踪过的文件),此处就不进行示例了,快动手实验一下吧。

除了删除文件,我们可能经常会有对文件重命名的需求(对已经被git跟踪过的文件重命名),我们来模拟一遍,操作如下:
注:为了方便演示,我直接将刚才的testfile1还原回来,以便执行重命名的操作,还原的操作就不再赘述了,可以参考之前的文章:“后悔了,怎么办?”
/d/workspace/git/test_repo (master)
$ ls
testfile1

/d/workspace/git/test_repo (master)
$ mv testfile1 tf1

/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        deleted:    testfile1

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

        tf1

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

/d/workspace/git/test_repo (master)
$ git add -A

/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    testfile1 -> tf1
如上述操作所示,我先使用mv命令在文件系统中重命名了testfile1,将testfile1重命名为tf1,之后,使用"git status"命令查看,发现git会把重命名的操作理解成先删除了testfile1,又创建了tf1,之后,我将变更添加到了暂存区,发现git自动将之前的变更识别为了重命名操作,那么,有没有更加方便的方法呢?必须有的,方法就是,不在文件系统中重命名testfile1,而是直接执行如下命令即可。
git mv testfile1 tf1
同样,为了方便演示,我将仓库还原成了没有重命名之前的状态,然后执行如下命令进行演示:
/d/workspace/git/test_repo (master)
$ ls
testfile1

/d/workspace/git/test_repo (master)
$ git mv testfile1 tf1

/d/workspace/git/test_repo (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        renamed:    testfile1 -> tf1
如上述命令所示,当我们使用"git mv"命令重命名文件后,重命名操作会自动添加到暂存区,从而省略了手动执行"git add"命令的步骤,还是很方便的。

小结
git commit -am "注释"
上述命令表示:省略"git add"操作,自动完成暂存并创建提交,换句话说就是,直接将工作区中的所有变更创建成一个提交,注意:完全新建没有被git跟踪过的文件不会自动暂存。
git rm testfile
上述命令表示:删除testfile文件,并自动将变更暂存,效果相当于在文件系统中删除testfile,然后手动暂存。
git mv testfile tf
上述命令表示:将testfile重命名成tf,并自动将变更暂存,效果相当于在文件系统中重命名了testfile,然以手动暂存。

好了,这篇文章就先总结到这里,希望能够对你有所帮助,再见啦~






欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) Powered by Discuz! X2.5