git命令

git命令

基本用法

git 初始化

//设置name和email,GIt全局配置文件的配置方法:git config --global(私人不用) <配置名称> <配置的值>
git config --global user.name "Chen Jian Yong"
git config --global user.email "schacon@mail.com"
//查看刚才的设置
cat ~/.gitconfig
获得一个Git仓库
//克隆一个仓库
git clone http://git.shiyanlou.com/shiyanlou.gitproject
//进入git仓库
cd gitproject/
//初始化一个新的仓库
cd /home/shiyanlou/
mkdir project
cd project
git init //初始化

正常的工作流程
1,创建或修改文件
2,使用git add命令添加新创建或修改的文件到本地的缓存区(Index)
3,使用git commit命令提交到本地代码库
4,(可选,有的时候并没有可以同步到远程代码库)使用git push将本地代码库同步到远端代码库

//进入新建仓库
cd project
touch file{1..3}
//修改文件
echo "test" >> file{1..3}
//查看仓库状态
git status
//将新建文件添加到缓存区
git add file{1..3}
//查看缓存区文件被修改
git diff --cached
//提交到代码库
git commit -m "add 3 files"
分支与合并
//创建分支
git branch experimental
//查看当前分支列表,以及目前的开发环境在哪个分支上
git branch 
//切换到expermental分支,作修改
git checkout experimental
echo "update" >> file1
git status
git add file1
git commit -m "update file1"
cat file1
//切换到master分支,做修改
//expermental分支上的修改,在master上看不到
git checkout master
echo "update again" >> file2
git status
git add file2
git commit -m "update file2 on master"
cat file2
//两个分支同时修改同个文件,合并会失败
git checkout master
echo "master:update file3" >> file3
git commit -a -m 'update file3 on master'
git checkout experimental
echo "experimental: update file3" >> file3
git commit -a -m 'update file3 on experimental'    //-a:用于提交过的文件
//合并
git checkout master
git merge experimental
//不需要分支experimental,删除
git branch -d experimental	//-d:删除已经被当前分支合并的分支 -D:强制删除分支
//撤销一个合并
git reset --hard HEAD^	//回到上一个版本
//当版本很多的时候
git log pretty = online	//显示所有提交版本的记录,选择id
git rest --hard id
git日志
//显示所有的日志
git log
//git log帮助
git help log
//找出所有从“v2.5“开始在fs目录下的所有Makefile修改
git log v2.5.. Makefile fs/
//日志统计 打印详细的提交记录
git log --stat
//格式化日志
git log --pretty=oneline	//--pertty:表现若干格式
git log --pretty=short 
git log --graph --pretty=oneline	//--graph可视化
//日志排序
git log --pretty=format:' %h : %s' --topo-order --graph	//正序
git log --pretty=format:'%h : %s' --topo-order --graph -reverse//反序

//比较提交
cd gitproject
echo "new line" >>README.md
echo "new file" >> file1
git status		//查看当前修改状态
git diff	//比较修改的或提交的文件内容
git add *	//把当前目录下所有修改的新增的文件都自动添加
git diff --cached	//查看缓存区内与上一次提交之间的差距
git commit -m 'update code'	//提交代码
//比较分支
git branch test
git checkout test
echo "branch test" >> file1
echo "new file2" >> file2
git add * 
git commit -m 'update test branch'
git diff master test	//查看test分支和master分支的区别
//更多查看
git checkout master
//查看与test分支的区别
git diff test
git --diff test file1	//显示当前工作目录下的file1与test分支之间的差别
git diff test --stat	//统计有哪些文件被改动,有多少行被改动

分布式的工作流程

流程

//另外的用户与你协同开发,且使用同一个用户(一般是多用户)
cd /tmp
git clone /home/shiyanlou/gitproject myepo
ls -l myrepo
cd myrepo
//修改并提交
echo "newcontent" > newfile
git add newfile
git commit -m "add newfile"
//合并这份修改到gitproject的git仓库
//在仓库/home/shiyanlou/gitproject中把myrepo的修改给pull下来
cd /home/shiyanlou/gitproject
git pull /tmp/myrepo master
ls
posted @ 2017-10-19 12:33  哨音  阅读(111)  评论(0编辑  收藏  举报