我心中的git开发实战

看了廖雪峰老师的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用户shell登陆(可选)

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

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

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

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

四、客户端克隆git仓库。

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

git clone [email protected]:/home/git/repository.git/

五、git命令操作

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

chenhuachaodeMacBook-Pro:repository chenhuachao$ git config --global user.name chenhuachao
chenhuachaodeMacBook-Pro:repository chenhuachao$ git config --global user.email [email protected]

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

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 <[email protected]>
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
[email protected]‘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

删除仓库文件:

[[email protected] repository]# git rm dev.txt
rm ‘dev.txt‘
[[email protected] repository]# git commit -m "del dev"
[dev 772c462] del dev
 1 files changed, 0 insertions(+), 3 deletions(-)
 delete mode 100644 dev.txt
[[email protected] repository]# git push origin dev
[email protected]‘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 [email protected]:/home/git/repository.git/
   9ebf491..772c462  dev -> dev
[[email protected] repository]# ls
test.txt

常见冲突问题::

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

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

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

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

    modify test wiht B

commit 6ff7cc88f64e3b4e80543354f5b611d4b5782014
Author: chenhuachao <[email protected]>
Date:   Tue Feb 14 13:58:10 2017 +0800

    add test file
[[email protected] 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,才能推送。
[email protected]‘s password:
To 192.168.100.136:/home/git/repository.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to ‘[email protected]:/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
[email protected]‘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
[email protected]‘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就能更新到最新文件

[[email protected] repository]# git pull
[email protected]‘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(-)
[[email protected] repository]# cat test.txt
add test file
modiry test wiht A
modify test file B

版本回退问题:

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

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

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

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

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

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

分支问题:

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

[[email protected] repository]# git branch
* master

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

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

master 分支用于发布正式版本

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

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

[[email protected] repository]# git checkout -b dev
Switched to a new branch ‘dev‘
[[email protected] repository]# git branch
* dev
  master
[[email protected] repository]# ls
test.txt
[[email protected] repository]# vim dev.txt
[[email protected] repository]# git add dev.txt
[[email protected] repository]# git commit -m "add branch dev"
[dev 820e19f] add branch dev
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 dev.txt
[[email protected] repository]# git push origin dev
[email protected]‘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 [email protected]:/home/git/repository.git/
 * [new branch]      dev -> dev

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

chenhuachaodeMacBook-Pro:repository chenhuachao$ git pull
[email protected]‘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
[email protected]‘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 #成功拉取
[email protected]‘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分支进行修改,提交,合并

[[email protected] repository]# git branch bug1

[[email protected] repository]# git checkout bug1
Switched to branch ‘bug1‘
[[email protected] repository]# git branch
* bug1
  dev
  master
[[email protected] repository]# ls
dev.txt  test.txt
[[email protected] repository]# vim dev.txt
[[email protected] repository]# cat dev.txt
add branch dev
repair bug1
[[email protected] repository]# git add dev.txt
[[email protected] repository]# git commit -m "repair bug1"
[bug1 9ebf491] repair bug1
 1 files changed, 1 insertions(+), 0 deletions(-)
[[email protected] repository]# git checkout dev
Switched to branch ‘dev‘
合并bug1分支到dev
[[email protected] 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(-)
[[email protected] repository]# git branch -d bug1
Deleted branch bug1 (was 9ebf491).
[[email protected] repository]# cat dev.txt
add branch dev
repair bug1

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

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

[[email protected] 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

隐藏分支:

[[email protected] repository]# git checkout -b bug2
Switched to branch ‘bug2‘
[[email protected] repository]# ls
dev.txt  test.txt
[[email protected] repository]# cat dev.txt
add branch dev
[[email protected] repository]# git branch
* bug2
  dev
  master
[[email protected] repository]# vim dev.txt
[[email protected] repository]# git stash
Saved working directory and index state WIP on bug2: 820e19f add branch dev
HEAD is now at 820e19f add branch dev
[[email protected] repository]# git branch
* bug2
  dev
  master
[[email protected] repository]# git stash list
[email protected]{0}: WIP on bug2: 820e19f add branch dev
[[email protected] 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/[email protected]{0} (3ac6925cb085c50293b7adbb1c17a450fdaadb50)

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

时间: 2024-08-04 09:54:05

我心中的git开发实战的相关文章

git开发实战:问题解决

git作为一个高效的开发协作工具,其版本管理和分支管理是其高效管理代码的体现.但是我们在平时开发中,往往要一边修着bug一边做着新功能,这样有可能就会遇到以下几种场景 1.改完bug忘记切换分支了,代码改了很多怎么办.莫慌,git如果这种问题都解决不了何谈高效.使用以下命令即可解决.    1.1 git add .      (把所有改动暂存)    1.2 git stash     (把暂存的文件提交到git的暂存栈)    1.3 git checkout 本该提交代码的分支      

iOS开发——开发实战篇&amp;版本控制SVN和Git简单实战总结

版本控制SVN和Git简单实战总结 如果你对iOS开发中的版本控制还不了解那么你可以先看看这篇(大致看一遍就ok) 关于版本控制使用起来并不难,但是可能你会遇到这样问题! 学了这么多命令,感觉自己都知道,而且基本上都能敲出一二,但是就是不轻松公司实际开发中到底要怎么用,或者我该怎么下手,下面我们就来看看我们到了公司之后首先要做的,和之后经常要做的一些事情(命令太多没必要去记,常用的也就那么几个). 首先,你必须先知道,在天朝,SVN目前任是主流,但是又不的不会(这里具体原因我就不多说了)! 好了

J2EE开发实战基础系列之开卷有益

时隔七年再次接触培训有关的事情,是兴奋,更多的是恐惧,不知该如何下手. 本系列针对有Java语法基础的开发者或者爱好者,从工作开发角度出发讲解,不同于其他视频,一切皆以实用为主,过程中如有疑问,请提问于我,回答将发布在教程中添加提问部分,提问者越多,教程覆盖越全面,以实际问题为主. ----------------------------------------------------------------------------------------- 首先介绍下目前J2EE方面培训的入门

新书《Jfinal极速开发实战》正式发布

JfinalUIB学习交流QQ群 :309647612 书籍整个创作周期从2015年8月1日开始到2015年9月10日,时间仓促,难免有瑕疵,希望大家能够指出存在的问题,我会不断的更新纠正,谢谢大家! 前四章免费阅读,内容会持续定期更新,紧随Jfinal的发展,希望大家看看做出评价,谢谢!   百度阅读地址 :http://yuedu.baidu.com/ebook/3fc54b55d5bbfd0a7956739f 目录 Jfinal极速开发实战... 为什么要写这本书... 3 读者对象...

Flask之旅《Flask Web开发:基于Python的Web应用开发实战》学习笔记

<Flask Web开发:基于Python的Web应用开发实战> 点击上方的"目录"快速到达哦! 虽然简单的网站(Flask+Python+SAE)已经上线,但只是入门.开发大型网站,系统地学习一遍还是有必要的. 1 虚拟环境 2016-6-8 书上介绍了 virtualenv,每个venv都会拷贝一份packages到项目 /venv目录. virtualenv venv venv\Scripts\activate.bat (venv) $ pip freeze >

《Jfinal极速开发实战》正式发布

----------------------------------------------------------------------- 百度阅读地址 :http://yuedu.baidu.com/ebook/3fc54b55d5bbfd0a7956739f 定价55.55元,百度阅读抽取三成,剩下为作者所得,谢谢大家支持! 暂时没有出纸质版本是因为费用实在太高(3-5W),个人无法承受,望大家体谅. -------------------------------------------

《Flask Web开发:基于Python的Web应用开发实战》pdf 免费下载

<Flask Web开发:基于Python的Web应用开发实战>pdf 免费下载链接: https://u253469.ctfile.com/fs/253469-292665036 第一部分 Flask 简介第1 章 安装 .........................................................................................................................................

Flask开发实战:前言

Flask 好不好自己去网络找,本系列博文参考李辉的<Flask Web开发实战> 一本书核心内容 本书由三部分组成,分别为基础篇.实战篇.进阶篇,共16章.本书章节经过精心设计,力求让读者可以循序渐进地掌握Flask开发的基础知识和技巧. 第一部分:基础篇.介绍Flask开发相关的基础知识. ·第1章:搭建开发环境,编写一个最小的Flask程序并运行它,了解Flask基本知识. ·第2章:介绍Flask与HTTP的交互方式以及相关的Flask功能. ·第3章:介绍Jinja2模板的使用. ·

第四篇 :微信公众平台开发实战Java版之完成消息接受与相应以及消息的处理

温馨提示: 这篇文章是依赖前几篇的文章的. 第一篇:微信公众平台开发实战之了解微信公众平台基础知识以及资料准备 第二篇 :微信公众平台开发实战之开启开发者模式,接入微信公众平台开发 第三篇 :微信公众平台开发实战之请求消息,响应消息以及事件消息类的封装 首先,我们看看原来写的dopost方法: /** * 处理微信服务器发来的消息 */ public void doPost(HttpServletRequest request, HttpServletResponse response) thr