git

  1.安装git

#linux版
[root@localhost ~]# yum install git -y
#windowns版
https://github.com/git-for-windows/git/releases/download/v2.25.1.windows.1/Git-2.25.1-64-bit.exe
  2.配置git(会在当前用户的家目录下创建一个.gitconfig文件)
#配置用户名
[root@localhost ~]# git config --global user.name "lee"   
#配置用户邮箱
[root@localhost ~]# git config --global user.email "lee@example.com"
[root@localhost ~]# git config --global color.ui true

[root@localhost ~]# cat .gitconfig
[user]
    name = lee
    email = lee@example.com
[color]
    ui = true

  3.创建本地仓库

[root@localhost ~]# mkdir demo
[root@localhost ~]# cd demo/
[root@localhost demo]# git init
初始化空的 Git 版本库于 /root/demo/.git/

  

  • git常用命令
   add        #添加文件到暂存区
   bisect     通过二分查找定位引入 bug 的变更
   branch     列出、创建或删除分支
   checkout   检出一个分支或路径到工作区
   clone      克隆一个版本库到一个新目录
   commit     #提交信息到版本库
   diff       显示提交之间、提交和工作区之间等的差异
   fetch      从另外一个版本库下载对象和引用
   grep       输出和模式匹配的行
   init       创建一个空的 Git 版本库或重新初始化一个已存在的版本库
   log        显示提交日志
   merge      合并两个或更多开发历史
   mv         #移动或重命名一个文件、目录或符号链接
   pull       获取并合并另外的版本库或一个本地分支
   push       更新远程引用和相关的对象
   rebase     本地提交转移至更新后的上游分支中
   reset      重置当前HEAD到指定状态
   rm         从工作区和索引中删除文件
   show       显示各种类型的对象
   status     显示工作区状态
   tag        创建、列出、删除或校验一个GPG签名的 tag 对象
  • 创建文件比提交到暂存区
[root@localhost demo]# touch file1 file2 file3
[root@localhost demo]# git add .
  • 将暂存区文件提交到仓库
[root@localhost demo]# git commit -m "新增file1-3"
[master(根提交) aa040d5] 新增file1-3
3 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file1
create mode 100644 file2
create mode 100644 file3
  • 更改文件名称
[root@localhost demo]# git mv file1 file4
  • 本地目录文件和暂存区文件比对
[root@localhost demo]# git diff file4
  • 暂存区文件和本地仓库文件比对
[root@localhost demo]# git diff --cached file4

  • 文件回退

  • 查看git提交日志
#查看所有改动,以一行显示
[root@localhost demo]# git log --oneline
8097089 新改动
aa040d5 新增file1-3

#查看最近一次的改动
[root@localhost demo]# git log -1

#查看最近两次改动
[root@localhost demo]# git log -2
  • 分支
#查看分支
[root@localhost demo]# git branch

#创建分支
[root@localhost demo]# git branch 分支名

#切换分支
[root@localhost demo]# git checkout 分支名

#删除分支,需要先退出当前分支
[root@localhost demo]# git branch -d 分支名

#合并分支
[root@localhost demo]# git checkout master
[root@localhost demo]# git merge 被合并的分支名
  • 标签
#查看所有标签
[root@localhost demo]# git tag

#查看标签详细信息
[root@localhost demo]# git show v1.0

#对当前最新代码打标签
[root@localhost demo]# git tag -a "v1.0" -m "描述信息"

#指定commitID打标签
[root@localhost demo]# git tag -a "v1.0" 42db8caa -m "描述信息"

#删除标签
[root@localhost demo]# git tag -d v1.0

  

  

初学linux,每学到一点东西就写一点,如有不对的地方,恳请包涵!
原文地址:https://www.cnblogs.com/forlive/p/12469720.html