Git——基本操作(三)

一、安装和配置

1.Git安装

yum install git -y

 安装完Git就可以对其做一些配置:

Git有一个工具被称为git config,它允许你获得和设置配置变量;

这些变量可以控制Git的外观和操作的各个方面。这些变量可以被存储在三个不同的位置:
(1)/etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’--system’ 给 git config,它将明确的读和写这个文件。
(2)~/.gitconfig 文件 :具体到你的用户。你可以通过传递--global 选项使Git 读或写这个特定的文件。
(3)位于git目录的config文件 (也就是 .git/config) :无论你当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。

 2.设置用户和邮箱

  当安装完 Git 应该做的第一件事就是设置你的用户名称与邮件地址。 这样做很重要,因为每一个 Git 的提交都会使用这些信息,并且它会写入到你的每一次提交中,不可更改:

git config --global user.name "root"                     
git config --global user.email "root@localhost"

3.获取帮助

   若你使用 Git 时需要获取帮助,有三种方法可以找到 Git 命令的使用手册:

git  --help             \获取git命令信息
git help config     \获取具体命令帮助信息

二、Git的简单使用

(1)创建工作区

# mkdir /git      

(2)创建版本库

# cd /git/
# git init                                    
Initialized empty Git repository in /git/.git/

(3)版本提交

# cp /etc/passwd .                   \注意文件必须要在工作区中
# git status                             \查看有无变化
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:                 \没跟踪的文件
    #   (use "git add <file>..." to include in what will be committed)
    #
    #    passwd
    nothing added to commit but untracked files present (use "git add" to track)
# git add passwd                                \将文件添加到暂存区
# git reset HEAD passwd                    \将已添加到暂存区的文件从暂存区撤除
# git commit -m "add passwd"          \将文件提交到版本库 -m后为描述信息
    [master (root-commit) c30efe0] add passwd
     1 files changed, 1 insertions(+), 0 deletions(-)
     create mode 100644 passwd
# git status
    # On branch master
    nothing to commit (working directory clean)
# echo bbbbbbb >> passwd
# git status
    # On branch master
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)

(4)版本比较

版本比较有两种情况:

a.未添加到暂存区

# git diff passwd                        \将工作区和最近一次版本进行比较

b.已添加到暂存区

# git diff --cached passwd         \将暂存区和最近一次版本进行比较
        
# git add passwd                          \跟前面一样两步提交
# git commit -m "append bbb..."                    
     [master 5630321] append bbb...
     1 files changed, 1 insertions(+), 0 deletions(-)

(5)查看修改记录

# git log 
    commit 5630321b52cd9218359685ea9092ab495de545ee
    Author: root <root@localhost>
    Date:   Fri Feb 17 01:04:12 2017 +0800

        append bbb...

    commit c30efe02cc6db7f35446c1b3036531a6e039b59b
    Author: root <root@clone12.uplooking.com>
    Date:   Fri Feb 17 00:50:42 2017 +0800

        add passwd
# git log --pretty=oneline                    \简洁显示(只显示版本号和描述信息) 
    5630321b52cd9218359685ea9092ab495de545ee append bbb...
    c30efe02cc6db7f35446c1b3036531a6e039b59b add passwd

(6)撤销修改

#git checkout -- filename

撤销修改分两种情况:
  a. 如果在工作区进行了修改,但未添加到暂存区,撤销后将回到修改前的状态
  b. 如果进行修改后已添加到暂存区又进行了修改,撤销后将回到暂存区的状态

(7)版本回退

# git reset --hard HEAD^         \回退到上一个版本 ,如果是上两个版本就接两个^^, ...
    HEAD is now at c30efe0 add passwd
# cat passwd   \下面添加的内容没有了

如果版本太多可用:

# git log --pretty=oneline
    c5076eb443e128fff13e076575433b8c8868055f change bin upper
    6fffe8ab3a5bde75cb25493a73588e92eda4c19c delete root
    c30efe02cc6db7f35446c1b3036531a6e039b59b add passwd
# git reset --hard HEAD~2       \HEAD~2=HEAD^^
    HEAD is now at c30efe0 add passwd

