搭建git开发环境


百度百科的定义:

GIT(分布式版本控制系统)

Git是一款免费、开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。
Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。
官网地址:http://git-scm.com/

最大特点:分布式

分布式相比于集中式的最大区别在于开发者可以提交到本地,每个开发者通过克隆(git clone),在本地机器上拷贝一个完整的Git仓库。它的好处就是你可以离线提交代码,你可以在没有网络的地方进行代码的提交和更新。
 

搭建GIT开发环境

虽然说是分布式代码管理系统,但你仍然要搭建一套GIT服务端,这是因为需要一个统一的地方用于保存不同人提交的代码,而用户本地一般是自己开发代码的维护。

1. 服务器端环境搭建

以CentOS或Redhat为例,搭建非常简单,通过yum安装,创建git用户

初始化Git仓库

先选定一个目录作为Git仓库,这里是/data/git/project.git。

[root@git ~]# cd /data/git/

[root@git git]# git init --bare project.git



2. windows客户端环境搭建

由于工作机器一般安装windows系统,所以这里以windows系统为例,linux系统非常简单,使用方法参照windows,这里不特别说明。

首先安装git client,这里推荐安装msysGit,地址 http://msysgit.github.io/, 安装过程没有什么特别,按提示一步一步安装即可,安装完成后,菜单中会有Git Bash和Git GUI两项,

Git Bash为命令行窗口,这里可以进行git的命令行操作,相信习惯使用linux的人比较喜欢这个界面,


另外client安装完成后,进入任何一个windows的文件夹,鼠标右键菜单会显示相关的git操作菜单,使用git会非常直观

 

客户端需要将本地的key发送给git管理员,生成的方法就是通过前面的GIT GUI菜单来生成,生成时候,不要填任何数据



3. 代码管理

这里通过一个场景来描述代码管理,让初次使用GIT的人能够快速上手,Git的各种命令和用法可以参考Git的帮助

场景:
用户A和用户B都在使用同一个Git库,远程Branch为origin。
1. A用户添加一个文件linux.txt到远程库中;
[root@centos65 project]# cat linux.txt 
linux

2. B用户从远程库中check out最新的代码到本地;
3. A用户修改Linux.txt后上传到远程库中;
4. B用户修改linux.txt后上传到远程库中,这里涉及到代码的冲突以及解决;

  • 获取服务端项目

windows中的换行符为 CRLF, 而在linux下的换行符为LF,所以先执行下面命令

$ git config --global core.autocrlf false  //禁用自动转换  

然后在本地新建目录,用于保存项目代码,执行git init,在本地建立.git目录,由于是隐藏目录,可以通过ls -la查看

$ git init 

执行git clone命令,获取服务端最新项目

$ git clone git@git-server-ip:/export/secondary/workspace/git/project.git
 
 
  • 修改文件,并将文件同步到服务端项目
1)A用户将本地linux.txt文件放入远程Git库
[root@centos65 project]# cat linux.txt 
linux
[root@centos65 project]# git add linux.txt
[root@centos65 project]# git commit -m "linux"
[root@centos65 project]# git push origin master
  
  
2)B用户从远程库中check out最新的代码到本地;
$ git clone git@git-server-ip:/export/secondary/workspace/git/project.git

   
   
如果已经执行过clone命令,可以通过下面命令从服务端获取最新的代码
$ git pull


3)A用户对该文件进行修改,然后将修改提交到远程库
[root@centos65 project]# cat linux.txt 
linux
linux A
[root@centos65 project]# git status
[root@centos65 project]# git log --pretty=oneline  readme.txt
1760fc0cfa585e3955065099f5f011f607b64e23 add 2,3
cb52a98c4ff26d88d4829306b5bf12629ac24927 add 4/23
64095cc4f742e194915bbb0fa9c758154c72f0a3 add file
注:Git status是非常有用的命令,会提示当前操作的状态,并会提示进一步操作
如果 git status 告诉你有文件被修改过,用 git diff 可以查看修改内容。
git log命令显示从最近到最远的提交日志,你看到的一大串类似 1760fc0cfa585e3955065099f5f011f607b64e23 的是commit id(版本号)


 
修改后提交本地库
[root@centos65 project]# git add linux.txt
[root@centos65 project]# git commit -m "linux A"
 
将更新提交到远程库
[root@centos65 project]# git push origin master
Total 3 (delta 0), reused 0 (delta 0)
To git@git-server-ip:/export/secondary/workspace/git/project.git
   64095cc..de00f23  master -> master
 
4)B用户对该文件进行修改并提交,这里涉及到代码的冲突以及解决;
[root@centos65 project]# cat linux.txt 
linux
linux 1
linux B
[root@centos65 project]# git add linux.txt 
[root@centos65 project]# git commit -m "linux B"
 
