dawn-liu

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

git的安装

yum -y install git

进行git的全局配置

git config --global user.name "yourname" #设定自定义的昵称
git config --global user.email "youremail@example.com" #设定自己的邮箱
git config --global color.ui true #设置命令代码多颜色显示
git config --list #列出以上配置的信息

全局配置对应的文件

[liming@centos7 2018-09-21]$ cat ~/.gitconfig
[user]
    name = liming
    email = liming@centos7.com
[color]
    ui = true

 新建项目目录并初始化

mkdir  2018-09-21
cd  2018-09-21

git init  .    在当前目录初始化,之后会出现目录  .git

局部配置用户的相关信息

[liming@centos7 2018-09-21]$ git config user.name 'liming_local'
[liming@centos7 2018-09-21]$ cat .git/config  #其对应的文件
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[user]
    name = liming_local

 git status命令:查看当前用户的提交状态

 git   log 命令:查看用户操作的记录

git add命令,添加到待提交列表里面

首先:我们来新建两个文件  readme  和 log
然后使用 git add提交
[liming@centos7 2018-09-21]$ git add readme
[liming@centos7 2018-09-21]$ git add log
其次:提交改变并查看状态
[liming@centos7 2018-09-21]$ git commit -m "初始化提交"
[master (root-commit) 04018e4] 初始化提交
 2 files changed, 2 insertions(+)
 create mode 100644 log
 create mode 100644 readme
[liming@centos7 2018-09-21]$ git status
# On branch master
nothing to commit, working directory clean
再其次:在文件readme中加入一行,并查看状态
[liming@centos7 2018-09-21]$ vim readme
[liming@centos7 2018-09-21]$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   readme
#
no changes added to commit (use "git add" and/or "git commit -a")
好,我们先将它提交

[liming@centos7 2018-09-21]$ git add readme
[liming@centos7 2018-09-21]$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: readme
#
[liming@centos7 2018-09-21]$ git commit -m "增加一行readme"
[master 5eb920f] 增加一行readme
1 file changed, 1 insertion(+)

查看log,里面没有中文显示

[liming@centos7 2018-09-21]$ git log
commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date: Fri Sep 21 15:13:07 2018 +0800

<E5><A2><9E><E5><8A><A0><E4><B8><80><E8><A1><8C>readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date: Fri Sep 21 15:11:36 2018 +0800

<E5><88><9D><E5><A7><8B><E5><8C><96><E6><8F><90><E4><BA><A4>

设置git的编码格式

 git config --global core.quotepath false   #显示 status 编码

 git config --global gui.encoding utf-8  #图形界面编码

 git config --global i18n.commit.encoding utf-8  #提交信息编码

 git config --global i18n.logoutputencoding utf-8  #输出 log 编码

 export LESSCHARSET=utf-8

 再次查看

[liming@centos7 2018-09-21]$ git log
commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交

 本地删除文件并恢复 git  checkout ,场景:当你修改文件后发现文件修改的有很大问题,可以回到之前提交列表的状态

[liming@centos7 2018-09-21]$ rm -f readme
[liming@centos7 2018-09-21]$ ls
log

[liming@centos7 2018-09-21]$ git checkout readme
[liming@centos7 2018-09-21]$ ls
log  readme
[liming@centos7 2018-09-21]$ more readme

 git show 加 id 查看日志的详细内容

[liming@centos7 2018-09-21]$ git show  04018e41728e15d1a61a59923abfa51f443994ed
commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交

diff --git a/log b/log
new file mode 100644
index 0000000..a1514f8
--- /dev/null
+++ b/log
@@ -0,0 +1 @@
+log log
diff --git a/readme b/readme
new file mode 100644
index 0000000..54e4beb
--- /dev/null
+++ b/readme
@@ -0,0 +1 @@
+this is a first file for git
View Code

git  reset 命令

下面先看一张表

git reset 撤销有三种方式:尽量不采用head的方式

1  --soft  commit-id 表示从本地仓库的位置推到待提交列表的位置,再次使用git commit -m 可以提交到本地仓库位置

[liming@centos7 2018-09-21]$ git log
commit 8aa0bb07b80425baf96300c1439c94e923803c1c
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:31:25 2018 +0800

    增加文件3.txt

commit a25abc1a8dd505825bcc87240c08ea477ba23de5
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:40 2018 +0800

    增加文件2.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
