看了廖雪峰老师的git教程,自己总结一下实际会用到的问题。

基础环境:

        server:centos 6.8

        client :A:mac  B:linux

一、安装git,搭建git服务器

    1、yum install  git 此步骤省略。

    2、服务器ip地址192.168.100.136

二、添加git账户,用来启动git服务器。

    1、useradd -d /home/git -m git    添加git用户及家目录

    2、设置git账户密码  passwd git 

    2、初始化git仓库。这里选择/home/git/repository.git

    我心中的git开发实战_git

三、禁用git用户shell登陆(可选)

    1、修改/etc/passwd中的git账户登录信息

    我心中的git开发实战_git_02

    2、添加客户端的ssh公钥到服务器的authorized_keys中。

         客户机和服务器都执行ssh-keygen,默认一直回车,公钥就是.ssh/目录下的id_rsa.pub,将客户机id_rsa.pub里的内容复制到服务器的authorized_keys文件中,如下图。

         服务器在/root/.ssh/目录下新建authorized_keys,用于存储需要认证的客户端公钥。

    我心中的git开发实战_git_03

四、客户端克隆git仓库。

    1、登录客户机(A),默认在/document/目录下克隆仓库。

        git clone git@192.168.100.136:/home/git/repository.git/

     我心中的git开发实战_git_04

        

五、git命令操作

    首先进入克隆后到仓库目录,配置需要提交到git仓库的用户和邮箱信息,用于日后的开发问题排查。

chenhuachaodeMacBook-Pro:repository chenhuachao$ git config --global user.name chenhuachao
chenhuachaodeMacBook-Pro:repository chenhuachao$ git config --global user.email chenhuachao@163.com

新建一个文件,并且加入内容,提交仓库,并查看状态,日志信息

chenhuachaodeMacBook-Pro:repository chenhuachao$ echo "add test file " >test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git add test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git commit -m "add test file"
[master (root-commit) 6ff7cc8] add test file
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
nothing to commit, working tree clean
chenhuachaodeMacBook-Pro:repository chenhuachao$ git log
commit 6ff7cc88f64e3b4e80543354f5b611d4b5782014
Author: chenhuachao <chenhuachao@163.com>
Date:   Tue Feb 14 13:58:10 2017 +0800

    add test file
chenhuachaodeMacBook-Pro:repository chenhuachao$

git add命令只是把文件提交到暂存区,git commit才会把暂存区的内容推送到git本地仓库,如果想要提交到远程仓库,那么就要使用git push origin命令

chenhuachaodeMacBook-Pro:repository chenhuachao$ git push origin
git@192.168.100.136's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To 192.168.100.136:/home/git/repository.git
 * [new branch]      master -> master

删除仓库文件:

[root@chenhuachao repository]# git rm dev.txt
rm 'dev.txt'
[root@chenhuachao repository]# git commit -m "del dev"
[dev 772c462] del dev
 1 files changed, 0 insertions(+), 3 deletions(-)
 delete mode 100644 dev.txt
[root@chenhuachao repository]# git push origin dev
git@192.168.100.136's password:
Counting objects: 12, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 769 bytes, done.
Total 8 (delta 0), reused 0 (delta 0)
To git@192.168.100.136:/home/git/repository.git/
   9ebf491..772c462  dev -> dev
[root@chenhuachao repository]# ls
test.txt


常见冲突问题::

重新登录一台客户机(B),重新克隆git仓库,可以看到推送到内容已经出现

[root@chenhuachao home]# git clone git@192.168.100.136:/home/git/repository.git/
Initialized empty Git repository in /home/repository/.git/
git@192.168.100.136's password:
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), 220 bytes, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
[root@chenhuachao home]# ls
chenhuachao  git  python_script  repository
[root@chenhuachao home]# cd repository/
[root@chenhuachao repository]# ls
test.txt
[root@chenhuachao repository]# cat test.txt
add test file
[root@chenhuachao repository]#

