git、githup使用

一、git安装、配置

  git安装:

    root@ubuntu~# apt-get  install  git

  git配置githup/自己的git服务器端账号, 即在用户的home目录下生成.gitcofig文件,可手动修改:

    root@ubuntu~# git config --global user.name "Your Name Here"      

    root@ubuntu~# git config --global user.email your_email@example.com

    root@ubuntu~# git config --global color.ui true

  查看已有的配置信息:

    root@ubuntu~# git config --list

二、git使用:

  git创建仓库(repository,老外简写repo):

    新建项目目录,eg: cd  /root/mygit

      1、首次将本地的项目初始化到git服务器:

        root@ubuntu~# git init    #创建一个空的repository,在当前目录下生成了.git文件夹

        root@ubuntu~# git add *

        root@ubuntu~# git commit -m 'initial project version'

        root@ubuntu~# git remote add origin git@github.com:username/project_name.git     # 即刚刚创建的仓库的地址

        root@ubuntu~# git push -u origin master                          #推送代码到远程代码库

      2、将git服务器代码初始化到本地:      

        root@ubuntu~# git clone git_address                           #把GitHub里的项目复制到本地    
  git添加及提交
    root@ubuntu~#git  add   demo.py                                 #添加demo.py文件

    root@ubuntu~#git status                                     #查看状态 

    root@ubuntu~#git commit  -m "log here"  #不加参数-m,则默认使用vi打开一个文件写入log      #等同于svn的commit 

    root@ubuntu~#git status

 1          _______________________________
 2         |                               |   
 3         |           histroy             |   
 4         |_______________________________|
 5                         |   
 6                         |   
 7                         | git commit
 8          _______________|_______________
 9         |                               |   
10         |           staging area        |   
11         |_______________________________|                                 
12                          |   
13                          |   
14                          | git add 
15          ________________|_______________
16         |                               |   
17         |           working  dir        |   
18         |_______________________________|                                                                        

   root@ubuntu~#git status -s    #查看三者之间的是否相同,标记位:M , MM,  

   root@ubuntu~#git diff       #比较working dir 与staging area之间的差异

   root@ubuntu~#git diff   --staged    #比较staging area 与 history之间的差异,用于add之后检测

  撤销误操作:

   root@ubuntu~#git add demo    #add一版代码到staging area

   root@ubuntu~#git status -s     

   M   demo

   root@ubuntu~#git  reset demo   #将history中的demo覆盖staging area

   root@ubuntu~#git checkout demo    #将staging area中文件覆盖到working dir

   直接从history覆盖到working dir:

   root@ubuntu~#git  checkout HEAD demo 

     同理将working dir中改动直接提交到history:

   root@ubuntu~#git  commit -am "update log"

 删除以及重命名:

     root@ubuntu~#git rm  demo

   root@ubuntu~#git status -s

   root@ubuntu~#git commit -m 'delete demo'

  保存working dir中的demo文件

   root@ubuntu~#git rm --cached demo    #只是把staging area中demo删除了

   root@ubuntu~#git reset demo        #把history中的demo覆盖到staging area中

   root@ubuntu~#git  mv demo demo.bak   #把working dir和staging area中重命名

   root@ubuntu~#git commit -m 'rename demo' #history也重命名了

   root@ubuntu~# git rm --cached demo.bak  #只修改了staging area部分

   root@ubuntu~#mv demo.bak demo

   root@ubuntu~#git add demo

   root@ubuntu~#git status -s

   root@ubuntu~#git commit -m 'rename demo.bak'

  赞存工作区:在代码修改之后,马上需要在原版本上修改bug,此时就可以将此时的工作区临时保存起来;

    root@ubuntu~#git stash    #此时working dir是history中的内容

    root@ubuntu~#vim  demo

    root@ubuntu~#git commit  -am 'quick fix'  #紧急提交,修改bug

    root@ubuntu~#git stash list         

    root@ubuntu~#git  stash pop         #将临时工作区恢复到working dir

  分支管理:

    root@ubuntu~# git  branch          #列出当前所有分支

    * master                    #*表示当前使用的分支
    root@ubuntu:~# git branch try_idea      #创建branch
    root@ubuntu:~# git branch
    * master
      try_idea
    root@ubuntu~# git checkout try_idea      #切换到try_idea分支
    D    aaa.py
    Switched to branch 'try_idea'
    root@ubuntu-ceph-06:~/cp/git# git branch
     mater
    * try_idea                     #当前分支

    root@ubuntu~#git branch -d  try_idea       #删除分支

    联合操作,创建分支并切换到新建的分支上:

    root@ubuntu~#git checkout -b try_idea

  版本合并:

    root@ubuntu~#git branch mybranch      

    root@ubuntu~#git checkout mybranch

    root@ubuntu~#vim  demo          #做下修改;

    root@ubuntu~#git commit -am "mybranch commit"

    root@ubuntu~#git  checkout master

    root@ubuntu~#git  merge  mybranch    #将mybranch分支合并到master分支

    root@ubuntu~#git branch -d mybranch   #只有在合并之后才能删除分支,否则报错

 分支版本合并:如图

 1                                                   master branch
 2   ____           ____            ____              ____
 3  |    |         |    |          |    |            |    |   
 4  |  a |  <---   |  b |    <---  |  c |  <----     | e  |
 5  |____|         |____|          |____|            |____|    
 6                                    |   
 7                                    |                bugfix branch
 8                                  __|_              ____
 9                                 |    |            |    |   
10                                 |  d |  <----     |  f | 
11                                 |____|            |____|

    root@ubuntu~#git merge  buffix      #在master版本上,并提交版本,到达了e版

    出现需要合成临时版本的情况,即c版合并f版,生成临时版本之后,再于e版本合并;

    出现的对话框,选择ctrl+x即可;若有冲突则提交不成功;需要手动vim修改

 git log, 如果嫌输出信息太多,看得眼花缭乱的,可以试试加上--pretty=oneline参数:

用带参数的git log也可以看到分支的合并情况: 

1 root@ubuntu-ceph-06:~/cp/mygit# git log --graph --pretty=oneline --abbrev-commit
2  *   f0be77e configct fixed
3  |  
4  | * 290e5e3 change master
5  | * c539383 branche test
6  * | a315058 aa.txt
7  |/  
8  * e51e421 msg
9  * 6c84560 init msg

git分枝策略:

 

原文地址:https://www.cnblogs.com/chris-cp/p/5042033.html