Git之2——基本操作
来源:互联网 发布:杭州seo外包服务 编辑:程序博客网 时间:2024/05/27 20:52
添加索引--add
可以使用git add命令将文件添加至索引中,这与svn的add命令基本相似,个人觉得可以理解为svn的add操作。
[root@localhost project_test]# git add t1.txt t2.txt
很多版本控制系统都提供了一个 "add" 命令:告诉系统开始去跟踪某一个文件的改动。但是Git里的 ”add” 命令从某种程度上讲更为简单和强大. git add
不但是用来添加不在版本控制中的新文件,也用于添加已在版本控制中但是刚修改过的文件; 在这两种情况下, Git都会获得当前文件的快照并且把内容暂存(stage)到索引中,为下一次commit做好准备。
查看提交文件--diff
git diff命令可以查看哪些文件将被提交,可以使用cached参数来指定已做修改并加入到索引中的文件。我们可以对之前创建的t1.txt进行修改。然后哦使用diff命令。
[root@localhost project_test]# vim t1.txt
[root@localhost project_test]# git diff
diff --git a/t1.txt b/t1.txt
index e69de29..3d8bd3a 100644
--- a/t1.txt
+++ b/t1.txt
@@ -0,0 +1,3 @@
+a
+b
+ c
[root@localhost project_test]# git diff --cached
fatal: No HEAD commit to compare with (yet)
当前项目状态--status
git status命令可以获取当前项目状态。
[root@localhost project_test]# git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
#new file: t1.txt
#new file: t2.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
#modified: t1.txt
#
提交--commit
修改完成后,将修改文件加入索引并提交,会提示添加注释,我们添加对应的注释即可。
[root@localhost project_test]# git add t1.txt
root@localhost project_test]# git commit
[master (root-commit) 263118b] test save
1 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 t1.txt
create mode 100644 t2.txt
[root@localhost project_test]# ls
t1.txt t2.txt
分支合并基础
创建分支
一个git仓库可以有多个分支。首先我们可以创建一个分支。
[root@localhost project_test]# git branch project_test2
查看分支
[root@localhost project_test]# git branch
* master
project_test2
*标示当前工作的分支,在分支中间我们可以进行切换。
现在我们可以切换到project_test2分支下,然后对文件进行操作。
分支切换
[root@localhost project_test]# git checkout project_test2
Switched to branch 'project_test2'
对t1.txt修改,提交,并切换回master分支。
[root@localhost project_test]# vim t1.txt
[root@localhost project_test]# git commit -a
[project_test2 4c897c7] 测试分支
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost project_test]# git checkout master
Switched to branch 'master'
查看t1.txt文件,并对其修改。
[root@localhost project_test]# vim t1.txt
这样子我们两个分支分别都对文件进行了修改(diverged),我们可以对分支进行合并。如果不存在冲突则会完成合并,但是因为我们之前的操作,在这里造成了冲突。
[root@localhost project_test]# git merge project_test2
Updating 263118b..4c897c7
error: Your local changes to 't1.txt' would be overwritten by merge. Aborting.
Please, commit your changes or stash them before you can merge.
使用git diff命令来查看冲突的文件
[root@localhost project_test]# git diff
diff --git a/t1.txt b/t1.txt
index 3d8bd3a..49341cb 100644
--- a/t1.txt
+++ b/t1.txt
@@ -1,3 +1,4 @@
a
b
c
+分支master的修改
编辑冲突的文件,解决冲突。
root@localhost project_test]# git merge project_test2
Updating 263118b..4c897c7
Fast-forward
t1.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
合并后,我们可以使用gitk查看项目的历史。这个插件需要单独安装。
删除分支
在某个分支完成工作后,我们可以删除对应的分支。
[root@localhost project_test]# git branch -d project_test2
Deleted branch project_test2 (was 4c897c7).
[root@localhost project_test]# git branch branch_2
[root@localhost project_test]# git branch -D branch_2
Deleted branch branch_2 (was 4c897c7).
撤销合并
如果我们觉得某个合并不太对,那么我们可以撤销合并。
[root@localhost project_test]# git reset --hard HEAD
如果已经把合并后的代码提交,可以使用如下命令。
[root@localhost project_test]# git reset --hard ORIG_HEAD
Git 日志
git log可以查看所有的提交日志。
[root@localhost project_test]# git log
commit 4c897c74dac839ff38f0b0117f1b6abf6faa9a35
Author: lj <lj@test.com>
Date: Sat Aug 8 02:12:02 2015 -0700
测试分支
commit 263118bccf0f043f8f150d7bb1576178eea1f17e
Author: lj <lj@test.com>
Date: Sat Aug 8 01:56:48 2015 -0700
test save
可以添加--stat参数,在日志中显示那些文件被修改。
[root@localhost project_test]# git log --stat
commit 4c897c74dac839ff38f0b0117f1b6abf6faa9a35
Author: lj <lj@test.com>
Date: Sat Aug 8 02:12:02 2015 -0700
测试分支
t1.txt | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
commit 263118bccf0f043f8f150d7bb1576178eea1f17e
Author: lj <lj@test.com>
Date: Sat Aug 8 01:56:48 2015 -0700
test save
t1.txt | 3 +++
1 files changed, 3 insertions(+), 0 deletions(-)
比较提交——git diff
可以使用git diff比较任意两个版本之间的差异。
新建test分支,并修改文件,然后使用此命令。
[root@localhost project_test]# git branch
* master
[root@localhost project_test]# git branch test
[root@localhost project_test]# git checkout test
Switched to branch 'test'
[root@localhost project_test]# ls
t1.txt t2.txt
[root@localhost project_test]# vim t1.txt
[root@localhost project_test]# git diff master..test
[root@localhost project_test]# git diff master...test
[root@localhost project_test]# git diff master
diff --git a/t1.txt b/t1.txt
index 2379f05..781ec2e 100644
--- a/t1.txt
+++ b/t1.txt
@@ -1,4 +1,5 @@
a
b
c
-测试分支2的修改
+测试分支2的修改
+分支test修改
本文参考:http://gitbook.liuhui998.com/3_2.html
- Git之2——基本操作
- GIT——基本操作
- Eclipse之Git基本操作
- Git版本管理之旅(二)—— Git基本操作流程及相关命令
- Git学习之Git基本操作
- Git常用命令——基本操作
- git简介、安装、基本操作——Git折腾小记
- 一步一步学习Git——Git基本操作(转)…
- git简介、安装、基本操作——Git折腾小记
- git基本操作(2)
- git学习 #2:git基本操作
- git学习 #2:git基本操作
- git(2)----git的基本操作常用命令
- Android基础入门教程——1.5.1 Git使用教程之本地仓库的基本操作
- 版本管理—Git使用教程之本地仓库的基本操作
- Git 简洁教程之:基本操作
- Git使用本地仓库之基本操作
- Git使用本地仓库之基本操作
- A Simple Review Of 《Query Languages for Graph Databases》(III)
- HTML/CSS IE6、7兼容性问题、bug总汇
- ZOJ 3430 Detect the Virus (AC自动机)
- 进制转换/码制转换和计算机大端和小端模式
- 梯度下降(批量&随机)和非负矩阵分解
- Git之2——基本操作
- 在eclipse 中运行 需sudo root权限执行的程序
- jquery 实现可编辑div
- android fragment(2)
- 自定义view(视图类)
- 布尔(bool)全排列
- linux下vi命令大全
- hpu1695 一道签到题
- 结构体内嵌一级指针