如果这个时候,在B机器上修改test.txt文件并且提交,推送到git远程仓库。

[root@chenhuachao repository]# git config --global user.name zhangsan
[root@chenhuachao repository]# git config --global user.email zhangsan@163.com
[root@chenhuachao repository]# cat test.txt
add test file
modify test file B
[root@chenhuachao repository]# git add test.txt
[root@chenhuachao repository]# git commit -m "modify test wiht B"
[master bcd8cc4] modify test wiht B
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@chenhuachao repository]# git log
commit bcd8cc4245748a93f6636780f4ea46dde2193497
Author: zhangsan <zhangsan@163.com>
Date:   Tue Feb 14 14:12:50 2017 +0800

    modify test wiht B

commit 6ff7cc88f64e3b4e80543354f5b611d4b5782014
Author: chenhuachao <chenhuachao@163.com>
Date:   Tue Feb 14 13:58:10 2017 +0800

    add test file
[root@chenhuachao repository]# git push origin

然后在A机器上同时也修改test.txt文件,提交并推送到git远程仓库。

chenhuachaodeMacBook-Pro:repository chenhuachao$ vim test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ cat test.txt
add test file
modiry test wiht A
chenhuachaodeMacBook-Pro:repository chenhuachao$ git add test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git commit -m "modify test with A"
[master aa68ea5] modify test with A
 1 file changed, 1 insertion(+)
chenhuachaodeMacBook-Pro:repository chenhuachao$ git push origin
推送时提示需要更新仓库使用命令git pull,才能推送。
git@192.168.100.136's password:
To 192.168.100.136:/home/git/repository.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@192.168.100.136:/home/git/repository.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

现在在A上用git pull更新仓库

chenhuachaodeMacBook-Pro:repository chenhuachao$ git pull
git@192.168.100.136's password:
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.100.136:/home/git/repository
   6ff7cc8..bcd8cc4  master     -> origin/master
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
提示有冲突,因为在A上提交的和在B上提交的是同一个文件,需要手工解决冲突
Automatic merge failed; fix conflicts and then commit the result

解决冲突

chenhuachaodeMacBook-Pro:repository chenhuachao$ cat test.txt
add test file
<<<<<<< HEAD   #这是最后一次提交的
modiry test wiht A
=======        #分隔符
modify test file B  #这是上一次提交的
>>>>>>> bcd8cc4245748a93f6636780f4ea46dde2193497

更改文件内容为下面,然后重新提交,推送。就能解决冲突:

chenhuachaodeMacBook-Pro:repository chenhuachao$ cat test.txt
add test file
modiry test wiht A
modify test file B
chenhuachaodeMacBook-Pro:repository chenhuachao$ git add test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git commit -m "modify test with A"
[master 697f752] modify test with A
chenhuachaodeMacBook-Pro:repository chenhuachao$ git push origin
git@192.168.100.136's password:
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 545 bytes | 0 bytes/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To 192.168.100.136:/home/git/repository.git
   bcd8cc4..697f752  master -> master

同时在B机器上执行git pull就能更新到最新文件

[root@chenhuachao repository]# git pull
git@192.168.100.136's password:
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From 192.168.100.136:/home/git/repository
   bcd8cc4..697f752  master     -> origin/master
Updating bcd8cc4..697f752
Fast-forward
 test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@chenhuachao repository]# cat test.txt
add test file
modiry test wiht A
modify test file B

版本回退问题

如果想要回退到下一个版本

[root@chenhuachao repository]# git log --pretty=oneline
697f7523546f62a07506cedc91d9bc69a78ae999 modify test with A
aa68ea5d3c37acc891dcf55e378896ddc1ce21c3 modify test with A
bcd8cc4245748a93f6636780f4ea46dde2193497 modify test wiht B
6ff7cc88f64e3b4e80543354f5b611d4b5782014 add test file
回退到上一个版本
[root@chenhuachao repository]# git reset --hard HEAD^
HEAD is now at aa68ea5 modify test with A
[root@chenhuachao repository]# cat test.txt
add test file
modiry test wiht A

