一、git常用的基础命令
1.1 git安装 / 配置
yum install git -y
#配置用户信息
git config --global user.name "oldhei"
git config --global user.email "123456@qq.com"
git config --global color.ui true
[root@memcache137 ~]# git config --list
user.name=oldhei
user.email=123456@qq.com
color.ui=true
#配置完后,也可在家目录有对应的git配置信息
[root@memcache137 ~]# cat /root/.gitconfig
[user]
name = oldhei
email = 123456@qq.com
[color]
ui = true
1.2初始化git
cd /root/shellDir/git_data
git init
1.3 git 常规使用
#查看仓库/工作区状态
git status
touch a b c
git status
#提交文件a到暂存区域
git add a
#添加目录中所有改动过的文件
git add .
在上图,有提示指令说,撤出暂存区
#将文件C 撤出暂存区
git rm --cached c
#同时删除工作目录和暂存区文件
git rm -f b
# 提交
git commit -m "commit all file a b c"
#此时再看状态 就是 nothing to commit, working directory clean
git status
1.4 git修改文件名称方法 git mv
#这里认为
#改名可这样操作:
git mv a.txt a
git commit -m "modify file a"
1.5 git diff文件比对
# 默认比对工作目录和暂存区有什么不同
git diff
#比对暂存区域和本地仓库
git diff --cached
#如果某个文件已经被仓库管理,如果在此更改此文件,这样写即可
git commit -am "add newfile"
1.6 git log 查看历史提交的信息
#显示详细信息
git log
#一行式显示信息
git log --oneline
##一行式显示信息且显示当前指针
git log --oneline --decorate
git log -p #显示具体内容变化
git log -1 #显示一条内容
1.7 git reset 回滚数据到某一个提交点
git reset --hard d77fdf8
1.8 git reflog 显示所有历史信息
#显示所有历史信息
git reflog
1.9 git代码合并
1.10 git branch创建分支
#查看分支
git branch
#创建一个分支,名为 testing
git branch testing
做实验:
touch aaa bbb ccc
git add aaa
git commit -m "add aaa"
git add bbb
git commit -m "add bbb"
git add ccc
git commit -m "add ccc"
git log --oneline --decorate
此时master指向的是"add ccc" 最后一次提交的。 下图是要走的操作
1.12 git checkout切换分支
git checkout -b testing # 创建并切换到testing分支
git checkout testing
git branch
#删除分支
git branch -d testing
#切到 testing分支,执行操作
git checkout testing
touch test-ddd
git add .
git commit -m "add new file ddd by testing"
下图可知,现在HEAD指针,是指向testing分支的。
#切回master分支
touch master-eee
git add .
git commit -m "add file master-eee"
1.13 git merge 合并
#切回master分支
git merge testing
此时会弹出下面内容,写注释,然后:wq 保存即可。
1.14 git 代码冲突合并
此时我们要去修改冲突的文件 a ,删掉/处理/二者都保留内容。
修改完后 git commit -am "merge testing " 即可
1.15 git tag 标签使用
#创建tag v1.0
git tag -a v1.0 b2ca6be -m "tab v1.0 create "
# 查看所有tag
git tag
#删除标签
git tag -d v1.0
# 查看 v1.0 更改过什么内容
git show v1.0
#利用标签进行回滚
git reset --hard v1.0
小结
参考来源:
BV1pJ411R7ci 李老师
朱哥 https://www.zsythink.net/archives/category/%e8%bf%90%e7%bb%b4%e7%9b%b8%e5%85%b3/git