微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

# Git 基础命令

第一章

第二章

2.1Git安装

2.1.1 Git源码安装

2.1.1 Git 源码包下载地址

https://mirrors.edge.kernel.org/pub/software/scm/git/

2.1.2 Git 安装步骤

##源码编译git需要先安装支持git的依赖包
[root@Git02 ~]# yum -y install curl-devel expat-devel gettext-devel zlib-devel perl-ExtUtils-Embed
[root@Git02 ~]# tar xf git-2.9.5.tar.gz -C /usr/src/
[root@Git02 src]# cd /usr/src/git-2.9.5/
[root@Git02 git-2.9.5]# ./configure --prefix=/usr/local/git &>/dev/null && make &>/dev/null && make install &>/dev/null
##做软链接
[root@Git02 git-2.9.5]# ln -s /usr/local/git/bin/* /usr/bin
[root@Git02 git-2.9.5]# ln -s /usr/libexec/git-core/* /usr/bin
##这里需要用git2.9.5版本的git,这样才有svn到git的数据迁移的一些命令
[root@Git02 git-2.9.5]# find / -type f -name 'git-svn'
/usr/local/git/libexec/git-core/git-svn
/usr/src/git-2.9.5/git-svn
[root@Git02 git-2.9.5]# ln -s /usr/local/git/libexec/git-core/git-svn /usr/bin/git-svn
[root@Git02 git-2.9.5]# which git-svn
/usr/bin/git-svn

2.2 Git 命令基础

[root@Git01 git]# LANG=zh_CN.UTF8
[root@Git01 git]# git
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

这些是各种场合常见的 Git 命令:

开始一个工作区(参见:git help tutorial)
   clone      克隆一个仓库到一个新目录
   init       创建一个空的 Git 仓库或重新初始化一个已存在的仓库

在当前变更上工作(参见:git help everyday)
   add        添加文件内容至索引
   mv         移动或重命名一个文件、目录或符号链接
   reset      重置当前 HEAD 到指定状态
   rm         从工作区和索引中删除文件

检查历史和状态(参见:git help revisions)
   bisect     通过二分查找定位引入 bug 的提交
   grep       输出和模式匹配的行
   log        显示提交日志
   show       显示各种类型的对象
   status     显示工作区状态

扩展、标记和调校您的历史记录
   branch     列出、创建或删除分支
   checkout   切换分支或恢复工作区文件
   commit     记录变更到仓库
   diff       显示提交之间、提交和工作区之间等的差异
   merge      合并两个或更多开发历史
   rebase     在另一个分支上重新应用提交
   tag        创建、列出、删除或校验一个 GPG 签名的标签对象

协同(参见:git help workflows)
   fetch      从另外一个仓库下载对象和引用
   pull       获取并整合另外的仓库或一个本地分支
   push       更新远程引用和相关的对象

命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些概念帮助。
查看 'git help <命令>' 或 'git help <概念>' 以获取给定子命令或概念的
帮助。

2.2.1 Git init 初始化GIT工作目录

[root@Git01 ~]# mkdir -p /mycode
[root@Git01 ~]# cd /mycode
[root@Git01 mycode]# git init
初始化空的 Git 仓库于 /mycode/.git/
[root@Git01 mycode]# ls -la
总用量 0
drwxr-xr-x   3 root root  18 6月  11 15:46 .
dr-xr-xr-x. 18 root root 238 6月  11 15:46 ..
drwxr-xr-x   7 root root 119 6月  11 15:46 .git

2.2.2 Git add 将文件提交到暂存区

[root@Git01 mycode]# touch test.txt
[root@Git01 mycode]# git add test.txt 
[root@Git01 mycode]# git status
位于分支 master

初始提交

要提交的变更:
  (使用 "git rm --cached <文件>..." 以取消暂存)

	新文件:   test.txt

2.2.3 Git commit 将暂存区的文件提交到本地仓库

[root@Git01 mycode]# git commit -m "test.txt"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'root@Git01.(none)')

这里报错是因为之前没有做全局变量

[root@Git01 mycode]# git config --global user.email "1399523522@qq.com"
[root@Git01 mycode]# git config --global user.name "Rock"
[root@Git01 mycode]# git commit -m "test.txt"
[master(根提交) 4889630] test.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git01 mycode]# git status
位于分支 master
nothing to commit, working tree clean