这里我们多提交几个版本,然后重新指定回退到某个版本

[root@chenhuachao repository]# git log --pretty=oneline
38be51f65e853a45109b05724e3b8cc46bc20a19 add 3
325d62bf97e4ef6d5bac2a4631b1587c25bc5c30 add 2
2db34db57dfa5aff79884034929963d3c9302f39 add 1
aa68ea5d3c37acc891dcf55e378896ddc1ce21c3 modify test with A
6ff7cc88f64e3b4e80543354f5b611d4b5782014 add test file
[root@chenhuachao repository]# cat test.txt
add test file
modiry test wiht A
add 1
add 2
add 3
这里我们回退到add 1的那个版本,只需要指定版本号的前7位即可
[root@chenhuachao repository]# git reset --hard 2db34db
HEAD is now at 2db34db add 1
[root@chenhuachao repository]# cat test.txt
add test file
modiry test wiht A
add 1

如果忘记历史提交的命令,可以通过git reflog命令查看

[root@chenhuachao repository]# git reflog
2db34db HEAD@{0}: 2db34db: updating HEAD
38be51f HEAD@{1}: commit: add 3
325d62b HEAD@{2}: commit: add 2
2db34db HEAD@{3}: commit: add 1
aa68ea5 HEAD@{4}: HEAD^: updating HEAD
697f752 HEAD@{5}: pull : Fast-forward
bcd8cc4 HEAD@{6}: commit: modify test wiht B
6ff7cc8 HEAD@{7}: clone: from git@192.168.100.136:/home/git/repository.git/

分支问题:

利用命令git branch查看仓库分支

[root@chenhuachao repository]# git branch
* master

实际开发当中,不会直接在git的master分支上直接操作

实际开发过程中:应有dev测试分支,平时只允许在dev分支进行开发,完全测试通过之后,才能合并到master分支。

master 分支用于发布正式版本

dev 分支用于开发环境和发布测试环境

在B机器上新建分支dev,添加新文件,并提交dev分支到远程git仓库

[root@chenhuachao repository]# git checkout -b dev
Switched to a new branch 'dev'
[root@chenhuachao repository]# git branch
* dev
  master
[root@chenhuachao repository]# ls
test.txt
[root@chenhuachao repository]# vim dev.txt
[root@chenhuachao repository]# git add dev.txt
[root@chenhuachao repository]# git commit -m "add branch dev"
[dev 820e19f] add branch dev
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 dev.txt
[root@chenhuachao repository]# git push origin dev
git@192.168.100.136's password:
Counting objects: 8, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (6/6), 511 bytes, done.
Total 6 (delta 0), reused 0 (delta 0)
To git@192.168.100.136:/home/git/repository.git/
 * [new branch]      dev -> dev

现在登录A机器,重新更新仓库git pull

chenhuachaodeMacBook-Pro:repository chenhuachao$ git pull
git@192.168.100.136's password:
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From 192.168.100.136:/home/git/repository
 * [new branch]      dev        -> origin/dev    #能看到dev分支,但是默认更新上不会出现初master分支以外的其它分支
Already up-to-date.
chenhuachaodeMacBook-Pro:repository chenhuachao$ git branch
* master
chenhuachaodeMacBook-Pro:repository chenhuachao$ ls
test.txt
此时,如果想在A机器也有B机器提交的dev分支,就需要先在A机器创建dev分支,然后指定本地dev分支和远程仓库的dev分支地址,才能够更新远程dev分支。
chenhuachaodeMacBook-Pro:repository chenhuachao$ git branch dev
chenhuachaodeMacBook-Pro:repository chenhuachao$ git branch
  dev
