您的位置:首页 > 其它

git实用操作学习记录

2013-03-06 19:22 344 查看
git是一个版本控制系统。
查看系统有没装git
[wang@localhost git]$ rpm -qa|grep -i git
或者直接 man git

1.下载
先把自己介绍给git系统

[root@localhost git]# git config --global user.name "williamwang"
[root@localhost git]# git config --global user.email "wangshan13088@163.com"

[wang@localhost git]$ git clone git@192.168.10.115:acr320/api.git
Initialized empty Git repository in /home/wang/project/git/api/.git/

ssh: connect to host 192.168.10.115 port 22: Network is unreachable 打开网络
fatal: The remote end hung up unexpectedly

[wang@localhost git]$ git clone git@192.168.10.115:acr320/api.git
Initialized empty Git repository in /home/wang/project/git/api/.git/

The authenticity of host '192.168.10.115 (192.168.10.115)' can't be established.
RSA key fingerprint is 92:b3:7d:4c:a4:52:f9:2a:b7:69:4a:6c:d2:78:0b:fa.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.115' (RSA) to the list of known hosts.
git@192.168.10.115's password:

[wang@localhost git]$ ssh-keygen (这里设了个空密码)
Generating public/private rsa key pair.
Enter file in which to save the key (/home/wang/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/wang/.ssh/id_rsa.
Your public key has been saved in /home/wang/.ssh/id_rsa.pub.
The key fingerprint is:
ca:cb:fa:ac:f7:fc:68:99:53:37:11:ad:74:47:19:d4 wang@localhost.wang
The key's randomart image is:
+--[ RSA 2048]----+
| ..+=|
| o o.E|
| . + . |
| o |
| S . |
| . . . o |
| o + . . |
| o.o=. |
| o==o+o. |
+-----------------+

把/home/wang/.ssh/id_rsa.pub这个文件发送给'192.168.10.115'主管,他会给权限,然后就可以下了。

[root@localhost test]# git pull /home/wang/project/ master
如果开发版上另一个人有修改文件,并且我认可他,就可以把他路径上的主干合并到我这来
当然也可以合并到分支exam检测 git pull /home/wang/project/ master:exam

2.上传

[root@localhost test]# git init 初始化当前所在目录的这个项目,创建一个空的git仓库或者重新初始化一个已存在的仓库。
Reinitialized existing Git repository in /home/wang/project/test/.git/
[root@localhost test]# ls -a
. .. .git test.c

[root@localhost test]# git add . 制作索引,创建一个blob文件来记录最新的修改代码,并链接到index file
[root@localhost test]# git commit
Aborting commit due to empty commit message.

修改信息如下:
add new file :test.c just for test

add by william in 2013.3.5

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: test.c
#
~

第一行一定要是少于50字的开发概括信息,而且第二行必须是空行,第三行开始才细致描述开发信息。因为很多版本的服务系统的email机制都会选取log中的第一行为邮件题目。

[root@localhost test]# git diff --cached 检测源码做了哪些修改
会与index file文件做比较,加了--cached参数就和最近一次提交的比较。

[root@localhost test]# get status 哪些文件发生了改动,当前状态的一个总结
提示信息:changed but not updated 已修改,但未git add
提示信息:Changed to be committed 已经git add 但是未git commit
提示信息:Untracked files 增加了新文件或新目录

[root@localhost test]# git add test.c
[root@localhost test]# git commit

也可以直接用git commit -a 提交所有修改。但是它无法提交新增的文件或文件夹

[root@localhost test]# git log
commit 3bbdaaaa559d8c38c02800882f5789145a416756
Author: williamwang <wangshan13088@163.com>
Date: Tue Mar 5 15:54:44 2013 +0800

add new file :test.c just for test
added by william in 2013.3.5

如果你觉得git log 太简单,可以用git log -p ,不但会给出开发日志,而且会显示每个开发版本的代码区别所在。

3.分支

[root@localhost test]# git branch exam
[root@localhost test]# git branch
exam
* master

[root@localhost test]# git checkout exam
Switched to branch "exam"
[root@localhost test]# vim test.c
[root@localhost test]# git commit -a

[root@localhost test]# git checkout master
Switched to branch "master"
[root@localhost test]# vim test.c
[root@localhost test]# git merge exam 分支合并到主干
Already up-to-date.
[root@localhost test]# git commit -a

[root@localhost test]# git branch -d exam
Deleted branch exam (was 3bbdaaa).

-d 表示在分支已经合并到主干后删除分支
-D 表示无论如何都删除分支

4.查看信息
[root@localhost test]# git show master
也可以查看分支信息。
[root@localhost test]# git show HEAD^ 查看HEAD的上一级信息

[root@localhost test]# git tag V3 3bbdaaa 将3bbdaaa开头的commit取一个别名为V3
[root@localhost test]# git show V3
[root@localhost test]# git branch test V3 建一个test的分支

[root@localhost test]# git cat-file -t 6374efc2d22 查看对象类型
commit
[root@localhost test]# git cat-file commit 6374efc2d22 查看一个commit类型的对象
tree c336bee3fced17ae7a8323bb60bebc338c6de143
parent 3bbdaaaa559d8c38c02800882f5789145a416756
author williamwang <wangshan13088@163.com> 1362472954 +0800
committer williamwang <wangshan13088@163.com> 1362472954 +0800

william wang
包括tree,parent,作者信息。

[root@localhost test]# git ls-tree c336bee
100644 blob 0cfc1ee1b5aad2c1a24d11969a813df3e8578364test.c
包含blob和文件

[root@localhost test]# git cat-file -t 0cfc1ee
blob
[root@localhost test]# git cat-file blob 0cfc1ee
#include <stdio.h>
int main()
{
printf("hello!\n");
return 0;
}
包含文件的数据内容

[root@localhost test]# tree ./.git/objects/
./.git/objects/
|-- 0c
| `-- fc1ee1b5aad2c1a24d11969a813df3e8578364
|-- 3b
| `-- bdaaaa559d8c38c02800882f5789145a416756
|-- 63
| `-- 74efc2d222824a8b44c57f92c2fabf4b2ec9d5
|-- 98
| `-- 0cbcb227eaedd869a6fe8a86ac1118f569af5d
|-- c3
| `-- 36bee3fced17ae7a8323bb60bebc338c6de143
|-- fe
| `-- 97b305dd7491bd9d535eb12a78e38e5512a093
|-- info
`-- pack

8 directories, 6 files
[root@localhost test]# cat .git/HEAD
ref: refs/heads/master
[root@localhost test]# cat .git/refs/heads/master
6374efc2d222824a8b44c57f92c2fabf4b2ec9d5
[root@localhost test]# git cat-file -t 6374e
commit
[root@localhost test]# git cat-file commit 6374e
tree c336bee3fced17ae7a8323bb60bebc338c6de143
parent 3bbdaaaa559d8c38c02800882f5789145a416756
author williamwang <wangshan13088@163.com> 1362472954 +0800
committer williamwang <wangshan13088@163.com> 1362472954 +0800

william wang
所以HEAD指向的是最后一次commit的信息。
commit:指向一个tree对象,当前节点的tree目录的镜像,parent 就是之前的commit

tree:用于显示一个目录的状态,包括blob对象和子目录对象
blob:包含文件的数据

查看index file信息

[root@localhost test]# git ls-files --stage
100644 0cfc1ee1b5aad2c1a24d11969a813df3e8578364 0test.c
[root@localhost test]# git cat-file -t 0cfc1ee
blob
[root@localhost test]# git cat-file blob 0cfc1ee
#include <stdio.h>
int main()
{
printf("hello!\n");
return 0;
}

小结:
git维护代码分为三部分:“当前目录”“索引文件”“git仓库”分别标注为1,2,3.
git add 完成1->2
git commit完成2->3
git commit -a 两者结合
git diff 得到的是从2到1的变化
git diff --cached得到的是从3到2的变化
git diff HEAD 得到的是从3到1的变化

5.git 命令补充
git init 初始化git仓库。
git init会创建一个.git隐藏目录。在.git目录下,会创建objects,refs/heads,refs/tags和模版文件,同时,指向master分支的HEAD文件也会被创建。
git-fsck 检查仓库的错误
git-gc 用来完成一些琐碎的工作,比如重组或删减。
git clone 克隆git仓库到另一个新目录。

git checkout -- test.c 取消对test.c文件的修改。
git reset --hard HEAD 表示所有都撤销都以前状态,而soft之撤销commit。
git pull 和git fetch 同步更新git仓库
git push 上传共享

推荐《看日记学git》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: