git

Posted XY

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git相关的知识,希望对你有一定的参考价值。

1.git官网

https://www.git-scm.com/

2.git安装

[[email protected] ~]# yum install git

3.用户配置

[[email protected] ~]# git config --global user.name "xiaoyi"                  # 创建用户
[[email protected] ~]# git  config --global user.email "[email protected]"      # 配置邮箱
[[email protected] ~]# git config --global color.ui true                       # 配置颜色
[[email protected] ~]# git config --list                       # 查看配置
user.name=xiaoyi
user.email=[email protected]
color.ui=true
[[email protected] ~]# mkdir xiao         # 创建仓库目录
[[email protected] ~]# cd xiao/
[[email protected] xiao]# git init        # 初始化仓库
Initialized empty Git repository in /root/xiao/.git/
[[email protected] xiao]# ll -a
total 12
drwxr-xr-x. 3 root root 4096 Jun 14 10:06 .
dr-xr-x---. 4 root root 4096 Jun 14 10:06 ..
drwxr-xr-x. 7 root root 4096 Jun 14 10:06 .git

4.创建文件,并提交

[[email protected] xiao]# echo "1 hehe" > readme.txt   # 创建文件                                                                                                                                                                                                                       
[[email protected] xiao]# cat readme.txt     # 查看文件
1 hehe
[[email protected] xiao]# git status         # 查询
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       readme.txt
nothing added to commit but untracked files present (use "git add" to track)
[[email protected] xiao]# git add readme.txt # 加入到暂存区
[[email protected] xiao]# git status         # 查询
# On branch master   
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   readme.txt
#
[[email protected] xiao]# git commit -m "the first commit"  # 提交到仓库
[master (root-commit) 3511675] the first commit
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 readme.txt

5.提交第二个文件

[[email protected] xiao]# echo "xiao" > deploy.sh  # 创建文件
[[email protected] xiao]# git add deploy.sh        # 提交到暂存区
[[email protected] xiao]# git status               # 查看状态
[[email protected] xiao]# git commit -m "2th"      # 提交
[[email protected] xiao]# git log                  # 查看日志

6.修改文件

[[email protected] xiao]# echo 2 >> deploy.sh    # 追加文件
[[email protected] xiao]# git status      
[[email protected] xiao]# git add deploy.sh 
[[email protected] xiao]# git commit -m "add 2"
[[email protected] xiao]# git log               # 查看日志
commit cc7913913c2f1d037dab05ee0a5e50b5ec8b9d36
Author: xiaoyi <[email protected]>
Date:   Tue Jun 14 10:30:09 2016 +0800

    add 2

commit c91cad8dbe07c8671c5eed33073d666ea6ce26ad
Author: xiaoyi <[email protected]>
Date:   Tue Jun 14 10:25:09 2016 +0800

    2th

commit 3511675998635b5bbc3e8384cdf3867b57a96d0a
Author: xiaoyi <[email protected]>
Date:   Tue Jun 14 10:13:28 2016 +0800

    the first commit

7.版本回退

[[email protected] xiao]# cat deploy.sh          # 当前版本内容
xiao
2
[[email protected] xiao]# git reset --hard HEAD^ # 一个^回退上一个版本,二个^^回退前二个版本
HEAD is now at c91cad8 2th
[[email protected] xiao]# cat deploy.sh 
xiao

8.回退到指定版本

[[email protected] xiao]# git reflog            # 查看flog
c91cad8 [email protected]{0}: HEAD^: updating HEAD
cc79139 [email protected]{1}: commit: add 2
c91cad8 [email protected]{2}: commit: 2th
3511675 [email protected]{3}: commit (initial): the first commit
[[email protected] xiao]# git reset --hard cc79139   # 回退到这个版本

 9.版本迁出

git checkout -- readme.txt

 

以上是关于git的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段15——git命令操作一个完整流程

如何管理在每个 git 版本中添加私有代码片段?

使用 Git 来管理 Xcode 中的代码片段

markdown Git代码片段

GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段

GitGit 分支管理 ( 克隆远程分支 | 克隆 master 分支 git clone | 查看远程分支 git branch -a | 克隆远程分支 git checkout -b )(代码片段