2.2.4 Git remote 用于管理远程仓库

# 查看远程仓库
[root@Git01 mycode]# git remote -v
[root@Git01 mycode]# git remote add --help
用法:git remote add [<选项>] <名称> <地址>

    -f, --fetch           抓取远程的分支
    --tags                抓取时导入所有的标签和关联对象
                          或不抓取任何标签(--no-tags)
    -t, --track <分支>    跟踪的分支
    -m, --master <分支>   主线分支
    --mirror[=<push|fetch>]
                          把远程设置为用以推送或抓取的镜像
  • 创建远程仓库,如下图所示:

    DB51CE03FBB7D7B77E7E93D109252FC7.png-85.4kB


    4FEE3A8B06DF482569B733BFE37B3E21.png-120.2kB

[root@Git01 mycode]# git remote add origin
# origin 为代号,如果不设置认的代号为origin
[root@Git01 mycode]# git remote add test https://github.com/Rock940120/rock.git
# 这里 test 为自己设置的代号
[root@Git01 mycode]# git push -u test master
Username for 'https://github.com': m15107210025@163.com
Password for 'https://m15107210025@163.com@github.com': 
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 202 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 test 的远程分支 master。
  • 上传成功后,可以在远程仓库刷新后看到传上来的test.txt,如图:

    96C3A6D44C5FB434378B43C047903DCB.png-118.9kB

2.2.5 Git clone 克隆一个现有仓库到本地

新创建一个Git02,在Git02上来模拟克隆主分支上的master版本

[root@Git02 ~]# mkdir /mycode
[root@Git02 ~]# cd /mycode
[root@Git02 mycode]# git clone https://github.com/Rock940120/rock.git master
cloning into 'master'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
[root@Git02 mycode]# ll -ld *
drwxr-xr-x 3 root root 34 Jun 11 16:52 master
[root@Git02 mycode]# cd master/
[root@Git02 master]# ls
test.txt
[root@Git02 master]# git status
# On branch master
nothing to commit, working directory clean
[root@Git02 master]# echo "welcome to BeiJing" > test.txt 
[root@Git02 master]# LANG=zh_CN.UTF8
[root@Git02 master]# git add test.txt 
[root@Git02 master]# git status
# 位于分支 master
# 要提交的变更:
#   (使用 "git reset HEAD <file>..." 撤出暂存区)
#
#	修改:      test.txt

[root@Git02 master]# git config --global user.email "1399523522@qq.com"
[root@Git02 master]# git config --global user.name "Rock"
## 提交到本地仓库
[root@Git02 master]# git commit -m "user02"
[master 64c448b] user02
 1 file changed, 1 insertion(+)
[root@Git02 master]# git status
# 位于分支 master
# 您的分支领先 'origin/master' 共 1 个提交。
#   (使用 "git push" 来发布您的本地提交)
#
无文件要提交,干净的工作区
  • 查看远程仓库

[root@Git02 master]# git remote -v
origin	https://github.com/Rock940120/rock.git (fetch)
origin	https://github.com/Rock940120/rock.git (push)
## 这里没有设置远程仓库,克隆下来代号认为origin,

## 推送到远程仓库
[root@Git02 master]# git push -u origin master
Username for 'https://github.com': m15107210025@163.com
Password for 'https://m15107210025@163.com@github.com': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 249 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   4889630..64c448b  master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。

  • 查看远程仓库,看代号为users,版本已经更新,如下图所示:

    ![1.png-83.7kB]

2.2.6 Git fetch 将远程仓库的某个版本同步到本地仓库

修改git02的test后,将test提交到远程仓库,然后在git01拉取到git01的本地仓库。
[root@Git01 mycode]# cat test.txt 
[root@Git01 mycode]# git remote -v
test	https://github.com/Rock940120/rock.git (fetch)
test	https://github.com/Rock940120/rock.git (push)
[root@Git01 mycode]# git fetch
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
展开对象中: 100% (3/3), 完成.
来自 https://github.com/Rock940120/rock
   4889630..64c448b  master     -> test/master
[root@Git01 mycode]# cat test.txt
###这里只是同步到本地仓库,工作目录文件没有改变
###如果将某个用户的某个分支同步,下面命令
[root@Git01 mycode]# git fetch -u test master

