zoukankan      html  css  js  c++  java
  • git@oschina.net源代码管理使用日记

    git的优势:

    1 可以创建分支;

    2 版本控制是基于每一次提交的,而不需要考虑每次提交了多少个文件。

    下载:

    下载网址为:http://git-scm.com/download,根据您的操作系统选择相应的安装宝,下载完后直接按照默认安装即可(本文讲述全部基于Windows操作系统)。

    概述:

    git源代码管理器的仓库位置可以基于本地服务器远程服务器两种方式:

    本地服务器主要用于管理自己的代码,将本机作为源代码管理服务器;

    远程服务器则用于将源代码公开,并由多个人来对代码进行编辑,主要用于团队开发中。

    git源代码的提交流程,可以用下面这张图片来表示:

    你的本地仓库由 git 维护的三棵“树”组成:

    1) 工作目录(WorkDir),它持有实际文件;

    2) 缓存区(Index),它像个缓存区域,临时保存你的改动;

    3) HEAD,指向你最近一次提交后的结果。

    经过上面三步以后,你的改动现在已经在本地仓库的 HEAD 中了,但是源代码并未保存(推送)到远程仓库,可以执行如下命令以将这些改动提交到远端仓库:git push origin master,(master表示你要提交的分支,可以把 master 换成你想要推送的任何分支)

    使用方法:

    1 初始化,创建项目;

    1)以本机或者局域网电脑作为源代码管理服务器,

    a) 需要在源代码管理服务器上面安装git

    b) 然后创建一个目录(如git)作为源代码存储根目录,如"c:\git"

    c)进入git目录,点击鼠标右键菜单中的“Git Init Here"按钮,即可完成初始化操作(或者利用”Git Bash命令行程序通过执行“git init“命令完成初始化);

    d)然后在该根目录下以项目为单位分别创建项目目录,或者创建一些文件,如readme(目录必须包含文件才会被提交,否则在提交的时候空目录会被忽略);

    e)对于上一部创建的项目目录,需要在仓库根目录点击右键菜单中的”Git Commit Tool“按钮,然后点击弹出界面中的”提交“按钮,或者在仓库根目录点击右键菜单中的”Git Bash“,弹出命令行窗口,依次输入:git add .回车git -commit -m 'some message'回车,才能将初始化目录和文件提交到代码仓库中

    2)如果oschina服务器作为源代码管理服务器,则省掉了初始化步骤,需要先在oschina的注册帐号,然后通过oschina提供的功能在线创建项目。访问地址举例:

      a)http方式:http://git.oschina.net/UserName/Test.git

      b)ssh方式:git@git.oschina.net:UserName/Test.git

    2 将git仓库中文件克隆到本地

    1)本地git仓库:进入要下载代码的目录入:c:\git-workspace-local,点击右键菜单中的”Git Bash"按钮,弹出命令行窗口,输入命令:git clone '本地git仓库根目录', 本文为"c:\git'

    2)局域网或者oschina仓库:进入要下载代码的目录入:c:\git-workspace-oschina,点击右键菜单中的”Git Bash"按钮,弹出命令行窗口,输入命令:git clone '远程git仓库地址', 如:http://git.oschina.net/UserName/Test.git或者git@git.oschina.net:UserName/Test.git(本人在采用git协议的进行clone的时候报错,不知是何故,还请阅者告之,谢谢!)

    3 创建分支,创建分支操作智能在源代码管理器中创建,而非本地workspace

    1)以本机或者局域网电脑作为源代码管理服务器:在源代码管理器服务器仓库根目录,点击右键菜单中的”Git History“,然后在弹出的”gitk:git“窗体左上角区域点击鼠标右键创建分支

    2)远程服务器(oschina),直接通过网站”创建分支“功能创建;

    4 分支切换: git checkout branchname

    5 文件提交

    1)在资源管理器中,创建/修改/删除/文件,或者在git bash工具中执行touch filename命令创建文件

    2)通过执行:git add filename(用“.”替换文件名表示提交当前目录的所有文件),将修改提交到workspace;

    3)通过执行:git commit -m 'some message of this commit', 将修改提交到缓冲区;

    4)通过执行:git push origin branchname(本地存储),或者通过执行 git push 回车 用户名 回车 密码 回车(远程服务器)将修改提交到源代码管理仓库中去

    注:在提交到本地仓库的时候,在执行上面第四步的时候,源代码管理仓库最好切换到与当前提交的的分支不同的分支(如现在有两个分支:master和MyBranch,当前工作区MyBranch进行checkout, 在执行git push的时候,源代码管理仓库最好切换到master 分支,否则在执行git push的时候会抱错)

    6 文件夹提交:不直接支持空文件夹的提交,需要在文件中创建文件,然后提交文件,这样会将文件所在的文件夹一起提交。

     

     常见错误

    在使用Git Push代码到数据仓库时,提示如下错误:

    [remote rejected] master -> master (branch is currently checked out)

    remote: error: refusing to update checked out branch: refs/heads/master

    remote: error: By default, updating the current branch in a non-bare repository

    remote: error: is denied, because it will make the index and work tree inconsistent

    remote: error: with what you pushed, and will require 'git reset --hard' to match

    remote: error: the work tree to HEAD.

    remote: error:

    remote: error: You can set 'receive.denyCurrentBranch' configuration variable to

    remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into

    remote: error: its current branch; however, this is not recommended unless you

    remote: error: arranged to update its work tree to match what you pushed in some

    remote: error: other way.

    remote: error:

    remote: error: To squelch this message and still keep the default behaviour, set

    remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.

    To git@192.168.1.X:/var/git.server/.../web

    ! [remote rejected] master -> master (branch is currently checked out)

    error: failed to push some refs to 'git@192.168.1.X:/var/git.server/.../web'

    解决办法:

    这是由于git默认拒绝了push到当前分支操作,需要进行设置,修改.git/config文件后面添加如下代码:

    [receive]
    denyCurrentBranch = ignore

    无法查看push后的git中文件的原因与解决方法:在仓库根目录git bash 命令窗口中执行命令 git reset --hard 才能看到push后的内容.

    在初始化远程仓库时最好使用(不知道为什么要这样做,使用该命令执行创建仓库后,创建的三一个空仓库,而且无法看到项目文件)

    git --bare init

    而不要使用:git init

    git init 和git --bare init 的具体区别:http://blog.haohtml.com/archives/12265

     

    推荐git学习的两篇文章:

    1)git - 简易指南

    2)图解Git[强烈推荐]

  • 相关阅读:
    leetcode-38.报数
    leetcode-35.搜索插入位置
    leetcode-27.移除元素
    leetcode-26.删除重复数组中的重复项
    leetcode-20.有效的括号
    leetcode-973最接近原点的K个点
    leetcode-14最长公共前缀
    leetcode-13罗马字符转整数
    MFC俄罗斯方块
    leetcode-9.回文数(水仙花数)
  • 原文地址:https://www.cnblogs.com/Juvy/p/3556902.html
Copyright © 2011-2022 走看看