Linux上Git项目版本管理工具的使用(本地)

环境:Centos6.5

1.初始化配置

进入用户文件件
[root@localhost /]# cd usr/
新建一个空的文件夹
[root@localhost usr]# mkdir myGit
进入项目文件夹中
[root@localhost usr]# cd myGit/
初始化用户信息(不是临时的,有自动写入配置文件) /root/.gitconfig 中
[root@localhost myGit]# git config --global user.name "xutigui"
[root@localhost myGit]# git config --global user.email [email protected]

2.提交代码到本地仓库

[root@localhost myGit]# git init
Initialized empty Git repository in /usr/myGit/.git/
新建三个空的文件
[root@localhost testGit]# touch file1 file2 file3
添加单个文件到暂存区
[root@localhost testGit]# git add file1
查看状态
[root@localhost testGit]# git status
添加当前目录中所有文件到暂存区
[root@localhost testGit]# git add .
提交到本地仓库中
[root@localhost testGit]#git commit . -m "新增加三个空的文件file1-3"

3.文件改名重新提交到本地仓库

笨方法

重命名
[root@localhost testGit]# mv file1 file4
删除file1
[root@localhost testGit]# git rm file1
重新添加到本地暂存区
[root@localhost testGit]# git add file4
重新提交到本地仓库
[root@localhost testGit]# git commit file4 -m "rename file1 -> file4"

简单方法

[root@localhost testGit]# git mv file4 file1

4.对比本地工作目录文件内容、暂存区文件内容、本地仓库文件内容的差异

4.1 本地内容file1 与 暂存区 file1 文件内容的对比

[root@localhost testGit]# echo "add context1" >> file1
[root@localhost testGit]# git diff file1
diff --git a/file1 b/file1
index b14018a..9f43c6b 100644
--- a/file1
+++ b/file1
@@ -1 +1,2 @@
 xutigui123456789
+add context1

4.2 暂存区 file1 与 仓库 file1 文件内容的对比

[root@localhost testGit]# git add file1
[root@localhost testGit]# git diff --cached file1
diff --git a/file1 b/file1
index b14018a..9f43c6b 100644
--- a/file1
+++ b/file1
@@ -1 +1,2 @@
 xutigui123456789
+add context1

5.Git回退

5.1 查看提交日志

查看提交日志
[root@localhost testGit]# git log
一行常看所有
[root@localhost testGit]# git log -- oneline
查看最后一条记录
[root@localhost testGit]# git log -1
查看全部日志
[root@localhost testGit]# git reflog

5.2 本地文件误操作 未提交到暂存区

使用暂存区文件回滚

清空file1
[root@localhost testGit]# > file1
本地文件误操作 未提交到暂存区 使用暂存区文件回滚
[root@localhost testGit]# git checkout -- file1

5.3 本地文件误操作 已经提交到暂存区

使用本地仓库回滚暂存区再回滚本地文件目录

本地文件误操作 已经提交到暂存区 使用本地仓库回滚暂存区在回滚本地文件目录
[root@localhost testGit]# > file1
[root@localhost testGit]# git add file1
[root@localhost testGit]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   file1
#       deleted:    file4
#
[root@localhost testGit]# git reset HEAD -- file1
Unstaged changes after reset:
M       file1
[root@localhost testGit]# git checkout -- file1

5.4 本地文件误操作 已经提交到本地仓库中

使用快照回退

[root@localhost testGit]# echo "roback add context" >> fie1
[root@localhost testGit]# git add file1
[root@localhost testGit]# git commit file1 -m "roback add context -> file1"
[root@localhost testGit]# git reset --hard fe6096e
HEAD is now at fe6096e 新增加三个空的文件file1-3
查看所有快照
[root@localhost testGit]# git reflog --oneline

6.分支管理

默认分支–主干 ->

6.1 创建分支

查看分支
[root@localhost testGit]# git branch
创建分支
[root@localhost testGit]# git branch dev
切换分支
[root@localhost testGit]# git checkout dev
Switched to branch 'dev'
创建分支中的文件
[root@localhost testGit]# touch f1 f2 f3 f4

6.2 合并分支

合并master 
[root@localhost testGit]# git merge master -m "合并主干"
Merge made by recursive.
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file7
 create mode 100644 file8
合并好了测试完毕切换到主干合并分支
[root@localhost testGit]# git checkout master
Switched to branch 'master'
[root@localhost testGit]# git merge dev -m "合并分支dev"

6.3删除分支

模块开发完了以后就可以删除分支
[root@localhost testGit]# git branch -d dev
Deleted branch dev (was d337269).

6.4 分支冲突

新建分支
[root@localhost testGit]# git branch devops
在主干上编辑文件 提交到仓库
[root@localhost testGit]# cat f1
123456445656
mmmmmmmmmmmm
[root@localhost testGit]# git add .
[root@localhost testGit]# git commit . -m "向文件f1中添加内容"
切换到分支devops上编辑同一个文件后合并主干
[root@localhost testGit]# git checkout devops
[root@localhost testGit]# vim f1
[root@localhost testGit]# cat f1
123456445656
dddddddddddd
[root@localhost testGit]# git add .
[root@localhost testGit]# git commit . -m "编辑分支内容之后提交"
[devops 886dcd7] 编辑分支内容之后提交
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost testGit]# git merge master -m "合并修改之后的主干"
Auto-merging f1
CONFLICT (content): Merge conflict in f1
Automatic merge failed; fix conflicts and then commit the result. # 此处报错
[root@localhost testGit]# git status
[root@localhost testGit]# cat f1
<<<<<<< HEAD
iiiiiiiiiiii
=======
123456xutigui
>>>>>>> master
[root@localhost testGit]# vi f1
[root@localhost testGit]# git add .
[root@localhost testGit]# git commit . -m "处理冲突后提交" 

7.Git标签

7.1 基于目前最新的commitId添加标签

[root@localhost testGit]# git tag -a "v1.1.1" -m "拍照"
查看标签
[root@localhost testGit]# git tag
v1.1.1
查看标签内容
[root@localhost testGit]# git show v1.1.1

7.1 关联指定的commitId添加tag

[root@localhost testGit]# git tag -a "v1.0.0" fe6096e -m "初始化"
[root@localhost testGit]# git tag

7.2 删除标签

[root@localhost testGit]# git tag -d V1.1
Deleted tag 'V1.1' (was 3592eda)

8.克隆/拉取/推送

8.1克隆

通过HTTPS协议克隆

git clone https://gitee.com/zxzllyj/sample-project.git

通过SSH协议克隆

生成公匙和私匙
$ ssh-keygen -t rsa -C "[email protected]"
查看是否成功
$ ssh -T git@github.com
克隆
git clone git@gitee.com:zxzllyj/sample-project.git

8.2拉取

git remote add [shortname] [url]

8.3推送

查看主机名
git remote show
查看主机信息
git remote show origin
推送本地仓库到远程仓库中
git push -u origin master

猜你喜欢

转载自blog.csdn.net/qq_39286483/article/details/105357785
今日推荐