2.2.7 Git merge 将本地仓库同步到工作目录

#将git01本地仓库同步到本地目录
[root@Git01 mycode]# git status
位于分支 master
您的分支落后 'test/master' 共 1 个提交,并且可以快进。
  (使用 "git pull" 来更新您的本地分支)
nothing to commit, working tree clean
[root@Git01 mycode]# git merge test/master
更新 4889630..64c448b
fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)

2.2.7 Git pull 将远程仓库的变更拉取到本地仓库,并更新本地工作目录

git pull 相当于 git fetch + git merge
开发一般不会用这种方法拉取代码,因为一旦有版本冲突,这样就没办法修改程序的。

[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
[root@Git01 mycode]# echo "git01" >> test.txt 
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
尚未暂存以备提交的变更:
  (使用 "git add <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	修改:     test.txt

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@Git01 mycode]# git add test.txt 
[root@Git01 mycode]# git commit -m "git01"
[master f28df52] git01
 1 file changed, 1 insertion(+)
[root@Git01 mycode]# git push -u test master
Username for 'https://github.com': Rock940120
Password for 'https://Rock940120@github.com': 
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 254 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   64c448b..f28df52  master -> master
分支 master 设置为跟踪来自 test 的远程分支 master。

###
账号:Rock940120
密码:wl545355.

在Git02直接拉取代码
[root@Git02 master]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@Git02 master]# git remote -v
origin	https://github.com/Rock940120/rock.git (fetch)
origin	https://github.com/Rock940120/rock.git (push)
[root@Git02 master]# git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
来自 https://github.com/Rock940120/rock
 * branch            master     -> FETCH_HEAD
更新 64c448b..f28df52
fast-forward
 test.txt | 1 +
 1 file changed, 1 insertion(+)
[root@Git02 master]# cat test.txt 
welcome to BeiJing
git01

2.2.8 Git mv && Git reset 暂存区文件修改和撤销

[root@Git01 mycode]# touch change
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	change

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@Git01 mycode]# git add change 
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   change

[root@Git01 mycode]# mv change changed
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   change

尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     change

未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	changed

[root@Git01 mycode]# git add changed 
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   change
	新文件:   changed

尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     change

[root@Git01 mycode]# git rm change
rm 'change'
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   changed

[root@Git01 mycode]# git mv changed change
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   change

[root@Git01 mycode]# git reset change 
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	change

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@Git01 mycode]# git add *
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   change


2.2.9 Git diff 文件对比

将本地工作目录的文件与本地仓库中的文件进行对比

[root@Git01 mycode]# git reset change
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	change

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@Git01 mycode]# rm -rf change 
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
nothing to commit, working tree clean
[root@Git01 mycode]# echo "wendy" >> test.txt 
[root@Git01 mycode]# git diff test.txt 
diff --git a/test.txt b/test.txt
index 24c8146..a205cdb 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 welcome to BeiJing
 git01
+wendy
[root@Git01 mycode]# vim test.txt 
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# git diff test.txt 
diff --git a/test.txt b/test.txt
index 24c8146..ecb0ac7 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 welcome to BeiJing
-git01
+wendy

2.2.10 Git log 查看git提交历史纪录

  • Git log 查看提交历史纪录
  • git log -2:查看最近几条记录
  • git log -p -1 : 显示每次提交内容差异
  • git log --stat -2:stat简要显示数据增改行数,这样就能看到提交中修改内容
  • git log -- pretty=oneline:一行显示提交的历史纪录
[root@Git01 mycode]# git log -2
commit f28df52dfa37c6c90573ec677a1b8d104e36641f
Author: Rock <1399523522@qq.com>
Date:   Wed Jun 17 10:47:42 2020 +0800

    git01

commit 64c448b325a650c2402a6e4d6f85a5eef99e9600
Author: Rock <1399523522@qq.com>
Date:   Thu Jun 11 17:02:56 2020 +0800

    user02
[root@Git01 mycode]# git log -p -1
commit f28df52dfa37c6c90573ec677a1b8d104e36641f
Author: Rock <1399523522@qq.com>
Date:   Wed Jun 17 10:47:42 2020 +0800

    git01

diff --git a/test.txt b/test.txt
index 14908ba..24c8146 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1,2 @@
 welcome to BeiJing