[liming@centos7 2018-09-21]$ git reset --soft a25abc1a8dd505825bcc87240c08ea477ba23de5
[liming@centos7 2018-09-21]$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   3.txt
#
[liming@centos7 2018-09-21]$ git log
commit a25abc1a8dd505825bcc87240c08ea477ba23de5
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:40 2018 +0800

    增加文件2.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
View Code

 2 --mixed commit-id 表示回到本地代码的位置,需要 git add   ,   git   commit  才能恢复到本地仓库位置

[liming@centos7 2018-09-21]$ git log
commit 7668f11905334fda74fc409c8ecbc7cafc3a6352
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:45:54 2018 +0800

    3.txt重新提交

commit a25abc1a8dd505825bcc87240c08ea477ba23de5
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:40 2018 +0800

    增加文件2.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
[liming@centos7 2018-09-21]$ git status
# On branch master
nothing to commit, working directory clean
[liming@centos7 2018-09-21]$ git reset --mixed a25abc1a8dd505825bcc87240c08ea477ba23de5
[liming@centos7 2018-09-21]$ git log
commit a25abc1a8dd505825bcc87240c08ea477ba23de5
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:40 2018 +0800

    增加文件2.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
[liming@centos7 2018-09-21]$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    3.txt
nothing added to commit but untracked files present (use "git add" to track)
View Code

3 --hard commit-id (慎用,生产中用到的次数极少);表示本地仓库和本地代码的状态都回到 提交代码时的状态;请看下面会到  增加文件1.txt  的状态

[liming@centos7 2018-09-21]$ git log
commit 6d5057d6b725a44863d53fdfff8ed7a43f521363
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:57:14 2018 +0800

    第三次提交3.txt

commit a25abc1a8dd505825bcc87240c08ea477ba23de5
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:40 2018 +0800

    增加文件2.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
[liming@centos7 2018-09-21]$ git reset --hard fee63c016ef26ce241c72da2ecab9449cd9508c3
HEAD is now at fee63c0 增加文件1.txt
[liming@centos7 2018-09-21]$ git log
commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
[liming@centos7 2018-09-21]$ ls
1.txt  log  readme
View Code

 git rm 场景:你在本地删除一个文件,要想本地仓库保持一致,就使用这个命令

[liming@centos7 2018-09-21]$ rm 1.txt   #本地删除文件
[liming@centos7 2018-09-21]$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    deleted:    1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[liming@centos7 2018-09-21]$ git log
commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交

[liming@centos7 2018-09-21]$ git rm 1.txt #本地仓库也删除
rm '1.txt'
[liming@centos7 2018-09-21]$ git log
commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
[liming@centos7 2018-09-21]$  git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    deleted:    1.txt
#
[liming@centos7 2018-09-21]$ git commit -m '删除了1.txt'  #提交一次删除操作
[master a90c6c4] 删除了1.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 1.txt
[liming@centos7 2018-09-21]$ git status   #状态就可以了
# On branch master
nothing to commit, working directory clean
[liming@centos7 2018-09-21]$ git log
commit a90c6c43997799e4fd8ec318157c952d07442531
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 17:12:20 2018 +0800

    删除了1.txt

commit fee63c016ef26ce241c72da2ecab9449cd9508c3
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 16:30:10 2018 +0800

    增加文件1.txt

commit 5eb920f9674586be09d5d99033b10a19f59e8957
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:13:07 2018 +0800

    增加一行readme

commit 04018e41728e15d1a61a59923abfa51f443994ed
Author: liming_local <liming@centos7.com>
Date:   Fri Sep 21 15:11:36 2018 +0800

    初始化提交
View Code

 .gitignore文件:用来忽略一些不想提交的文件,这个最好初始化的时候创建

首先建立一个文件 
touch  .gitignore
touch database.yml #忽略这个文件

vim .gitignore
*yml  忽略yml结尾的文件

提交.gitignore
git add .gitignore

git commit -m '提交文件 .gitignore'

 git  checkout   分支名称, 切换到其他分支的命令

[liming@centos7 2018-09-21]$ git branch
* master
[liming@centos7 2018-09-21]$ git branch 20180921-001 #创建分支
[liming@centos7 2018-09-21]$ git branch #当前分支前面有*
  20180921-001
* master
[liming@centos7 2018-09-21]$ git checkout 20180921-001  #切换到分支上面
Switched to branch '20180921-001'
接着我们修改文件readme
再提交
git add readme
git commit -m '001分支修改readme内容'
查看
分别切换到主分支,和001分支,发现001分支多一条log

 删除一个分支:git branch -d   名称