* master
切换到dev分支
chenhuachaodeMacBook-Pro:repository chenhuachao$ git checkout dev
Switched to branch 'dev'
chenhuachaodeMacBook-Pro:repository chenhuachao$ ls
test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git pull
git@192.168.100.136's password:
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
    git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
    git branch --set-upstream-to=origin/<branch> dev
指定本地dev分支和远程dev分支地址链接。用于push推送
chenhuachaodeMacBook-Pro:repository chenhuachao$ git branch --set-upstream-to=origin/dev dev
Branch dev set up to track remote branch dev from origin.
chenhuachaodeMacBook-Pro:repository chenhuachao$ ls
test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ git pull #成功拉取
git@192.168.100.136's password:
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.
chenhuachaodeMacBook-Pro:repository chenhuachao$ ls
dev.txt  test.txt
chenhuachaodeMacBook-Pro:repository chenhuachao$ cat dev.txt
add branch dev

这样就可以在A机器上的dev分支进行修改提交推送了。同样,也可以在dev分支下,继续创建分支,比如修复某个bug,不再详解。

分支合并

比如说,现在有一个bug1需要解决。在dev分支下新建bug1分支,并切换到bug1分支进行修改,提交,合并

[root@chenhuachao repository]# git branch bug1

[root@chenhuachao repository]# git checkout bug1
Switched to branch 'bug1'
[root@chenhuachao repository]# git branch
* bug1
  dev
  master
[root@chenhuachao repository]# ls
dev.txt  test.txt
[root@chenhuachao repository]# vim dev.txt
[root@chenhuachao repository]# cat dev.txt
add branch dev
repair bug1
[root@chenhuachao repository]# git add dev.txt
[root@chenhuachao repository]# git commit -m "repair bug1"
[bug1 9ebf491] repair bug1
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@chenhuachao repository]# git checkout dev
Switched to branch 'dev'
合并bug1分支到dev
[root@chenhuachao repository]# git merge bug1
Updating 820e19f..9ebf491
Fast-forward   #这里合并说使用的fast-forward方法。建议使用git merge --no-ff -m "repair bug1" bug1命令进行合并,可以记录日志信息
 dev.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
[root@chenhuachao repository]# git branch -d bug1
Deleted branch bug1 (was 9ebf491).
[root@chenhuachao repository]# cat dev.txt
add branch dev
repair bug1

这里就可以推送分支dev到远程git仓库。

日志信息,图形化现实缩略信息命令

[root@chenhuachao repository]# git log --graph --pretty=oneline --abbrev-commit
* 9ebf491 repair bug1
* 820e19f add branch dev
* 2db34db add 1
* aa68ea5 modify test with A
* 6ff7cc8 add test file

隐藏分支

[root@chenhuachao repository]# git checkout -b bug2
Switched to branch 'bug2'
[root@chenhuachao repository]# ls
dev.txt  test.txt
[root@chenhuachao repository]# cat dev.txt
add branch dev
[root@chenhuachao repository]# git branch
* bug2
  dev
  master
[root@chenhuachao repository]# vim dev.txt
[root@chenhuachao repository]# git stash
Saved working directory and index state WIP on bug2: 820e19f add branch dev
HEAD is now at 820e19f add branch dev
[root@chenhuachao repository]# git branch
* bug2
  dev
  master
[root@chenhuachao repository]# git stash list
stash@{0}: WIP on bug2: 820e19f add branch dev
[root@chenhuachao repository]# git stash pop
# On branch bug2
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   dev.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (3ac6925cb085c50293b7adbb1c17a450fdaadb50)

更多git命令,需要不断摸索。

##仓库迁移

1、 从原地址克隆一份裸版本库,比如原本托管于 GitHub。

git clone --bare git://github.com/username/project.git

2、从新的git服务器新建一个git仓库

git init -bare newproject.git

chown -R git:git newproject.git

3、进入第一步刚克隆的裸版本库中。

cd project.git
git push --mirror git@gitcafe.com/username/newproject.git

4、删除旧版本库即可