+git01
[root@Git01 mycode]# git log --pretty=oneline
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git add *
[root@Git01 mycode]# git commit -m "ceshi"
[master ae81646] ceshi
 1 file changed, 1 insertion(+), 1 deletion(-)
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt

2.3 Git 追根述源-git log

2.3.1 Git还原历史数据

Git服务程序中有个叫HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字符串来区分每个提交版本,另外认的HEAD版本指针会指向到最近的一次提交版本记录,而上一次提交版本会叫HEAD,上上个个版本则会叫做HEAD^,当然一般会用HEAD-5来表示往上数第五个提交版本。

  • git reset --hard HEAD^ #-->还原历史提交版本上一次
  • git reset --hard 4889630383afc854273be11f9c0e320041f0a98f #--> 找到历史还原点的SHA-1值后,就可以还原
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git reset --hard HEAD^
HEAD 现在位于 f28df52 git01
[root@Git01 mycode]# git log --pretty=oneline
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
git01
[root@Git01 mycode]# git reset --hard 4889630383afc854273be11f9c0e320041f0a98f
HEAD 现在位于 4889630 test.txt
[root@Git01 mycode]# cat test.txt

2.3.2 Git reflog 还原未来数据

当git回滚到某个版本提交后,解决办法如下:

  • GIt reflog: 查看未来历史更新点
[root@Git01 mycode]# git log --pretty=oneline
ae81646948b78fea961fe2fd3c252b8e8d0dcc0e ceshi
f28df52dfa37c6c90573ec677a1b8d104e36641f git01
64c448b325a650c2402a6e4d6f85a5eef99e9600 user02
4889630383afc854273be11f9c0e320041f0a98f test.txt
[root@Git01 mycode]# git reflog
ae81646 HEAD@{0}: reset: moving to ae81646
4889630 HEAD@{1}: reset: moving to 4889630383afc854273be11f9c0e320041f0a98f
f28df52 HEAD@{2}: reset: moving to HEAD^
ae81646 HEAD@{3}: commit: ceshi
f28df52 HEAD@{4}: commit: git01
64c448b HEAD@{5}: merge test/master: fast-forward
4889630 HEAD@{6}: commit (initial): test.txt
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy

2.3.3 Git tag 标签使用

前面回滚使用的是一串字符串,又长又难记
git tag <标签> -m "描述"
每次提交可以打一个标签
|命令|说明|
|:-|:-|
|git tag|查看当前所有标签|
|git show V1.0|查看当前v1.0版本的详细信息|
|git tag v1.0 -m "描述"|创建标签|
|git tag -d v1.0|删除v1.0标签|

[root@Git01 mycode]# git tag
[root@Git01 mycode]# git tag v1.0
[root@Git01 mycode]# git tag
v1.0
[root@Git01 mycode]# git show v1.0
commit ae81646948b78fea961fe2fd3c252b8e8d0dcc0e
Author: Rock <1399523522@qq.com>
Date:   Wed Jun 17 11:37:05 2020 +0800

    ceshi

diff --git a/test.txt b/test.txt
index 24c8146..ecb0ac7 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,2 @@
 welcome to BeiJing
-git01
+wendy
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# echo "i miss you" >> test.txt 
[root@Git01 mycode]# git add * 
[root@Git01 mycode]# git commit -m "v1.0"
[master 52fe609] v1.0
 1 file changed, 1 insertion(+)
[root@Git01 mycode]# git status
位于分支 master
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
nothing to commit, working tree clean
[root@Git01 mycode]# git tag v2.0
[root@Git01 mycode]# git show v2.0
commit 52fe609625a9ccea56ef8a3d8932df8474d2579f
Author: Rock <1399523522@qq.com>
Date:   Wed Jun 17 14:27:25 2020 +0800

    v1.0

diff --git a/test.txt b/test.txt
index ecb0ac7..a2a418d 100644
--- a/test.txt
+++ b/test.txt
@@ -1,2 +1,3 @@
 welcome to BeiJing
 wendy
+i miss you
[root@Git01 mycode]# git tag
v1.0
v2.0
[root@Git01 mycode]# git reset --hard v1.0
HEAD 现在位于 ae81646 ceshi
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
[root@Git01 mycode]# git reset --hard v2.0
HEAD 现在位于 52fe609 v1.0
[root@Git01 mycode]# cat test.txt 
welcome to BeiJing
wendy
i miss you

