Chinaunix首页 | 论坛 | 博客
  • 博客访问: 100726
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2018-05-23 12:43
文章分类

全部博文(41)

文章存档

2016年(2)

2015年(3)

2014年(16)

2013年(20)

分类: 项目管理

2015-02-04 17:29:00

Git教程总结:                                                                                            
---------------------------------------------------------------------------------------------------------
git-----|                                                                                                
     |----安装git----|                                                                                      
     |             |--Ubuntu--|                                                                                       
     |            |      安装git:                                                                                         
     |            |            $ sudo apt-get install git                                                                     
     |            |      配置git:                                                                                         
     |            |            $ git config --global user.name "Your Name"                                                             
     |            |            $ git config --global user.email "email@example.com"                                           
     |                                                                                                      
     |                                                                                                      
     |--创建版本库---|                                                                                      
     |            |--创建本地git工作目录--|                                                                          
     |            |                  $ mkdir git                                                                                  
        |            |                  $ cd git                                                                               
        |            |                  $ git init                                                                             
        |            |                                                                                            
        |            |--第一次提交到本地git库--|                                                                  
        |            |                  $ vim liuxu0315.txt                                                                    
        |            |                  $ git add liuxu0315.txt                                                                
     |            |                  $ git commit -m "add liuxu0315.txt"                                                          
     |                                                                                                           
        |                                                                                                
        |--时光机穿梭---|                                                                                
     |            |-------|                                                                                          
     |            |      查看当前状态:                                                                                   
     |            |            $ git status                                                                                       
        |            |      查看difference:                                                                            
     |            |            $ git diff                                                                                     
        |            |                                                                                            
        |            |--版本退回--|                                                                               
        |            |-------查看提交历史记录:                                                                   
     |            |            $ git log                                                                                      
     |            |            $ git log --pretty=oneline                                                                     
        |            |-------退回上一个版本:                                                                     
     |            |            $ git reset --hard HEAD^                                                                       
     |            |      退回上上一个版本:                                                                               
     |            |            $ git reset --hard HEAD^^                                                                      
        |            |      退回往上100个版本:                                                                        
     |            |            $ git reset --hard HEAD~100                                                                    
     |            |-------从上一版本回到当前版本:                                                                   
     |            |            查看历史命令:                                                                                 
     |            |                  $ git relog                                                                                       
     |            |                  (得到当前版本commid id:3628164)                                                              
     |            |            返回到当前版本:                                                                               
     |            |                  $ git reset --hard 3628164                                                                           
        |            |                                                                                            
     |            |--撤销修改--|                                                                                     
     |            |-------修改文件:                                                                                 
     |            |            $ vim liuxu0315.txt                                                                            
     |            |            (噢,改错了,要返回修改前)                                                                     
     |            |      撤销修改:                                                                                       
     |            |            $ git checkout -- liuxu0315.txt                                                                
        |            |-------再次修改:                                                                           
     |            |            $ vim liuxu0315.txt                                                                            
     |            |            $ git add liuxu0315.txt                                                                        
     |            |            (完蛋了,又修改错了)                                                                          
     |            |      取消此次add:                                                                                    
     |            |            $ git reset HEAD liuxu0315.txt                                                                 
     |            |      撤销修改:                                                                                       
     |            |            $ git checkout -- liuxu0315.txt                                                                
        |            |                                                                                            
     |            |--删除文件--|                                                                                     
     |            |      $ rm liuxu0315.txt                                                                               
     |            |      $ git rm liuxu0315.txt                                                                           
     |            |      $ git commit -m "rm liuxu0315.txt"                                                               
     |                                                                                                               
     |                                                                                                               
        |---远程仓库----|                                                                                
        |            |--创建设置SSH Key--|                                                                        
     |            |      1.创建SSH Key:                                                                                   
     |            |            $ ssh-keygen -t rsa -C "youremail@example.com"                                                 
     |            |      2.将~/.ssh/id_rsa.pub复制到github的"个人设置-->SSH Keys"处。                                         
        |            |                                                                                            
        |            |--添加远程库--|                                                                             
     |            |      1.在github中先添加好一个仓库。                                                                   
     |            |      (得到一个如:git@github.com:liuxu0315/helloworld.git 这样地址)                                   
     |            |      (liuxu0315则是你的github ID)                                                                    
     |            |      2.在本地仓库下运行:                                                                             
     |            |            $ git remote add origin git@github.com:liuxu0315/helloworld.git                                
     |            |      此时可以推送到远程github仓库了:                                                                 
     |            |            $ git push -u origin master                                                                    
     |            |            (参数 -u 是将本地master分支和远程master分支关联起来)                                         
     |            |                                                                                                  
        |            |--克隆远程仓库--|                                                                           
     |            |      $ git clone git@github.com:michaelliao/learngit.git                                              
     |                                                                                                               
        |                                                                                                
        |---远程分支----|                                                                                
     |            |--创建与合并分支--|                                                                               
     |            |-------创建并跳转到dev分支:                                                                       
     |            |            $ git checkout -b dev                                                                          
     |            |            (git checkout为跳转到分支,-b为新建分支)                                                     
     |            |-------创建dev分支:                                                                              
     |            |            $ git branch dev                                                                               
     |            |      跳转到dev分支:                                                                                  
     |            |            $ git checkout dev                                                                             
     |            |-------查看分支:                                                                                 
     |            |            $ git branch                                                                                   
     |            |                                                                                                  
        |            |-------然后在dev分支上工作:                                                                
     |            |            $ vim dev.txt                                                                                  
     |            |            $ git add dev.txt                                                                              
        |            |            $ git commit -m "add dev.txt"                                                            
     |            |-------回到master合并dev分支:                                                                    
     |            |            $ git checkout master                                                                          
     |            |            $ git merge dev                                                                                
        |            |-------删除dev分支:                                                                        
     |            |            $ git branch -d dev                                                                            
        |            |                                                                                            
     |            |--解决冲突--|                                                                                     
     |            |      (当在dev和master同时修改dev.txt文件后,)                                                       
     |            |      (在master中合并dev分支是会冲突)                                                                
     |            |      (此时需要编辑一下dev.txt解决掉冲突,)                                                          
     |            |      (然后再add , commit提交)                                                                       
     |            |                                                                                                  
     |            |--工作区暂存--|                                                                                   
     |            |      (用于:需要跳转到其他分支,)                                                                   
     |            |      (      又不想commit当前分支,)                                                                     
     |            |      (      也不想放弃当前修改时)                                                                       
        |            |                                                                                            
     |            |-------保存当前工作现场:                                                                         
     |            |            $ git stash                                                                                    
        |            |      (跳转到其他分支工作后再回来此分支)                                                       
     |            |-------恢复工作现场:                                                                             
     |            |            1.查看以保存的stash内容:                                                                      
     |            |                  $ git stash list                                                                             
     |            |                  (可以看到0个或多个stash@{n},n=1,2,3,4...)                                                             
        |            |            2.恢复工作现场:                                                                         
        |            |                  $ git stash apply                                                                      
     |            |                  (或者)                                                                                      
     |            |                  $ git stash apply stash@{0}                                                                  
        |            |            3.删除stash内容:                                                                        
        |            |                  $ git stash drop                                                                       
        |            |---------------直接恢复并删除stash内容:                                                    
     |            |                  $ git stash pop                                                                              
        |                                                                                                
        |                                                                                                
        |---标签管理----|                                                                                
        |            |--创建标签--|                                                                               
     |            |-------快速创建标签:(例:$ git tag )                                                     
        |            |            $ git tag v1.0                                                                           
     |            |      查看标签:                                                                                       
     |            |            $ git tag                                                                                      
     |            |      给以前的commit id为6224937打标签:                                                               
        |            |            $ git tag v0.9 6224937                                                                   
        |            |      查看标签详细信息:                                                                         
        |            |            $ git show v0.9                                                                          
        |            |-------创建带说明的标签(commit为3628164):                                                
     |            |            $ git tag -a v0.1 -m "version 1.0" 3628164                                                     
        |            |      创建带GPG密钥的标签(确定本地已经配置和GPG KEY):                                         
     |            |            $ git tag -s v0.2 -m "signed version 0.2 released" fec145a                                     
     |            |                                                                                                  
        |            |--操作标签--|                                                                               
        |            |-------删除本地某个标签:                                                                   
     |            |            $ git tag -d v0.1                                                                              
     |            |      推送某个标签到远程仓库:                                                                         
     |            |            $ git push origin v1.0                                                                         
        |            |      一次全部推送未推送的本地标签:                                                             
        |            |            $ git push origin --tags                                                                 
        |            |-------删除远程仓库标签:                                                                   
     |            |            $ git tag -d v0.9                                                                              
        |            |            $ git push origin :refs/tags/v0.9                                                        
        |                                                                                                
        |                                                                                                
        |---自定义Git---|                                                                                
        |            |--忽略特殊文件--|                                                                           
     |            |      1.将忽略的文件写入.gitignore中                                                                   
        |            |            $ vim .gitignore                                                                         
        |            |            (如:)                                                                                     
        |            |            (dev.txt)                                                                              
        |            |            (*.class)                                                                              
        |            |            (temp/)                                                                                
     |            |      2.提交.gitignore到git仓库                                                                        
        |            |            $ git add .gitignore                                                                     
        |            |            $ git commit -m "add .gitigonre"                                                         
        |            |                                                                                            
        |            |--配置别名--|                                                                               
     |            |      $ git config --global alias.st status                                                            
        |            |      $ git st                                                                                   
        |            |      $ git config --global alias.co checkout                                                    
     |            |      $ git config --global alias.ci commit                                                            
     |            |      $ git config --global alias.br branch                                                            
     |            |      $ git config --global alias.unstage 'reset HEAD'                                                 
     |            |      $ git config --global alias.last 'log -l'                                                        
     |            |      $ git config --llobal alias.lg "log --color --graph                                              
     |            |            --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset                                           
     |            |            %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"                                    
     |            |      $ git lg                                                                                         
     |            |                                                                                                  
     |            |      (以上配置放在 .git/config 中,)                                                                 
     |            |      (个人配置文件在 ~/.gitconfig 中)                                      

阅读(1673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~