zoukankan      html  css  js  c++  java
  • java基础---->git的使用(一)

      这里面记录一下git的使用,只是平时工作中遇到的一些问题的解决方案,不会涉及到git的一些基础概念及说明。人的天性便是这般凉薄,只要拿更好的来换,一定舍得。

    Git的一些使用

    一、在码云建立好仓库之后,想把本地已经写好的代码推送上去。

    首先git init我们的项目:

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1
    $ git init
    Initialized empty Git repository in G:/Java/Go/program/2017-05-18/LearnPython1/.git/

     添加项目到本地的仓库:

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
    $ git add .

     提交代码到本地仓库:

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
    $ git commit -m 'commit python code'
    [master (root-commit) 4408093] commit python code
     110 files changed, 5905 insertions(+)
     create mode 100644 .idea/misc.xml
     create mode 100644 .idea/modules.xml
     create mode 100644 .idea/workspace.xml
     create mode 100644 LearnPython1.iml
     create mode 100644 datagram/connMysql.py
     create mode 100644 datagram/funUtils.py
     create mode 100644 datagram/huhx.py
     create mode 100644 file/bookdata.csv
     create mode 100644 file/huhx.png
     create mode 100644 file/huhx1.txt
     create mode 100644 file/huhx2.txt
     create mode 100644 file/log.txt
     create mode 100644 file/log1.txt
     create mode 100644 file/seach_button.png
     ......

    与服务器的分支建立联系:

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
    $ git remote add origin https://gitee.com/huhx/pythonLearn.git

    推送代码到远程仓库:

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
    $ git push -f origin master
    Counting objects: 137, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (130/130), done.
    Writing objects: 100% (137/137), 127.37 KiB | 1.46 MiB/s, done.
    Total 137 (delta 1), reused 0 (delta 0)
    To https://gitee.com/huhx/pythonLearn.git
     + 45c1fff...4408093 master -> master (forced update)

     注意上述的推送需要加-f参数,如果没有添加的话。会有如下的错误提示

    huhx@Linux MINGW64 /g/Java/Go/program/2017-05-18/LearnPython1 (master)
    $ git push origin master
    To https://gitee.com/huhx/pythonLearn.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://gitee.com/huhx/pythonLearn.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.

    二、建立分支的一些基础操作,更新或者提交代码

    整个的一个流程,可以如下的方式:

    创建开发分支:
      git checkout huhx-dev 添加到git管理:
      git add . 提交到huhx
    -dev本地仓库:
      git commit -m 'code' 切换到master分支:
      git checkout master 从远程仓库更新代码:
      git pull 合并huhx
    -dev分支:
      git merge huhx-dev 提交到本地的mater仓库:
      git commit
    -m 'master commit' 将本地的master代码提交到远程:
      git push origin master

    关于分支创建、查看可以参考博客:http://blog.csdn.net/arkblue/article/details/9568249

    三、git撤销的操作

    我们需要撤销上次提交的commit,注意这里还没有执行push的操作。举个例子来加以说明这个撤销的过程。比如我们我们修改提交了文件redis.txt文件。

    $ git add code/imp-root/imp-db/impserver_db/redis.txt

    此时如果我们要撤销git add的操作,可以输入以下的命令:

    $ git rm --cached  code/imp-root/imp-db/impserver_db/redis.txt
    rm 'code/imp-root/imp-db/impserver_db/redis.txt'

    我们接着去提交redis.txt,先git add再git commit操作。命令如下:

    $ git add code/imp-root/imp-db/impserver_db/redis.txt
    $ git commit -m 'test commit'
    [master 49e3162f] test commit
     1 file changed, 3 insertions(+), 1 deletion(-)

    现在我们开始commit的撤销操作,这个操作分为三种类型。git reset –-mixedgit reset –-softgit reset –-hard

    • git reset –-mixed:此为默认方式,不带任何参数的git reset,它回退到某个版本,保留修改后的文件源码,回退commit和index信息。也就是如果再提交,需要经过git add和git commit操作。
    • git reset –-soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。保留修改后的文件源码,如果还要提交,直接git commit即可。
    • git reset –-hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容。

    我们对git reset –mixed这种方式做一个测试,也就是默认的git reset commitid。首先我们需要通过git log得到最后的commit id。如下

    $ git log
    commit 49e3162fb3cf244f809d9b38bc1ed9c5e651ea49 (HEAD -> master)
    Author: huhongxiang <huhongxiang@csii.com.cn>
    Date:   Wed Dec 27 18:23:02 2017 +0800
    
        test commit
    
    commit ce477925b378e8d3ec451caafb0b72c811dc697b (origin/master)
    Author: huhongxiang <huhongxiang@csii.com.cn>
    Date:   Wed Dec 27 17:40:11 2017 +0800
    
        卡券赠送

    我们要回退到上一个版本,也就是commitId=ce477925b378e8d3ec451caafb0b72c811dc697b。所以执行撤销的命令如下:

    $ git reset ce477925b378e8d3ec451caafb0b72c811dc697b
    Unstaged changes after reset:
    M       code/imp-root/imp-db/impserver_db/redis.txt

    再通过git status,可以看到redis.txt也回退了index的信息。

    $ git status
    On branch master
    Your branch is up to date with 'origin/master'.
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   code/imp-root/imp-db/impserver_db/redis.txt

    此时执行git log,可以看到如下的输出。可以知道已经看不到redis.txt的commit提交记录了。

    $ git log
    commit ce477925b378e8d3ec451caafb0b72c811dc697b (HEAD -> master, origin/master)
    Author: huhongxiang <huhongxiang@csii.com.cn>
    Date:   Wed Dec 27 17:40:11 2017 +0800
    
        卡券赠送

    如果我们需要撤销git push到远程分支的操作,后续补充。

    友情链接

  • 相关阅读:
    sql中top使用方法
    hive查询练习
    sqoop课堂总结
    hive分区表与数据关联的三种方式
    hive中partition如何使用
    方法层!
    針對數據庫的數據的增刪改查的功能做接口
    Web Project犯错误!
    HttpServlet 详解(注!仿)
    创建一个程序,从应用程序中随机添加N名参加歌唱比赛的同学,并随机对这N名同学的比赛按姓名的拼音先后顺序进行排序
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusejavagit1.html
Copyright © 2011-2022 走看看