git init/add/status/commit/log/diff/show/rm/mv命令

1.git init创建版本库
git init将一个目录转化为git版本库

jie$ mkdir ~/gitstudy
jie$ cd ~/gitstudy/
jie$ git init
Initialized empty Git repository in /home/jie/gitstudy/.git/

可以看到现在gitstudy转化为了一个空的git库。注意:无论目录为空还是目录下有文件,git将目录转化为版本库的过程是相同的,git不会将目录下的文件一起加入版本库。

jie$ mkdir ~/gitstudy1
jie$ cd ~/gitstudy1/
jie$ echo "11" > 1.txt
jie$ ls
1.txt
jie$ git init
Initialized empty Git repository in /home/jie/gitstudy1/.git/
jie$ git status #这个命令可以查看当前git库的状态
On branch masterInitial commitUntracked files:(use "git add ..." to include in what will be committed)1.txtnothing added to commit but untracked files present (use "git add" to track)。

可以看到现在gitstudy1下存在文件1.txt,但仍然转化为了一个空的git库,并且1.txt没有加入版本库。
下面来看一下git init命令都做了哪些工作:

jie$ cd ~/gitstudy
jie$ ls -a
.  ..  .git
jie$ cd .git/
jie$ ls
branches  config  description  HEAD  hooks  info  objects  refs

git init命令在目录下创建了一个隐藏目录.git,.git目录中存放当前git库的版本信息,这个目录的内容以后再详细介绍。
2.git add将文件添加到暂存区
git init命令创建一个新的git版本库,最初git版本库是空的,使用git add file命令将file”添加”到版本库暂存区。
git add命令更准确地是将文件置于git的管理下。

jie$ echo "this is a test file" > test.txt
jie$ git add test.txt 

注意:也可以使用git add 目录,将该目录及其子目录下的文件添加到版本库。
现在git只是暂存了test.txt,这是提交commit之前的中间步骤,git将add和commit这两步分开,以避免频繁commit。
此时通过git status命令查看状态:

jie$ git status
On branch masterInitial commitChanges to be committed:(use "git rm --cached ..." to unstage)new file:   test.txt

test.txt 将在下次commit时被添加到版本库中。
3.git commit 将修改提交到版本库
使用git commit命令可以真正
将文件添加到版本库中,除了目录和文件内容的变化,git还会在每次提交的时候记录其它一些元数据,包括日志消息和作出本次变更的作者。

jie$ git commit test.txt -m "new test.txt"*** Please tell me who you are.Rungit config --global user.email "you@example.com"git config --global user.name "Your Name"to set your account's default identity.
Omit --global to set the identity only in this repository.fatal: unable to auto-detect email address (got 'jie@ubuntu.(none)')

如果没有配置提交作者相关信息的话,在git commit时就会出现上述错误信息。使用git config配置相关信息:

jie$ git config user.name "jie"
jie$ git config user.email "jie@jie.com"

配置完成后再次commit就不会再报错:

jie$ git commit -m "new test.txt"
[master (root-commit) 289d3bf] new test.txt1 file changed, 1 insertion(+)create mode 100644 test.txt

这里我们是使用 -m选项提交一条日志消息,更好的做法是使用交互式编辑器:

jie$ cat test.txt
this is a test file 
jie$ vi test.txt
jie$ cat test.txt
this is a test file
add for git commit 
jie$ git add test.txt
jie$ export  GIT_EDITOR=vim  #将交互式编辑器设为vim编辑器。因为我不会用其它的编辑器
jie$ git commit              #这个之后会打开一个vim编辑器,可以在那里输入日志消息。
[master afa8f4d] add last line: add for git commit1 file changed, 1 insertion(+)
jie$ git status
On branch master
nothing to commit, working directory clean

可以看到这时git库的状态是干净的(clean)。
4.git log/show 查看提交历史、提交内容