由于A用户已经修改了代码,B用户push的时候显示冲突,无法更新,需要进行冲突解决
[root@centos65 project]# git push origin master
To git@135.251.218.101:/export/secondary/workspace/git/project.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@135.251.218.101:/export/secondary/workspace/git/project.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.
 

如果推送失败,因为你的小伙伴的最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin抓下来,然后,在本地合并,解决冲突,再推送:
pull会显示下面两种情况,一种是能够自动auto-merge(当修改的不是同一行,比如新增加行),另一种auto-merge失败(当修改的为同一行代码),文件中会标示冲突的代码,需要按提示手工merge。
情况一:auto-merge
[root@centos65 project]# git pull 
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 135.251.218.101:/export/secondary/workspace/git/project
   a6de76c..04ddbf1  master     -> origin/master
Auto-merging linux.txt
Merge made by recursive.
 linux.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)


情况二:这回git pull成功,但是合并有冲突,需要手动解决。解决后,提交,再push:
[root@centos65 project]#  git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 135.251.218.101:/export/secondary/workspace/git/project
   de00f23..57c358a  master     -> origin/master
Auto-merging linux.txt
CONFLICT (content): Merge conflict in linux.txt
Automatic merge failed; fix conflicts and then commit the result.

[root@centos65 project]# cat linux.txt 
<<<<<<< HEAD
linux
linux 1
linux B
=======
linux raw


linux A
>>>>>>> 57c358ac6cb51b2e7b32addfa0d07707e9ba811b
手工合并后,再次提交
前面两种情况merge完成后,再提交代码
[root@centos65 project]# git add linux.txt

[root@centos65 project]# git commit -m "merge" [root@centos65 project]# git push origin master Counting objects: 13, done. Delta compression using up to 16 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (9/9), 871 bytes, done. Total 9 (delta 0), reused 0 (delta 0) To git@135.251.218.101:/export/secondary/workspace/git/project.git    57c358a..a6de76c  master -> master

 
[root@centos65 project]# git push origin master
Counting objects: 13, done.
Delta compression using up to 16 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (9/9), 871 bytes, done.
Total 9 (delta 0), reused 0 (delta 0)
To git@135.251.218.101:/export/secondary/workspace/git/project.git
   57c358a..a6de76c  master -> master

当然,如果B用户先下载最新版本. 然后修改后再上传,就不会产生冲突,当Git库使用人不多的时候,大家可以口头通知其他人正在修改
可以执行如下操作
git reset --hard
git pull

  • 分支(branch)操作相关命令

查看本地分支:$ git branch 查看远程分支:$ git branch -r 创建本地分支:$ git branch [name] ----注意新分支创建后不会自动切换为当前分支 切换分支:$ git checkout [name]


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenWRT是一个轻量级的嵌入式Linux发行版,适用于诸如路由器、防火墙等设备。LuCI(Lightweight User Interface Configuration)是一个为OpenWRT构建的Web管理界面框架。如果你想在本地开发OpenWRT的LuCI代码,你需要设置一个基本的Git开发环境。以下是搭建步骤: 1. **安装Git**: 确保你的系统已经安装了Git。如果你使用的是Linux或macOS,可以使用包管理器(如apt-get或brew)安装。 ```bash sudo apt-get install git # 对于Debian/Ubuntu brew install git # 对于macOS ``` 2. **克隆OpenWRT仓库**: 在你的开发目录下,使用以下命令克隆OpenWRT的主仓库和LuCI分支: ```bash git clone https://github.com/openwrt/openwrt.git cd openwrt git checkout -b luci-branch origin/luci ``` 3. **配置编译工具链**: 如果你需要自定义编译环境,确保安装交叉编译工具链,这通常根据你的目标硬件平台不同而异。OpenWRT有一个详细的文档指导:https://openwrt.org/docs/en/devel/developers_manual/compiling#cross-compilation 4. **获取LuCI源码**: 如果你想要单独管理LuCI的开发,可以在LuCI的GitHub上克隆: ```bash git clone https://github.com/LuaDist/luci.git luci ``` 5. **设置环境变量**: 配置你的系统路径,使得Git可以找到LuCI的源码: ```bash export PATH=$PATH:/path/to/luci ``` 6. **初始化项目**: 在每个仓库中执行必要的初始化步骤,比如`cd luci && make` 或 `cd openwrt && make package_luci-core` 7. **提交和同步更改**: 完成开发后,记得定期将更改提交到本地仓库,并将更改同步到远程仓库: ```bash git add . git commit -m "Your commit message" git push ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值