Git安装与常用命令

Git简介:

  • Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或大或小的项目。
  • Git是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源代码的版本控制软件。
  • Git与常用的版本控制工具CVS、Subversion等不同,它采用了分布式版本库的方式,不用服务器端软件支持。

安装Git

1.yum安装

[root@git ~]# yum install git -y

2.编译安装

Git下载地址: Releases · git/git · GitHub

# 安装依赖关系
[root@git ~]# yum install curl-devel expat-devel gettext-devel  openssl-devel zlib-devel autoconf gcc perl-ExtUtils-MakeMaker
# 编译安装 
[root@git ~]# tar -zxf git-2.0.0.tar.gz
[root@git ~]# cd git-2.0.0
[root@git ~]# ./configure --prefix=/usr/local/git # 没有文件可以略过
[root@git ~]# make  
[root@git ~]# make install  

初次运行Git前配置

[root@gitlab ~]# git config --global user.name "用户名"  #配置git使用用户
[root@gitlab ~]# git config --global user.email "邮箱"  #配置git使用邮箱
[root@gitlab ~]# git config --global color.ui true  #语法高亮
[root@gitlab ~]# git config --list # 查看全局配置
user.name=cc
[email protected]
color.ui=true

查看生成的配置文件

[root@gitlab ~]# cd
[root@gitlab ~]# cat .gitconfig 
[user]
        name = newrain
        email = [email protected]
[color]
    ui = true

在A机器上创建裸库

[root@gitlab ~]# useradd git
[root@gitlab ~]# passwd git
[root@gitlab ~]# mkdir /git-root/
[root@gitlab ~]# cd /git-root/
[root@gitlab git-root]# git init --bare shell.git
Initialized empty Git repository in /git-root/shell.git/
[root@gitlab git-root]# chown -R git:git shell.git

在B机器上创建本地库

[root@gitlab opt]# ssh-keygen
[root@gitlab opt]# ssh-copy-id [email protected]
[root@gitlab opt]# git clone [email protected]:/git-root/shell.git
[root@gitlab opt]# ls
rh  shell
[root@gitlab opt]# cd shell/
[root@gitlab shell]# vim test1.sh
[root@gitlab shell]# git add test1.sh
[root@gitlab shell]# git commit -m 'first commit'
[master (root-commit) 33c5fbf] first commit
 1 file changed, 2 insertions(+)
 create mode 100644 test1.sh
[root@gitlab shell]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/git-root/shell.git
 * [new branch]      master -> master

Git命令常规操作

命令 命令说明
#add 添加文件内容至索引
bisect 通过二分查找定位引入bug的变更
#branch 列出,创建或删除分支
#checkout 检出一个分支或路径到工作区
#clone 克隆一个版本库到一个新目录
#commit 记录变更到版本库
#diff 显示提交之间,提交和工作区之间等的差异
fetch 从另外一个版本库下载对象和引用
grep 输出和模式匹配的行
#init 创建一个空的
#log 显示提交日志
#merge 合并两个或更多开发历史
#mv 移动或重命名一个文件,目录或符号链接
#pull

获取并合并另外的版本库或一个本地分支

#push 更新远程引用和相关的对象
rebase 本地提交转移至更新后的上游分支中
#reset 重置当前HEAD到指定状态
#rm 从工作区和索引中删除文件
show 显示各种类型的对象
#status 显示工作区状态
# tag 创建,列出,删除或校验一个GPG签名的tag对象
  • git init:在当前目录初始化一个新的 Git 仓库。
  • git clone <仓库地址>:克隆远程仓库到本地。
  • git add <文件名>:将指定文件添加到暂存区。
  • git commit -m "<提交信息>":将暂存区中的文件提交到本地仓库,并附上提交信息。
  • git push:将本地仓库的提交推送到远程仓库。
  • git pull:从远程仓库拉取最新的改动到本地仓库。
  • git status:查看工作区和暂存区的状态,显示文件的变更情况。
  • git log:查看提交历史记录。
  • git branch:列出所有分支,当前分支前面会有一个星号。
  • git checkout <分支名>:切换到指定分支。
  • git merge <分支名>:将指定分支合并到当前分支。
  • git remote add origin <仓库地址>:将本地仓库与远程仓库关联起来。
  • git remote -v:查看当前关联的远程仓库地址。
  • git diff:查看当前文件与暂存区或者本地仓库的差异。
  • git reset <文件名>:将指定文件从暂存区移除,

git操作示意图

Git安装与常用命令_第1张图片

你可能感兴趣的:(git)