项目管理工具——git(本地仓库的管理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014104588/article/details/50615082

**参考廖雪峰的教程******************
一:创建本地git库

mkdir git_blog   
[jason@localhost git_blog]$ git init 
Initialized empty Git repository in /home/jason/git_blog/.git/

显示提示
初始化了一个空的git仓库在git_blog目录下的.git目录下。这个仓库管理的目录就是git_blog这个目录。
用ls -al就可以看到这个.git目录

二:添加文件
在git_blog目录下添加git_tes1如下

[jason@localhost git_blog]$ cat git_test1 
just for git test

保存文件推出vim后。执行git status命令查看本地仓库变动

[jason@localhost git_blog]$ git status 
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   git_test1
nothing added to commit but untracked files present (use "git add" to track)

上面Untracked files表示这个文件从添加到工作区以来还没有用git add添加到暂存区,所以这个文件是没有被git跟踪状态的。还提示用git add将这个文件被git跟踪。

[jason@localhost git_blog]$ git add git_test1
[jason@localhost git_blog]$ git status 
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   git_test1
#

上面提示Changes to be committed 表明这个文件下面的做法或许或是被提交到本地仓库。当然也可以用git rm –cached git_test1命令将该文件从暂存区删除。下面用执行该命令看看情况:

[jason@localhost git_blog]$ git rm --cached git_test1
rm 'git_test1'
[jason@localhost git_blog]$ ls
git_test1
[jason@localhost git_blog]$ git status 
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   git_test1
nothing added to commit but untracked files present (use "git add" to track)

上面显示 git rm –cache 可以将暂存区中的文件删除掉(工作区的文件依然存在),同时将本地仓库的记录更新回到没有git add这个文件的状态(untracked)。
现在再将git_test1文件add到暂存区,同时git commit到本地仓库

[jason@localhost git_blog]$ git commit -m "new file git_test1"
[master (root-commit) c37d49e] new file git_test1
 1 file changed, 1 insertion(+)
 create mode 100644 git_test1

结果显示再仓库里面创造了一个git_test1文件,[master (root-commit) c37d49e]表示这个文件是添加到本地仓库的master分支的(此时工作区属于master分支)。c37d49e这次提交的commit id(也可以当作这次的版本号) 可以用来恢复版本。
再用git status查看以下工作区的状态

[jason@localhost git_blog]$ git status 
# On branch master
nothing to commit, working directory clean

从显示可以知道工作区已经和本地仓库同步了。
现在将git_test1添加一行,现在文件中的内容如下

[jason@localhost git_blog]$ cat git_test1 
just for git test
add one line

执行git status查看状态

[jason@localhost git_blog]$ 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:   git_test1
#
no changes added to commit (use "git add" and/or "git commit -a")

显示结果说明,在master分支上面有一个文件被修改了,但是没有被添加到暂存区。
和上面一样执行git add和git commit命令。修改的文件会被添加到本地仓库。
执行git commit命令如下

[jason@localhost git_blog]$ git commit -m "modify git_test1"
[master 08d070c] modify git_test1
 1 file changed, 1 insertion(+)

可以看到这个master分支上的这个文件已经改变了。
三:版本回退
现在用git log命令查看被提交到本地仓库的版本记录

commit 08d070cdb220ebcd5306c8ea8ecfaccf922006c4
Author: zhangyou <1660365078@qq.com>
Date:   Sun Jan 31 14:37:52 2016 +0800

    modify git_test1

commit c37d49e9261fdf322de98e0e0be8d953fdc016e6
Author: zhangyou <1660365078@qq.com>
Date:   Sun Jan 31 14:19:53 2016 +0800

    new file git_test1

可以看到git本地仓库中有两个版本。现在的版本处于08d070…这个版本。文件中的内容如下

[jason@localhost git_blog]$ cat git_test1 
just for git test
add one line

现在回到上一个版本,即c37d4开头的版本。

[jason@localhost git_blog]$ git reset --hard  c37d49
HEAD is now at c37d49e new file git_test1

结果显示现在的版本已经处于c37d49e这个版本了,new file git_test1是当初git commit这个文件时添加到说明。现在文件的内容:

[jason@localhost git_blog]$ cat git_test1 
just for git test

可见版本已经回退了。
如果想回到08d070这个版本可以用下面两种方法:

若记得版本号
git reset --hard 08d070
若不记得版本号
[jason@localhost git_blog]$ git reflog 
c37d49e HEAD@{0}: reset: moving to c37d49
08d070c HEAD@{1}: commit: modify git_test1
c37d49e HEAD@{2}: commit (initial): new file git_test1
然后执行
git reset --hard 08d070c

四:查看区别
现在假设仓库处于08d070c这个版本
现在将文件添加一行,内容额如下

[jason@localhost git_blog]$ cat git_test1 
just for git test
add one line
this is different

git status显示如下

[jason@localhost git_blog]$ 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:   git_test1
#
no changes added to commit (use "git add" and/or "git commit -a")

可见现在暂存区中的内容和本地库的内容是一样的。但是工作区的git_test1文件被修改了。
下面用git diff查看差别
git diff:是查看working tree与index file的差别的。(index file是暂存区的文件)
git diff –cached:是查看index file与commit的差别的。
git diff HEAD:是查看working tree和commit的差别的。
这三个命令的后面都可以跟一个文件名。git diff只能查看被git跟踪的文件的不同,没有被跟踪的文件是不会显示不同的。

[jason@localhost git_blog]$ git diff
diff --git a/git_test1 b/git_test1
index 48928e5..afc31b4 100644
--- a/git_test1
+++ b/git_test1
@@ -1,2 +1,3 @@
 just for git test
 add one line
+this is different

比较工作区与暂存区的差别可以知道暂存区的git_test1文件增加了一行this is different。

[jason@localhost git_blog]$ git diff --cached  不显示,说明暂存区的内容已经和本的库的内容一样了。

五:撤销修改

$ git checkout -- readme.txt
命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态(也可以用git reset -hard xxxx恢复到和某一版本一样的状态);
一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
总之,就是让这个文件回到最近一次git commit或git add时的状态。

六:撤销最近一次的git add
若将工作区的内容更改且add到暂存区了,若想将add的内容删除掉,则用下面的命令:

[jason@localhost git_blog]$ git reset HEAD 

但是如果git add后,且将工作区的内容更改了,则执行git reset HEAD后,工作区的内容不会回退到git add之前的内容,仍然是修改后的内容。

七:删除文件
若使用rm命令直接删除文件的话,会在本地将文件删除,但是若该文件以添加到了仓库,则仓库中依然存在该文件,如下:

[jason@localhost git_blog]$ rm git_test3
[jason@localhost git_blog]$ 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:    git_test3
#
no changes added to commit (use "git add" and/or "git commit -a")

若是不小心删除的,则可以用git checkout – git_test3命令从本地仓库中恢复这个文件。
若确实想删除这个文件,则可以用git rm git_test3命令将文件删除,然后将更改commit到仓库,如下
(不需要用git add了)

[jason@localhost git_blog]$ git rm git_test3
rm 'git_test3'
[jason@localhost git_blog]$ git status 
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   deleted:    git_test3
#
[jason@localhost git_blog]$ git commit -m "delete git_test3"

猜你喜欢

转载自blog.csdn.net/u014104588/article/details/50615082