git-命令行学习

初始化项目

Git 全局设置:

git config --global user.name "xxxxxx"
git config --global user.email "[email protected]"

创建 git 仓库:

mkdir ocmodel
cd ocmodel
git init
touch README.md
git add README.md
git commit -m "first commit"
git remote add origin https://gitee.com/Simple_code/ocmodel.git
git push -u origin master

已有仓库?

cd existing_git_repo
git remote add origin https://gitee.com/Simple_code/ocmodel.git
git push -u origin master
Git 三大分区
Git三大分区png.png
bg2015120901.png
下面介绍一些git里面比较常见的命令行代码
01-git-help

git help
git help -a //显示出所有的命令
git help -g //显示使用手册
git help add //查看某个命令的帮助

02-git-config

针对用户设置
git config --global user.name 'name' //全局配置用户名称
git config --list
git config --help
git config unset --global user.name //重置name
git config --global user.email '[email protected]' //全局配置用户邮箱
git config --global color.ui true //配置git输出的颜色
cat ~/.gitconfig //查看git配置
git config user.name 'name' //配置用户名称
git config user.email '[email protected]' //配置用户邮箱

03-git-init

mkdir '文件名'
git init //初始化
cd .git //查看.git相关信息
open .git //在mac上使用终端显示隐藏的.git

04-git-commit

git status //查看当前状态
git add fileName //将Untracked files(工作区的文件)添加到暂存区
git add . //添加所有修改的文件
-> changes to commited:
git commit -m '添加描述' //将暂存区的文件添加到版本库
git log //查看以往的提交

git add fileName
git commit --amend // 会将修改合并到上一次提交,不会产生新的提交

05-git-diff

git status //查看当前状态
-> Change not staged for commit
git diff [file_name] //工作区 vs 暂存区查看修改前后的区别 (diff=difference)
git diff --staged //repositore和暂存区的差异
git diff head //工作区 vs 版本库
git diff --cached //暂存区 vs 版本库

06-git-rename

filename 修改名称
git rm 'filename'
git add 'new filename'
git status

07-git-mv

git mv 'old filenmae' 'newfilename' //修改文件名称
mkdir 'filename' //新建目录
git mv 'filename' '新的目录' //移动文件到新的目录

08-git-rm

git rm asset/css/o.css //删除repository的文件

09-git-head

git rm filename //删除repository的文件
git checkout HEAD -- filename //恢复到最近的一次提交
执行commit之后的提交
git checkout HEAD^ -- filename //恢复到上一次提交

10-git-revert

git log --oneline //一行显示所有提交的日志
git revert id //恢复该id的提交
注意
如果你最近的修改和要撤消的修改有重叠(overlap),那么就会被要求手工解决冲突(conflicts)

git revert HEAD 撤销前一次 commit
git revert HEAD^ 撤销前前一次 commit
git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。

11-git-reset

reset //指向之前的某一次提交
--soft //repository重置到指定状态,不会影响到工作区和暂存区
--mixed //repository重置到指定状态,保留工作区暂存区重置到到指定的状态
--hard //repository重置到指定状态,工作区和暂存区直接重置到指定的状态
git log --online //查看提交日志
git reset --soft id
git reset --mixed id
git reset --hard id