如果发现回退错了:

# git reflog                               
    c30efe0 HEAD@{0}: HEAD~2: updating HEAD
    c5076eb HEAD@{1}: commit: change bin upper
    6fffe8a HEAD@{2}: commit: delete root
    c30efe0 HEAD@{3}: HEAD^: updating HEAD
    5630321 HEAD@{4}: commit: append bbb...                   
# git reset --hard 6fffe8a          \用版本号回退
    HEAD is now at 6fffe8a delete root

(8)删除文件

# rm -rf  passwd
# git status
    # On branch master
    # Changed but not updated:
    #   (use "git add/rm <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #   
    #   deleted:    passwd
    #   
    no changes added to commit (use "git add" and/or "git commit -a")
# git commit -m "delete passwd"

(9)给版本打标

git tag -a v1.0 -m "release 1"     \给最近的版本打标
git tag -a v0.9 -m "change bin upper"  c5076eb  \给之前的某个版本打标
git  checkout  v0.9          \回退到打标的某个版本 

三、分支管理

什么时候需要建分支?
  a、为自己创建私人分支,避免和其他人产生干扰
  b、做一个具有风险性的试验性更新
  c、整合与其他人的工作,创建一个临时分支
  d、将一部分代码分离出来,使其独立工作

(1)创建分支

# git branch test       \test为分支的名字,自定义
# tree .git/refs/
    .git/refs/
    ├── heads
    │   ├── master
    │   └── test    └── tags

(2)切换分支 checkout

# git checkout test
    Switched to branch 'test'

(3)查看当前操作的分支

# git branch    
      master
    * test

(4)删除分支

# git checkout master  \先切回其他分支
    Switched to branch 'master'
# git branch -d test    \再删除想要删除的分支
    Deleted branch test (was aef4cba).

(5)查看和比较分支

# git branch develop
# echo hi >> file2
# git checkout develop
    Switched to branch 'develop'
# git add file2
# git commit -m "add file2" 

分支的比较
# git diff master ^develop

(6)合并分支

# git checkout master
    Switched to branch 'master'
# git merge develop         \合并develop分支到当前分支

解决冲突:
如果两个分支对相同文件的相同内容做了不同的修改,那么在合并时冲突在所难免,出现冲突后就需要手工解决冲突再提交

合并管理:
通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息。我们在进行分支合并时,可加上--no-ff选项强制禁用Fast forward模式.

git log --graph --pretty=oneline

四、四人协作

三台机器:

public:10.10.10.11
clone2:  10.10.10.12
clone3:  10.10.10.13

三台机器安装git

(1)public配置

# mkdir /project
# cd /project
# git init
    Initialized empty Git repository in /project/.git/
# echo a > a
# echo b > b
# git config --global user.name "public"
# git config --global user.email public@up.com
# git add a b
# git commit -m "public add a b"
    [master (root-commit) 0fd0dd3] public add a b
     1 files changed, 3 insertions(+), 0 deletions(-)
     create mode 100644 a b

(2)clone2操作

# mkdir /git-clone2
# git config --global user.name "clone2"
# git config --global user.email clone2@up.com
# git clone  10.10.10.11:/project  /git-clone2   \将公共服务器上的版本库>克隆到本地
# cd /git-clone2/
# ls
    a b
# cat a
    a
# echo clone2 >> a
# git add a
# git commit -m "clone2 modify a add clone2"

(3)clone3操作

# mkdir /git-clone3
# git config --global user.name "clone3"
# git config --global user.email clone3@up.com
# git clone 10.10.10.11:/project /git-clone3
# echo clone3 >> b
# git add b
# git commit -m "clone3 modify a add clone3"

(4)public从clone2抓取

# git pull 10.10.10.12:/git-clone2
# cat a
    a
    clone2

(5)public从clone3抓取

# git pull root@10.10.10.13:/git-clone3
# cat b
   b
   clone3

如果两个人修改未发生冲突的情况下,都是可以正常提交的
如果两个人修改发生冲突,那么需要手动解决冲突之后再提交或者合并。

原文地址:https://www.cnblogs.com/yangmingxianshen/p/8365712.html