[liming@centos7 2018-09-21]$ git branch 20180921-002
[liming@centos7 2018-09-21]$ git branch -d 20180921-002
Deleted branch 20180921-002 (was a4f1c8b).
[liming@centos7 2018-09-21]$ git branch
* 20180921-001
  master

 git merge  分支名称  将分支的改变同步到主分支,这个比svn和cvs有优势,没有奔溃的危险

先切换到主分支
git checkout master

合并
git merge 20180921-001

git log   #查看 日志多了分支的一条记录,id 也一样

 到这里为止,我们只用到了本地仓库的层次,还有远程的git服务器用来提交代码,然后成员之间相互交流

先说一下现有的git服务器,有githup这个是外国的,要考虑网络问题还有私有是要收费的,codIng.net这个是国内的,私有是免费的

现在我们使用coding.net说明

1 先在coding.net上面注册
2 新建项目
3 回到linux上面推送代码
[liming@centos7 2018-09-21]$ git remote add origin https://git.coding.net/study_more/first_item.git
[liming@centos7 2018-09-21]$ git push -u origin master
Username for 'https://git.coding.net': XXXXX
Password for 'https://study_more@git.coding.net': 
Counting objects: 17, done.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (17/17), 1.51 KiB | 0 bytes/s, done.
Total 17 (delta 2), reused 0 (delta 0)
To https://git.coding.net/study_more/first_item.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

回到coding.net上面查看

 当然也可以多个远程服务器的地址并存:

修改配置

cat .git/config
[remote "origin"]
    url = https://git.coding.net/study_more/first_item.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[remote "backup"]
    url =比如说github的地址
    fetch = +refs/heads/*:refs/remotes/bakcup/*

 说明:

git remote add origin https://git.coding.net/study_more/first_item.git  其中的origin指的就是 远程服务其的名称

git push -u origin master  (master:master)

 git clone  从远程下载git的项目

1 新建一个目录,可以是没有使用git初始化的
[liming@centos7 2018-09-21]$ cd ..
[liming@centos7 workspace]$ ls
2018-09-21
[liming@centos7 workspace]$ mkdir down_load
[liming@centos7 workspace]$ cd down_load/
[liming@centos7 down_load]$ git clone https://git.coding.net/study_more/first_item.git
Cloning into 'first_item'...
Username for 'https://git.coding.net': XXXXXX
Password for 'https://study_more@git.coding.net': 
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 17 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (17/17), done.
[liming@centos7 down_load]$ ls
first_item

 git pull origin master 从远程服务器上面pull项目,必须是使用了git初始化了的情况

这样一来,一个提交,一个pull就可以相互的交流了,工作中要多push

 git冲突的问题:

场景:我这边修改了本地文件没提交,另外一边有另外一个同事也修改了同样的文件并且提交了

我push命令的时候出现了错误:

[liming@centos7 2018-09-21]$ git pull origin master
Username for 'https://git.coding.net': study_more
Password for 'https://study_more@git.coding.net': 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://git.coding.net/study_more/first_item
 * branch            master     -> FETCH_HEAD
Auto-merging readme
CONFLICT (content): Merge conflict in readme
Automatic merge failed; fix conflicts and then commit the result.
[liming@centos7 2018-09-21]$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#   (use "git push" to publish your local commits)
#
# You have unmerged paths.
#   (fix conflicts and run "git commit")
#
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#
#    both modified:      readme
#
no changes added to commit (use "git add" and/or "git commit -a")

解决:注意上面提到了readme文件

首先 人工去协商;确认需要保留的

this is a first file for git
add  a line
在001分支上面加的内容
<<<<<<< HEAD
在目录2018-09-21中的修改
=======
在目录hhh中的修改
>>>>>>> 83f9fe1b6ee0bf33b70deda90d40043fba2084b8

我将上面的内容改为

this is a first file for git
add a line
在001分支上面加的内容
在目录2018-09-21中的修改

 

我再操作一次

git add readme

git commit -m "合并冲突"

git push origin master

在同事的另外一般就可以pull了看到的就是合并后的内容

注意事项:

首先想到的是要pull代码,修改代码要多提交,一旦出现冲突就可以短时间内解决,否则一旦代码多了,你哭去吧

 最后git使用注意的事项

1 不要改变时间线  rebase不要用,cherry-pick没必要用

2 代码存放选择:开源放 github ,私有放coding.net ,尽量不要自己搭建gitlab,出了问题不好解决

3 git log写具体些,宁多勿少

posted on 2018-09-21 10:56  dawn-liu  阅读(156)  评论(0编辑  收藏  举报