使用git push -f提交更改:
此时如果用git push会报错,因为我们本地库HEAD指向的版本比远程库的要旧`

git reset --hard origin/master
抛弃我所有的分阶段和未分阶段的更改,忘记当前的本地分支上的一切,使它与origin / master完全相同。

12-git-branch

git status //查看分支
*master //*表示在当前分支
git branch branchName //创建分支
git branch branchName commitid //根据commitid创建分支
git checkout commitid -b branchName //本地新拉出分支的名称
git brance //查看项目分支

13-git-checkout

git checkout 分支name //切换分支
git log --oneline --decorate //每行日志显示分支名称

14-git-branch-diff

git diff 分支名称1..分支名称2 //对比两个分支的差异
f //向下翻页
git diff 分支名称1..分支名称2 filename //某个分支对比两个分支的差异

15-git-fast-forward

子分支未进行add. commit之后

16-git-merge

主分支和子分支进行commit之后
git log --oneline --decorate --all -10 --graph //查看所有分支的10信息
git merge 子分支 //将子分支commit的文件合并当前分支

17-git-conflict

//在文件里面找到冲突代码
A合并到B
-<<<<<<<<< HEAD
B的代码
-=============
A的代码

->>>>>>>>>>>>>

18-git-rm-branch

git branch -m 分支名 新的分支的名字 //修改分支名字
git branch -d 分支名 //删除分支
git branch //查看本地分支
git branch -a //查看本地远程分支
git branch -r //查看远程分支

git checkout 分支名 ``
git push origin --delete [branch_name] //删除远程分支

19-git-stash

git stash save '描述信息' //暂时保存工作区的内容到暂存区
git stash list //显示缓存的stash列表
git stash show //查看缓存的stash,默认第一个
git stash show -p stash@{0} //查看缓存的stash@{0}
git stash apply stash@{0} 恢复缓存的stash@{0}
git stash pop 恢复工作区的内容,删除缓存的stash,默认第一个
git stash pop stash@{0} 恢复工作区的内容,删除缓存的stash@{0}
git stash drop stash@{0} 删除缓存的stash@{0}

git stash clear 删除所有缓存的stash

20-git-log

git log f向下 b向上
git log --online -5 //指定显示行数
git log --online --author="name" //指定作者
git log --online --grep='index.html' //指定包含index.html
git log --online --before='2014-07-05' //指定某个时间之前的提交
git log --online --before='3 days' //指定3天之前的提交
git log --online --before='1 week' //指定1周之前的提交
git log --online --graph //加上图像效果

git reflog 和 git log 区别

1、git log 是线上和本地的提交记录

2、git reflog(reference log参考日志的缩写)是本地的操作记录,除了提交还有删除/merge等记录,一般用于获取回滚的commitid。

21-git-alias

git config --global alias.co checkout //checkout命令添加别名co
cat ~/.gitconfig
vim ~/.bashprofile //通过系统添加别名

22-git-ignore

git config --global core.excludesfile ~/.gitignore_global //添加忽略文件
vim ~/. gitignore_global //编辑忽略文件
.DS_Store

23-git-gitgnore

为项目添加忽略文件
vim gitignore

24-git-remote

git remote //远程版本库

25-git-origin

git remote add origin xxx地址 //将本地的git提交至远程

26-git-push

git push -u origin master //提交master分支
git branch -a //查看本地分支
git branch -r //查看远程分支

27-git-remove-workflow

fork //继续开发

28-git-clone

git clone xxx //将代码从远程库克隆到本地
git clone xxx 文件夹名 //将代码从远程库克隆到本地并创建相应的文件夹

29-git-fetch

git fetch //拉去代码
git merge orgin/master //合并分支

30-git-fork

fork 基于项目继续开发

31-git-pull-request

pull-request //提交文件修改到主项目

32-git-collaborator

项目协作者

33-git-github-tools

github-tools git图像化工具

34- 解决revert误操作

git reflog //查看本地commit记录,找到对应的commit id
git reset --hard [commit id] //回退到某个版本

35-git-tag

git tag //创建本地tag
git push origin //推送到远程仓库
git push origin --tags //若存在很多未推送的本地标签,一次全部推送
git show //查看本地某个 tag 的详细信息
git tag 或者 git tag -l //查看本地所有 tag
git ls-remote --tags origin//查看远程所有 tag
git tag -d //本地 tag 的删除
git push origin ://远程 tag 的删除
git checkout -b //检出标签
git tag -a -m "XXX..." //可以指定标签信息
git tag -a v0.1.0 -m "release 0.1.0 version" //创建附注标签
git checkout [tagname] //切换标签·

36-修改 .gitignore 文件生效
#清除缓存  
git rm -r --cached .    
#重新trace file       
git add .      
#提交和注释            
git commit -m "update .gitignore" 
#可选,如果需要同步到remote上的话  
git push origin master
37-远程分支删除、本地分支更新
git remote update origin -p

你可能感兴趣的:(git-命令行学习)