git 的安装及使用

一、Git的安装和使用
1.1 Linux下版本库的创建
1.1.1 创建一个版本库 repository,在一个合适的地方创建一个空目录: `root@zengyue:/# mkdir -p /home/shiqi/git_rep` 切换到该目录下: `root@zengyue:/# cd /home/shiqi/git_rep` `root@zengyue:/home/shiq/git_rep# git init` 这样三条命令就创建好了一个Repository。注意在git_rep目录下有一个.git目录,隐藏目录
1.2 更改配置
1.2.1 在配置中写入你的github注册邮箱,和一个名字,使用命令:

git config --global user.email "your@example.com-此为注册邮箱"
git config --global user.name "user_name"

当然你也可以使用如下命令来编辑:

git config --global --edit

1.2.2 查看.git目录下config 文件并配置
root@zengyue:/home/shiq/git_rep# cd ./.git
root@zengyue:/home/shiqiGit/.git# cat config

root@zengyue:/home/shiqiGit/.git# cat config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = https://github.com/[github用户名]/[仓库名].git #这儿是你的远程仓库url地址.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
....

我这是很久以前的一个仓库,此仓库采用https 提交,没有用ssh。下片文章说ssh。可以按照这个模式去vim打开配置,这儿的配置文件只对本仓库管用,
/etc/gitconfig 文件是全库,该文件含有对系统上所有用户及他们所拥有的仓库都生效的配置值(/etc/gitconfig是全局配置文件), 如果传递--system选项给git config命令, Git 会读写这个文件。

接下来 Git 会查找每个用户的~/.gitconfig文件,你能传递--global选项让 Git读写该文件。

最后 Git 会查找由用户定义的各个库中 Git 目录下的配置文件(.git/config),该文件中的值只对属主库有效。 以上阐述的三层配置从一般到特殊层层推进,如果定义的值有冲突,以后面层中定义的为准

1.3 Git的使用

1.3.1 在版本库下创建并添加文件
root@zengyue:/home/shiq/git_rep# vim first_git_file.txt 随便写个内容,以后所有的文件都要在git_rep仓库或者它的子目录下,不然git就找不到你文件了

1.3.2 使用命令 git add 'pathname或文件名' 告诉git 把文件添加奥仓库
root@zengyue:/home/shiq/git_rep# git add 'first_git_file.txt'

1.3.3 命令 git commit -m"提交说明" 把文件提交到仓库
root@zengyue:/home/shiq/git_rep# git commit -m"first commit.test file" -m参数是提交说明,一定要带上,不然以后你不知道为什么做本次改动,和恢复

1.3.4 使用命令 git status 查看更改
root@zengyue:/home/shiq/git_rep# git status

如果你要查比更新后的文件与源文件不同的话用命令:
root@zengyue:/home/shiq/git_rep# git diff 文件名

1.3.5 查看最近四次修改、提交:git log
root@zengyue:/home/shiq/git_rep# git log

root@zengyue:/home/shiqiGit# git log
commit bce70e5f6739968d4478b82e64c7da7760ffc74c
Author: shiqi <--------@--.com>
Date:   Tue Feb 20 23:23:04 2018 +0800

    view

commit 9d84c2ffde33a1b86eae45b5d02c611149a2c374
Author: shiqi <--------@--.com>
Date:   Sun Feb 18 23:38:58 2018 +0800

    login

commit 2f23f15084705ee1ee7acedb1b9c928015755669
Author: shiqi <--------@--.com>
Date:   Wed Feb 14 22:34:14 2018 +0800

    login

commit 99146629af2a89ae7e20325fae3aea8d32e1cfc7
Author: shiqi <--------@--.com>
Date:   Tue Feb 13 23:46:50 2018 +0800

    login
1.3.6 回滚版本(超级好用记住)
回改到上一个版本:git reset --hard HEAD 回改到指定版本:git reset --hard ********* 那一串星号就是你的版本号

git会记录你的每一次命令,使用 git reflog 就可以找到以前的版本号
root@zengyue:/home/shiq/git_rep# git reflog

......
423d777 HEAD@{7}: commit: 分页功能,直接引入该模块,创建实例即可以使用
0444b8e HEAD@{8}: commit: login
995b18b HEAD@{9}: commit: models
......

1.3.7 把本地仓库的内容推送到github:git push origin master
root@zengyue:/home/shiq/git_rep# git push origin master

提示:git支持https和git两种传输协议,github分享链接时会有两种协议可选:这儿默认https协议
当你输入上面的命令后:就会提示你输入你的Github用户名 及密码。然后就推送到了你远程仓库

1.4 创建分支

1.4.1 创建一个分支 git branch [分支名]
root@zengyue:/home/shiq/git_rep# git branch dev

切换到该分支:git checkout dev
root@zengyue:/home/shiq/git_rep# git checkout dev

以上两个命令等同于git checkout -b dev 参数b表示创建并切换

查看分支:git branch

切换回分支: 当工作完成时切回master
git checkout master

posted @ 2018-08-03 15:20  G1733  阅读(154)  评论(0编辑  收藏  举报