git - 管理项目(SourceTree的使用)
Git 相关命令操作全
1.SourceTree 是什么?
SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作。同时它也是Mercurial和Subversion版本控制系统工具。支持创建、提交、clone、push、pull 和merge等操作。
特点:分布式
![](http://upload-images.jianshu.io/upload_images/1389082-a1ca66e8c5a60175.png?imageMogr2/auto-orient/strip%7CimageView2/2)
2.Sourcetree && Git 部分名词解释
1.克隆(clone):从远程仓库URL加载创建一个与远程仓库一样的本地仓库
2.提交(commit):将暂存文件上传到本地仓库(我们在Finder中对本地仓库做修改后一般都得先提交一次,再推送)
3.检出(checkout):切换不同分支
4.添加(add):添加文件到缓存区
5.移除(remove):移除文件至缓存区
6.暂存(git stash):保存工作现场
7.重置(reset):回到最近添加(add)/提交(commit)状态
8.合并(merge):将多个同名文件合并为一个文件,该文件包含多个同名文件的所有内容,相同内容抵消
9.抓取(fetch):从远程仓库获取信息并同步至本地仓库
10.拉取(pull):从远程仓库获取信息并同步至本地仓库,并且自动执行合并(merge)操作,即 pull=fetch+merge
11.推送(push):将本地仓库同步至远程仓库,一般推送(push)前先拉取(pull)一次,确保一致
12.分支(branch):创建/修改/删除分枝
13.标签(tag):给项目增添标签
14.工作流(Git Flow):团队工作时,每个人创建属于自己的分枝(branch),确定无误后提交到master分枝
15.终端(terminal):可以输入git命令行用git命令的话 开发中按照以下步骤进行代码提交
0.cd命令进入项目目录然后如下
1. 修改完代码后,git status查看自己的文件修改列表;
2. git diff <文件名> 查看自己的文件修改记录;
3. git add <文件名> 添加需要提交的文件列表;
4. git commit -m "提交说明"
5. git pull,更新一下当前最新的代码;
6. git push 提交代码
3.使用Sourcetree
1.第一步:安装
(1).图标:
![](http://upload-images.jianshu.io/upload_images/1389082-d07ff70a047ecdfc.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(2).不建议在appstore直接搜索下载SourceTree,因为appstore里的版本已经十分老旧(好像是2012年更新的)。建议前往官网下载安装最新版,这里提供一个SourceTree for Mac https://www.sourcetreeapp.com
(3).如果看到下面这个界面就说明安装成功
![](http://upload-images.jianshu.io/upload_images/1389082-c9e9b078489b97a3.png?imageMogr2/auto-orient/strip%7CimageView2/2)
2.以开源中国为例作为服务器代码托管(也可以用gitlab、github座位服务器代码托管)
(1).先注册开源中国,然后登录,拉到最下面点友情链接的第三个“码云”
![](http://upload-images.jianshu.io/upload_images/1389082-0711eefc20f94348.png?imageMogr2/auto-orient/strip%7CimageView2/2)
或者直接戳这http://git.oschina.net 然后注册登录,界面如下:
![](http://upload-images.jianshu.io/upload_images/1389082-447ce1141f9cb470.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-9b7cdd72033b9ae3.png?imageMogr2/auto-orient/strip%7CimageView2/2)
然后会给你注册时候的邮箱,发送一个验证邮件,注册一下
![](http://upload-images.jianshu.io/upload_images/1389082-3a02ff289001fa5d.png?imageMogr2/auto-orient/strip%7CimageView2/2)
然后点击链接,会跳转到主页
![](http://upload-images.jianshu.io/upload_images/1389082-92639bcdf704c6db.png?imageMogr2/auto-orient/strip%7CimageView2/2)
备注:已注册的童鞋忽略这一步
(2).创建项目
1).如果你是项目的创始人(小红), 那么你就点击项目(进行创建)
![](http://upload-images.jianshu.io/upload_images/1389082-fcee7cb8143103ff.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-c6e81417ac8313f9.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-9ce965094c6b1ef4.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(3.1)点击右上角管理
![](http://upload-images.jianshu.io/upload_images/1389082-6fdcb778935e8ea8.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(3.2)点击添加项目成员, 我这里另一个(小明)是lidongxu_work@126.com
![](http://upload-images.jianshu.io/upload_images/1389082-f262813c2dc644e9.png?imageMogr2/auto-orient/strip%7CimageView2/2)
这里说一下:
管理员: 最高权限
开发者: 可以上传或者克隆(下载)代码 (一般给予这个权限)
观察者: 只能下载, 无法上传
报告者: 只能看, 和评论, 什么也不能干!
(3.3)输入对方oschina邮箱账号, 然后给予权限![](http://upload-images.jianshu.io/upload_images/1389082-eb267c76cd24c57f.png?imageMogr2/auto-orient/strip%7CimageView2/2)
回到项目首页(注意是点你的项目, 例如我这里是a1209woshi)
![](http://upload-images.jianshu.io/upload_images/1389082-032a45c735ca4320.png?imageMogr2/auto-orient/strip%7CimageView2/2)
我这里获取到的是 https://git.oschina.net/women2323/a1209woshi.git
![](http://upload-images.jianshu.io/upload_images/1389082-46a8eb89d28ba82d.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-6199c116f5fae565.png?imageMogr2/auto-orient/strip%7CimageView2/2)
填写克隆URL地址如果是第一次使用SouceTree(并且是私人项目), 会弹出让你输入你的oschina账号和密码(小红)
![](http://upload-images.jianshu.io/upload_images/1389082-0279b4424664373f.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-03e539c44d28a1d3.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-9ae2cef5c28727be.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.1) 找到本地仓库文件夹(我的在桌面)
![](http://upload-images.jianshu.io/upload_images/1389082-73673f41b09b4365.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.2) 然后看工作区有变化, 然后把未暂存文件, 放入已暂存文件区中.
![](http://upload-images.jianshu.io/upload_images/1389082-661ee7d42e352547.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-31e3bf8cb4678f59.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.3) 这里说一下, Git的工作原理图
![](http://upload-images.jianshu.io/upload_images/1389082-060575dc8c864e06.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.4) 然后commit(提交)到本地仓库分支中
![](http://upload-images.jianshu.io/upload_images/1389082-0223e5ab067d63c6.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-0ef5cbf6a8584974.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-a2160f8eebdebe6f.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.5) 养成一个好习惯, 先从远程仓库拉去一下, 然后在本地合并好了, 然后再推送回到远程仓库中, (也就是说, 合并代码是在本地进行的) (注: 如果你不拉取, 很容易把别人刚提交的代码, 顶掉)
![](http://upload-images.jianshu.io/upload_images/1389082-60106db6e9614880.png?imageMogr2/auto-orient/strip%7CimageView2/2)
![](http://upload-images.jianshu.io/upload_images/1389082-beba1920197716ee.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(6.6) 回到网页上去看看, 刷新下, 会看到我刚传的项目
![](http://upload-images.jianshu.io/upload_images/1389082-8cff1f3988ef337f.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(7.1) 同样, 从URL克隆(新建本地仓库)
![](http://upload-images.jianshu.io/upload_images/1389082-46c59c4fa82df864.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(7.2) 然后填入克隆网址, 然后选择本地仓库位置
![](http://upload-images.jianshu.io/upload_images/1389082-baf7f98b097af86c.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(7.3) 进入到工作区之后, 因为我是要克隆代码从远程仓库到本地, 所以直接点击拉取
![](http://upload-images.jianshu.io/upload_images/1389082-1bd2d8a584a0480c.png?imageMogr2/auto-orient/strip%7CimageView2/2)
(7.4) 你会惊奇的发现, 本地仓库里, 已经有了远程仓库的东西啦!!(以后再写好代码, 只需要执行第6步)就可以了, 这样就可以方便和别人进行团队协作开发了)
4.用x-code管理代码
不用Sourcetree,直接用X-code进行代码版本管理,
参考http://www.jianshu.com/p/300f8fd043c5
source tree传到GitHub:
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步