git的使用

什么是Git

Git是分布式版本控制系统。

一、本地仓库的使用

    下载安装

       


  Windows为例:
   1)安装好之后再开菜单中找到 "Git - GitBash"点击打开Git命令行控制台。
   2)设置全局用户信息
      git config --global user.name "xiangff"     //设置全局用户名
      git config --global user.email 'xiangfeijava@163.com' //设置全局邮箱
 
      备注:项目可单独设置用户名和邮箱,只要把 --global去掉即可。
    3)使用本地Git
      i) 创建版本库: 在目录下执行 git init初始仓库.
      ii)创建文件添加并提交到本地版本库
          git add test.txt   //提交文件到本地版本库的暂存区
          git commit -m '说明' //提交到本地版本库
          git diff test.txt 查看文件更改的内容
      iii)其他操作
          git status 查看git当前状态
          git log 查看所有的commit记录
          git log --pretty=oneling 查看所有的commit记录,只显示说明信息
          git relog 查看所有版本号
          git reset -hard HEAD^ 回退到上一个版本
          git reset -hard 版本号(3f5a6453)

二、远程仓库的使用

           1) 注册github用户

           2) 新建+ (New repository)版本库

           3) 本地创建ssh公钥和私钥(Git Branch子弹)

                  ssh-keygen  -t rsa -C "xiangfeijava@163.com"

       4)将公钥设置到github上,Settings-> SSH and GPG keys -> New SSH Key, 将id_rsa.pub的内容复制到Key对应的输入框中,点击Add SSH Key即可。

        5) 本地版本库和远程版本库关联

            i) 克隆远程版本库到本地

                   git clone http://github.com/xiangffgh/helloworld.git

            ii)将已有的本地库关联到远程

                    git remote add origin https://github.com/xiangffgh/helloworld.git

        6)远程更新和提交

              git push origin master //提交

              git pull origin master //更新到最新版本

              

        7)分支管理    

               1、创建本地分支 local_branch

                    git branch local_branch

               2、创建本地分支local_branch 并切换到local_branch分支

                   git checkout -b local_branch

              3、切换到分支local_branch

                  git checkout local_branch

             4、推送本地分支local_branch到远程分支 remote_branch并建立关联关系

                  a.远程已有remote_branch分支并且已经关联本地分支local_branch且本地已经切换到local_branch

                    git push

                b.远程已有remote_branch分支但未关联本地分支local_branch且本地已经切换到local_branch

                   git pull origin dev 本地分支与远程分支相关联

              c.远程没有有remote_branch分支并,本地已经切换到local_branch

                  git push origin local_branch:remote_branch

              d.在本地新建分支并推送到远程

              git checkout -b test

              git push origin test   这样远程仓库中也就创建了一个test分支

           e.创建分支并直接关联远程分支

              git checkout -b 本地分支名称  origin/远程分支名称

           5、删除本地分支local_branch

                git branch -d local_branch

          6、删除远程分支remote_branch

               git push origin  :remote_branch

               git branch -m | -M oldbranch newbranch 重命名分支,如果newbranch名字分支已经存在,则需要使用-M强制重命名,否则,使用-m进行重命名。
               git branch -d | -D branchname 删除branchname分支
               git branch -d -r branchname 删除远程branchname分支

          7、查看本地分支

               git branch

         8、查看远程和本地分支

               git branch -a


三、常用命令和技巧


相关优秀书籍 

https://git-scm.com/book/zh/v2 (官方提供)

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000


相关优秀博文

http://blog.jobbole.com/78960/

http://my.oschina.net/u/877348/blog/152660

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值