2.3.4 Gitignore 文件

2.png-118kB

[root@Git01 mycode]# vim .gitignore
[root@Git01 mycode]# cat .gitignore 
target
*.log
?.idea
[root@Git01 mycode]# git status
位于分支 master
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	.gitignore

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@Git01 mycode]# git add .gitignore 
[root@Git01 mycode]# git status
位于分支 master
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	新文件:   .gitignore

[root@Git01 mycode]# git commit -m "提交gitignore"
[master 800a025] 提交gitignore
 1 file changed, 3 insertions(+)
 create mode 100644 .gitignore
[root@Git01 mycode]# git push test master
Username for 'https://github.com': Rock940120
Password for 'https://Rock940120@github.com': 
对象计数中: 9, 完成.
压缩对象中: 100% (4/4), 完成.
写入对象中: 100% (9/9), 759 bytes | 0 bytes/s, 完成.
Total 9 (delta 0), reused 0 (delta 0)
To https://github.com/Rock940120/rock.git
   f28df52..800a025  master -> master

3.png-45.7kB

[root@Git01 mycode]# touch 111.log
[root@Git01 mycode]# touch 222.idea
[root@Git01 mycode]# touch 333
[root@Git01 mycode]# git status
位于分支 master
您的分支与上游分支 'test/master' 一致。
未跟踪的文件:
  (使用 "git add <文件>..." 以包含要提交的内容)

	222.idea
	333

提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
[root@Git02 master]# rm -rf /mycode/
[root@Git02 master]# mkdir /mycode
[root@Git02 master]# cd /mycode
[root@Git02 mycode]# git clone https://github.com/Rock940120/rock.git
正克隆到 'rock'...
remote: Enumerating objects: 18, done.
remote: Counting objects: 100% (18/18), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 18 (delta 0), reused 18 (delta 0), pack-reused 0
Unpacking objects: 100% (18/18), done.
[root@Git02 mycode]# ls
rock
[root@Git02 mycode]# cd rock/
[root@Git02 rock]# ls -la
总用量 8
drwxr-xr-x 3 root root  52 6月  17 14:48 .
drwxr-xr-x 3 root root  18 6月  17 14:48 ..
drwxr-xr-x 8 root root 163 6月  17 14:48 .git
-rw-r--r-- 1 root root  20 6月  17 14:48 .gitignore
-rw-r--r-- 1 root root  36 6月  17 14:48 test.txt

第三章

3.1 Git的分支管理

在实际的项目开发中,进来保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。干活都会在dev分支上,每个人从dev分支上创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。

4.png-100.1kB

3.2 Git 本地分支管理

3.2.1 本地分支的创建与切换

命令 说明
git branch 查看当前分支情况
git branch linux 创建分支
git checkout 检查本地分支与远程分支的变更差异
git checkout linux 切换分支
[root@Git01 mycode]# git branch
* master
[root@Git01 mycode]# git branch dev
[root@Git01 mycode]# git branch
  dev
* master
[root@Git01 mycode]# git checkout dev
切换到分支 'dev'
[root@Git01 mycode]# git branch
* dev
  master