jie$ git log
commit afa8f4dcd31459c2261ae01bb552421d8a8a57ae
Author: jie 
Date:   Sat Apr 1 23:04:25 2017 -0700add last line: add for git commitcommit 289d3bf3b329e573478e7bd1d7c2fa63d81455b5
Author: jie 
Date:   Sat Apr 1 22:54:07 2017 -0700new test.txt

git log的输出显示了每条提交的作者信息、email、提交日期、日志信息、提交的内部识别码。
如果想查看特定提交的详细信息,可以使用git show commitid:

jie$ git show afa8f4dcd31459c2261ae01bb5524
commit afa8f4dcd31459c2261ae01bb552421d8a8a57ae
Author: jie 
Date:   Sat Apr 1 23:04:25 2017 -0700add last line: add for git commitdiff --git a/test.txt b/test.txt
index 493021b..c8e4f1e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@this is a test file
+add for git commit 

注意:上述git show命令中的 commitid=afa8f4dcd31459c2261ae01bb5524,
这个commitid就是afa8f4dcd31459c2261ae01bb552421d8a8a57ae,我们没必要写全,
对于commitid我们只需要写出开头一部分(保证这个”不完整commitid”唯一)就可以了:

jie$ git show afa8f4 #等价于git show afa8f4dcd31459c2261ae01bb552421d8a8a57ae
commit afa8f4dcd31459c2261ae01bb552421d8a8a57ae
Author: jie 
Date:   Sat Apr 1 23:04:25 2017 -0700add last line: add for git commitdiff --git a/test.txt b/test.txt
index 493021b..c8e4f1e 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@this is a test file
+add for git commit

5.git diff 查看提交差异
git diff 命令可以查看两个版本库之间的差异:

jie$ git diff afa8f4dc 289d3bf3b329e
diff --git a/test.txt b/test.txt
index c8e4f1e..493021b 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1 @@this is a test file
-add for git commit 

6.git rm/mv版本库里的文件的删除和重命名
现在我们的版本库中只有一个文件test.txt,如果想其重命名为test.c,要使用git mv:

jie$ git mv test.txt test.c
jie$ git status
On branch master
Changes to be committed:(use "git reset HEAD ..." to unstage)renamed:    test.txt -> test.cjie$ git commit -m "rename test.txt test.c"
[master 729dda1] rename test.txt test.c1 file changed, 0 insertions(+), 0 deletions(-)rename test.txt => test.c (100%)
jie$ git log
commit 729dda17ab88eeab41a1bc967e2e7c1d406ef985
Author: jie .com>
Date:   Sun Apr 2 00:31:51 2017 -0700rename test.txt test.ccommit afa8f4dcd31459c2261ae01bb552421d8a8a57ae
Author: jie .com>
Date:   Sat Apr 1 23:04:25 2017 -0700add last line: add for git commitcommit 289d3bf3b329e573478e7bd1d7c2fa63d81455b5
Author: jie .com>
Date:   Sat Apr 1 22:54:07 2017 -0700new test.txt

如果想删除文件 test.c,使用git rm:

jie$ git rm test.c 
rm 'test.c'
jie$ git status
On branch master
Changes to be committed:(use "git reset HEAD ..." to unstage)deleted:    test.cjie$ git commit -m "rm test.c"
[master c8aba20] rm test.c1 file changed, 2 deletions(-)delete mode 100644 test.c
jie$ git log
commit c8aba20cf1a9940ab68a170e4bd18402da8e7f17
Author: jie @jie.com>
Date:   Sun Apr 2 00:34:04 2017 -0700rm test.ccommit 729dda17ab88eeab41a1bc967e2e7c1d406ef985
Author: jie @jie.com>
Date:   Sun Apr 2 00:31:51 2017 -0700rename test.txt test.ccommit afa8f4dcd31459c2261ae01bb552421d8a8a57ae
Author: jie @jie.com>
Date:   Sat Apr 1 23:04:25 2017 -0700add last line: add for git commitcommit 289d3bf3b329e573478e7bd1d7c2fa63d81455b5
Author: jie @jie.com>
Date:   Sat Apr 1 22:54:07 2017 -0700new test.txt


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部