Git的基本使用
环境:Ubuntu15.04
1、安装
查看是否已安装git:
$git
Theprogram 'git' is currently not installed. You can install it bytyping:
sudoapt-get install git
上面的输出表示未安装git。
安装git:
$sudo apt-get install git
2、初始化配置
在使用前必须要指定用户名和EMAIL:
$git config --global user.name "wang_san_shi"
$git config --global user.email "threemonoliths@gmail.com"
查看配置:
$cat ~/.gitconfig
3、创建版本库
首先创建目录:
$mkdir git
$cd git
初始化目录:
$git init
添加文件test:
$more test
Thisis first version.
要让git来管理文件需要两步,add和commit:
$git add test
$git commit -m "create a file named test"
[master(root-commit) b0b86f4] create a file named test
1file changed, 1 insertion(+)
createmode 100644 test
commit后的版本就相当与一个保存点,今后可任意恢复到这里。
4、修改文件
在test文件中添加一行:
$more test
Thisis first version.
Thisis second version.
查看状态:
$git status
Onbranch master
Changesnot staged for commit:
(use"git add <file>..." to update what will be committed)
(use"git checkout -- <file>..." to discard changes inworking directory)
modified: test
nochanges added to commit (use "git add" and/or "gitcommit -a")
此时提示有修改,需要先add再commit。
查看不同:
$git diff test
diff--git a/test b/test
index133e97c..1d9c03f 100644
---a/test
+++b/test
@@-1 +1,2 @@
Thisis first version.
+Thisis second version.
提交:
$git add test
$git status
Onbranch master
Changesto be committed:
(use"git reset HEAD <file>..." to unstage)
modified: test
$git commit -m "add a line of second version"
[master7420847] add a line of second version
1file changed, 1 insertion(+)
$git status
Onbranch master
nothingto commit, working directory clean
5、版本的切换
查看各个版本:
$git log --pretty=oneline
742084706da22b0213b6c1f331c108bdce957378add a line of second version
b0b86f49c1f76eb644cbf0c14b9881cd6e70b04ecreate a file named test
可以看到两个版本。
回退到前一个版本:
$git reset --hard HEAD^
HEADis now at b0b86f4 create a file named test
现在HEAD已经在b0b86f4(地一个版本的版本号)这个位置了。
确认已经回退:
$more test
Thisis first version.
另外可以使用HEAD^^以及HEAD~100回退到前2个版本和前100个版本。
如果要再次回到第二个版本,则需要提供版本号:
$git reset --hard 742084
HEADis now at 7420847 add a line of second version
确认又回到最新版本:
$git log --pretty=oneline
742084706da22b0213b6c1f331c108bdce957378add a line of second version
b0b86f49c1f76eb644cbf0c14b9881cd6e70b04ecreate a file named test
git$more test
Thisis first version.
Thisis second version.
没有最新版本的版本号,则需要查询一下:
$git reflog
7420847HEAD@{0}: reset: moving to 742084
b0b86f4HEAD@{1}: reset: moving to HEAD^
7420847HEAD@{2}: commit: add a line of second version
b0b86f4HEAD@{3}: commit (initial): create a file named test
HEAD其实是一个指针,回退时仅仅改变指针的指向,因此速度非常快。
6、Git管理的是“修改”
创建版本库时,Git自动创建了一个master分支;
add命令是将文件添加到暂存区;
commit命令是将更改提交到master分支;
add命令之后进行的修改,将不会commit到分支:
添加一行并添加到暂存区:
$more test
Thisis first version.
Thisis second version.
Thisis third version.
$git add test
再次添加一行:
$more test
Thisis first version.
Thisis second version.
Thisis third version.
Thisis forth version.
直接commit:
$git commit -m "insert third and forth but add third only"
这样做的话forthvesion这一行是没有提交的。
查看test在工作区与版本库中的区别:
$git diff HEAD -- test
diff--git a/test b/test
index6f4ad13..9796814 100644
---a/test
+++b/test
@@-1,3 +1,4 @@
Thisis first version.
Thisis second version.
Thisis third version.
+Thisis forth version.
理解工作区、暂存区和版本库:
工作区内容包含所有修改。
暂存区内容仅包含gitadd过的修改。
版本库内容仅包含gitadd并且gitcommit过的修改。
撤销修改:
首先,取消暂存区的内容(更改将只存在于工作区):
$git reset HEAD filename
然后,丢弃工作区的修改(更改将彻底消失):
$git checkout -- filename
删除文件:
首先,在工作区删除文件:
$rm filename
接着在版本库删除文件:
$git rm filename
如果要恢复删除:
$git checkout – filename
7、远程仓库GitHub
7.1创建远程库并同步
要在github上创建远程仓库之前,需要三个步骤:
第一步:在github.com上注册用户;
第二步:在本地生成sshkey:$ssh-keygen -t rsa -C"threemonoliths@gmail.com"(如果~/.ssh下有id_rsa和id_rsa.pub两个文件,则这一步可以忽略)
第三步:在github.com中,setting→SSHkeys→ Add SSH key,然后复制本地~/.ssh/id_rsa.pub文 件内容,确认添加;
然后可以在github上创建repo(创建成功后在页面上可以看到提示),要同步本地库和远程库:
git remote add origin git@github.com:
threemonoliths
/git.git
git push -u origin master以后可以在本地同步已提交的更改到远程库了:
git push origin master
可以删除本地和远程的关联:
$ git remote -v
origin https://github.com/threemonoliths/git.git (fetch)
origin https://github.com/threemonoliths/git.git (push)
$ git remote rm origin
$git remote -v
7.2从远程库克隆
git clone
git
@github.com:
three
monoliths/git.git