[root@Git01 mycode]# ls
111.log  222.idea  333  test.txt
[root@Git01 mycode]# rm -rf 111.log 222.idea 
[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# git status
位于分支 dev
尚未暂存以备提交的变更:
  (使用 "git add/rm <文件>..." 更新要提交的内容)
  (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

	删除:     222.idea

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
[root@Git01 mycode]# git rm 222.idea
rm '222.idea'
[root@Git01 mycode]# git status 
位于分支 dev
要提交的变更:
  (使用 "git reset HEAD <文件>..." 以取消暂存)

	删除:     222.idea

[root@Git01 mycode]# git commit -m "dev1.0"
[dev 7d0b5bf] dev1.0
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 222.idea
[root@Git01 mycode]# git status
位于分支 dev
nothing to commit, working tree clean
[root@Git01 mycode]# git remote -v
test	https://github.com/Rock940120/rock.git (fetch)
test	https://github.com/Rock940120/rock.git (push)
[root@Git01 mycode]# git push -u test dev
Username for 'https://github.com': Rock940120
Password for 'https://Rock940120@github.com': 
对象计数中: 7, 完成.
压缩对象中: 100% (6/6), 完成.
写入对象中: 100% (7/7), 622 bytes | 0 bytes/s, 完成.
Total 7 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/Rock940120/rock/pull/new/dev
remote: 
To https://github.com/Rock940120/rock.git
 * [new branch]      dev -> dev
分支 dev 设置为跟踪来自 test 的远程分支 dev。

5.png-43.8kB

[root@Git01 mycode]# git branch
* dev
  master
[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# git checkout master
切换到分支 'master'
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  test.txt
[root@Git01 mycode]# git checkout dev
切换到分支 'dev'
您的分支与上游分支 'test/dev' 一致。
[root@Git01 mycode]# ls
333  test.txt

切换分支的时候,要清空暂存区,暂存区是公用的

[root@Git01 mycode]# ls
333  test.txt
[root@Git01 mycode]# touch QQ
[root@Git01 mycode]# git add QQ
[root@Git01 mycode]# git checkout master
切换到分支 'master'
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  QQ  test.txt
[root@Git01 mycode]# git checkout dev
A	QQ
切换到分支 'dev'
您的分支与上游分支 'test/dev' 一致。
[root@Git01 mycode]# git commit -m "提交"
[dev ee6a4bc] 提交
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 QQ
[root@Git01 mycode]# git checkout master
切换到分支 'master'
您的分支领先 'test/master' 共 2 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@Git01 mycode]# ls
222.idea  333  test.txt

3.3 本地分支的合并与删除

想把linux的工作成果合并到master分支上
先切换到master分支
git merge dev分支到master
git branch -d dev 确定合并后,可以放心的删掉dev分支

3.3.1 自动合并本地fen分支

[root@Git01 mycode]# git branch
  dev
 * master
[root@Git01 mycode]# git merge dev
更新 a1c2e44..ee6a4bc
fast-forward
 222.idea => QQ | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename 222.idea => QQ (100%)
[root@Git01 mycode]# ls
333  QQ  test.txt
[root@Git01 mycode]# git branch -d dev
warning: 并未删除分支 'dev', 虽然它已经合并到 HEAD,
         然而却尚未被合并到分支 'refs/remotes/test/dev' 。
error: 分支 'dev' 没有完全合并。
如果您确认要删除它,执行 'git branch -D dev'。
[root@Git01 mycode]# git checkout dev
切换到分支 'dev'
您的分支领先 'test/dev' 共 1 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@Git01 mycode]# git branch 
 * dev
  master
[root@Git01 mycode]# git status
位于分支 dev
您的分支领先 'test/dev' 共 1 个提交。
  (使用 "git push" 来发布您的本地提交)
nothing to commit, working tree clean
[root@Git01 mycode]# git push -u test dev 
Username for 'https://github.com': Rock940120
Password for 'https://Rock940120@github.com': 
对象计数中: 2, 完成.
压缩对象中: 100% (2/2), 完成.
写入对象中: 100% (2/2), 223 bytes | 0 bytes/s, 完成.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Rock940120/rock.git
   7d0b5bf..ee6a4bc  dev -> dev
分支 dev 设置为跟踪来自 test 的远程分支 dev。
[root@Git01 mycode]# git checkout master
切换到分支 'master'
您的分支领先 'test/master' 共 4 个提交。
  (使用 "git push" 来发布您的本地提交)
[root@Git01 mycode]# git merge dev
Already up-to-date.
[root@Git01 mycode]# git branch -d dev
已删除分支 dev(曾为 ee6a4bc)。

3.4 Git远程分支管理

3.4.1 将linux本地分支代码标签推送到github远程仓库的linux分支

[root@Git01 mycode]# touch tag.txt
[root@Git01 mycode]# ls
333  QQ  tag.txt  test.txt
[root@Git01 mycode]# 
[root@Git01 mycode]# git add tag.txt 
[root@Git01 mycode]# git commit -m "tag.txt"
[master 032659f] tag.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 tag.txt
[root@Git01 mycode]# git tag v3.0 -m "推送标签测试"
[root@Git01 mycode]# git push -u test v3.0
Username for 'https://github.com': Rock940120
Password for 'https://Rock940120@github.com': 
对象计数中: 3, 完成.
压缩对象中: 100% (3/3), 完成.
写入对象中: 100% (3/3), 369 bytes | 0 bytes/s, 完成.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Rock940120/rock.git
 * [new tag]         v3.0 -> v3.0

6.png-55.4kB

3.4 普通用户创建仓库

企业一般用普通用户创建git仓库,创建只用于作为仓库,不作为工作目录的仓库可以用git --bare init 初始化仓库

  • 在Git01初始化普通用户仓库
[root@Git01 ~]# useradd git
[root@Git01 ~]# echo "666666" | passwd --stdin git
更改用户 git 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[root@Git01 ~]# useradd git
[root@Git01 ~]# echo "666666" | passwd --stdin git
更改用户 git 的密码 。
passwd:所有的身份验证令牌已经成功更新。
[git@Git01 ~]# mkdir repos
[git@Git01 ~]# cd repos
[git@Git01 repos]# mkdir dev webapp benet
[git@Git01 repos]# ls
benet  dev  webapp
[git@Git01 repos]# cd dev
[git@Git01 dev]# git --bare init
初始化空的 Git 版本库于 /home/git/repos/dev/
[git@Git01 repos]# cd dev/
[git@Git01 dev]# ls -la
总用量 12
drwxrwxr-x 7 git git 119 6月  20 13:16 .
drwxrwxr-x 5 git git  44 6月  20 13:15 ..
drwxrwxr-x 2 git git   6 6月  20 13:16 branches
-rw-rw-r-- 1 git git  66 6月  20 13:16 config
-rw-rw-r-- 1 git git  73 6月  20 13:16 description
-rw-rw-r-- 1 git git  23 6月  20 13:16 HEAD
drwxrwxr-x 2 git git 242 6月  20 13:16 hooks
drwxrwxr-x 2 git git  21 6月  20 13:16 info
drwxrwxr-x 4 git git  30 6月  20 13:16 objects
drwxrwxr-x 4 git git  31 6月  20 13:16 refs
  • Git02克隆Git01的普通用户仓库
[root@Git02 ~]# mkdir /mycode
[root@Git02 ~]# cd /mycode
[root@Git02 mycode]# git init
初始化空的 Git 版本库于 /mycode/.git/
[root@Git02 mycode]# git clone git@192.168.43.4:/home/git/repos/dev
正克隆到 'dev'...
The authenticity of host '192.168.43.4 (192.168.43.4)' can't be established.
ECDSA key fingerprint is SHA256:4c6EfrB36b2OGYchGBiXZdK7nboDcpfQ2h8LIsGEGKk.
ECDSA key fingerprint is MD5:db:d1:0d:a6:80:f8:c8:03:e8:e0:27:66:cd:a3:0e:a1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.43.4' (ECDSA) to the list of kNown hosts.
git@192.168.43.4's password: 
warning: 您似乎克隆了一个空版本库。
[root@Git02 mycode]# ls
dev
[root@Git02 mycode]# ls dev/
[root@Git02 mycode]# ls -la dev/
总用量 0
drwxr-xr-x 3 root root  18 6月  20 14:57 .
drwxr-xr-x 4 root root  29 6月  20 14:57 ..
drwxr-xr-x 7 root root 119 6月  20 14:57 .git
[root@Git02 mycode]# cd dev/
[root@Git02 dev]# touch test.txt
[root@Git02 dev]# git add test.txt 
[root@Git02 dev]# git status
# 位于分支 master
#
# 初始提交
#
# 要提交的变更:
#   (使用 "git rm --cached <file>..." 撤出暂存区)
#
#	新文件:    test.txt
#
[root@Git02 dev]# git commit -m "V_01"
[master(根提交) 3107d23] V_01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git02 dev]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@Git02 dev]# git remote -v
origin	git@192.168.43.4:/home/git/repos/dev (fetch)
origin	git@192.168.43.4:/home/git/repos/dev (push)
[root@Git02 dev]# git commit -m "V_01"
[master(根提交) 3107d23] V_01
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
[root@Git02 dev]# git status
# 位于分支 master
无文件要提交,干净的工作区
[root@Git02 dev]# git remote -v
origin	git@192.168.43.4:/home/git/repos/dev (fetch)
origin	git@192.168.43.4:/home/git/repos/dev (push)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