手机版
你好,游客 登录 注册
背景:
阅读新闻

代码版本控制Git工具使用详解

[日期:2018-04-20] 来源:Linux社区  作者:liuleis [字体: ]

一、Git简介

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

二、Git安装

1.CentOS7安装Git

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

2.查看安装的Git版本

[root@server-1 ~]# git --version
git version 1.8.3.1

3.创建git安装目录并初始化

[root@server-1 ~]# mkdir /data/git/
[root@server-1 ~]# cd /data/git/
[root@server-1 git]# git init
Initialized empty Git repository in /data/git/.git/

初始化后在该目录下会生成.git隐藏目录

[root@server-1 git]# ls -la
total 0
drwxr-xr-x. 3 root root  18 Apr  8 09:07 .
drwxr-xr-x. 5 root root  45 Apr  8 09:05 ..
drwxr-xr-x. 7 root root 119 Apr  8 09:07 .git
[root@server-1 git]# ls .git/
branches  config  description  HEAD  hooks  info  objects  refs

4.新建一个test.txt测试文件

[root@server-1 git]# vim test.txt

123abc

把本地test.txt文件添加到git仓库

[root@server-1 git]# git add test.txt

add后必须执行commit才能真正把文件提交到git仓库里

[root@server-1 git]# git commit -m "add new file 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@server-1.(none)')

修改test.txt

[root@server-1 git]# vim test.txt

123abc
456789

[root@server-1 git]# git add test.txt
[root@server-1 git]# git commit -m "add new file 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@server-1.(none)')

报错信息fatal: unable to auto-detect email address (got 'root@server-1.(none)')解决办法

编辑vi .git/config ,添加如下参数

[user]
 email = root@server-1
 name = server-1

报错解决

再次添加和提交test.txt文件

[root@server-1 git]# git add test.txt
[root@server-1 git]# git commit -m "add new file test.txt"
[master (root-commit) 66455b2] add new file test.txt
 1 file changed, 2 insertions(+)
 create mode 100644 test.txt

查看当前仓库中的状态是否有改动的文件

[root@server-1 git]# git status
# On branch master
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#
#  .test.txt.swp
nothing added to commit but untracked files present (use "git add" to track)

5.查看仓库里面文件版本更新的东西

[root@server-1 git]# git add test.txt
[root@server-1 git]# git commit -m "add new file test.txt"
[master 2654728] add new file test.txt
 1 file changed, 3 insertions(+)
[root@server-1 git]# git status
# On branch master
# Untracked files:
#  (use "git add <file>..." to include in what will be committed)
#
#  .test.txt.swp
nothing added to commit but untracked files present (use "git add" to track)
[root@server-1 git]# git log
commit 26547288f6ad9e70ab842b4848febc6d4ddeb5aa
Author: server-1 <root@server-1>
Date:  Sun Apr 8 11:44:27 2018 +0800

    add new file test.txt

commit 66455b2eaeb1bde8f76b54626d290ac814642723
Author: server-1 <root@server-1>
Date:  Sun Apr 8 10:09:53 2018 +0800

    add new file test.txt
        [root@server-1 git]# git log
commit 26547288f6ad9e70ab842b4848febc6d4ddeb5aa
Author: server-1 <root@server-1>
Date:  Sun Apr 8 11:44:27 2018 +0800

    add new file test.txt

commit 66455b2eaeb1bde8f76b54626d290ac814642723
Author: server-1 <root@server-1>
Date:  Sun Apr 8 10:09:53 2018 +0800

    add new file test.txt
[root@server-1 git]# ^C
[root@server-1 git]# git log
commit 26547288f6ad9e70ab842b4848febc6d4ddeb5aa
Author: server-1 <root@server-1>
Date:  Sun Apr 8 11:44:27 2018 +0800

    add new file test.txt

commit 66455b2eaeb1bde8f76b54626d290ac814642723
Author: server-1 <root@server-1>
Date:  Sun Apr 8 10:09:53 2018 +0800

    add new file test.txt

查看单行显示日志

[root@server-1 git]# git log --pretty=oneline
26547288f6ad9e70ab842b4848febc6d4ddeb5aa add new file test.txt
66455b2eaeb1bde8f76b54626d290ac814642723 add new file test.txt

6.回退版本,撤销已修改的版本

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-04/151973.htm

linux
相关资讯       Git使用 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款