一文 读懂 Git 常用命令

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lm9521/article/details/89648297

1.  自报家门 

    git config --global user.name SLIGHTLEE
    git config --global user.email [email protected]

    
    查看配置 
    git config --list


2. 创建版本库

 进入到 E盘目录   cd e:/
 
 创建文件夹    mkdir  shopMatnager
 
 创建仓库      git init   


 
2.1 添加文件  2.1.1  创建一个文件 hello.txt
 

  查看状态   git status

  提交文件到暂存区   git add hello.txt  或者  当前目录下的所有文件    git add .
  
  提交文件到版本库   git commit -m "init"      -m 注释


    
  ## 将工作区文件 直接添加到版本库  
  git commit -am "直接提交"
  


  -------------------------
  工作区   暂存区   版本库
  -------------------------
  
  2.1.2  撤销版本描述
  
    撤销上一次提交 并将暂存区文件从新提交 
    修改文件 add 到暂存区 然后使用
    git commit --amend 
    
    可以把版本描述 修改
    
  
  2.1.3  撤销工作区修改
  


    撤销单个文件提交    git checkout -- index.txt
    撤销多个文件提交    git checkout -- .

  2.1.4  撤销暂存区修改    
  

    git reset HEAD index.txt


    
2.2 删除文件
 
 2.2.1  
 
 假如删除文件 read.txt 
 使用 git 删除   git rm read.txt
 查看状态  绿色的
 git commit -m "删除文件" 
 
 假如是手动删除   则需要 加一步  git  add  操作
 
  
  
 2.3 推到远程仓库 
 
  2.3.1  
  
  第一种:
  官方提供步骤 :

  git remote add origin https://gitee.com/leagle/hand_in_hand.git
  git push -u origin master

  git remote add origin https://gitee.com/leagle/y-mall.git

  git pull origin master


  
  远程地址的简写  添加远程库的别名为  origin 
  
  【第一次推送的时候使用 -u  以后不需要】
  
  
  第二种:
  git push  https://gitee.com/leagle/hand_in_hand.git master
 
 
 2.4 从远程仓库拉取项目
 
 (新用户操作)
  2.4.1 在磁盘操作 先进入一个盘符
 

    cd d:/
    git clone https://gitee.com/leagle/hand_in_hand.git


    
  2.4.2  修改文件之后  把文件 add 到工作区  commit 暂存区 
        
         push 到 远程仓库  git push https://gitee.com/leagle/hand_in_hand.git master
 
 (之前用户操作)
  2.4.3 进 d 盘 更新 从远程仓库拉取项目  
  git pull https://gitee.com/leagle/hand_in_hand.git master
  
  2.5 改动日志
  
   2.5.1 
     查看项目日志     git log
     查看本目录的日志 git log .
    
   

     感觉log乱 可以使用 git log --pretty=oneline   或则  git log --oneline


     如下:
        e378ce033a1469efef6c40c0599f29d0a4f33768 (HEAD -> master) 更新 Remember.md
        2f7a71801dc08a1dba933960a84c9fc8089493db (origin/master) edit content
        9f79b1ecbfbad70e5923ed042cc7b75d96bf447a delete 文件
        ce63053ff7246c4cb5f86369c9d095966116a725 提交hello.txt
        8e1b8380500a438040527ad0b7581e2204a8acec 第一次提交
        
  2.6 版本切换
  
   

   查看版本变化  git reflog


      
        e378ce0 (HEAD -> master) HEAD@{0}: pull https://gitee.com/leagle/hand_in_hand.git master: Fast-forward
        2f7a718 (origin/master) HEAD@{1}: commit: edit content
        9f79b1e HEAD@{2}: commit: delete 文件
        ce63053 HEAD@{3}: commit: 提交hello.txt
        8e1b838 HEAD@{4}: commit (initial): 第一次提交
      
      
      当head指向当前1版本   git reset --hard HEAD^ 
      
      版本号切   git reset --hard 2f7a718
      
  2.7 分支管理
   
   2.7.1 切换分支
   

   查看分支 git branch
   创建分支 git branch wechat
   切换分支 git checkout wechat
  
   修改分支名字 git branch -m wechat cc
   
   创建分支并切换分支 git branch -b dev 


  
   1. 在wechat 分支操作 add  commit 
  
   2.7.2  合并分支
   
    1. 把 wechat 合并到 master
  
    

  合并分支(切换到master操作) git merge wechat


  
     2. 不同的分支都修改了同一个文件
      
      如下 :
      lalalal  wechat

        <<<<<<< HEAD

        master 修改
        =======
        修改 wechat
        >>>>>>> wechat
        
        把 冲突部分 注释去掉
          lalalal  wechat

            master 修改
        
            修改 wechat
        
        然后 add  commit 
        
        
        利用强覆盖方式用你本地的代码替代git仓库内的内容   git push -f

   2.7.3 删除分支

  

      git branch -d wechat


        
   2.8 远程仓库
   
    2.8.1 
    
    #查看远程仓库别名
  

 
      查看远程仓库     git remote
      查看远程仓库地址 git remote -v
      


      
    #删除远程仓库别名(比如别名为 origin )
    

    git remote remove origin

     
         
    2.9  

     2.9.1 创建ssh key     
     
     ssh-keygen -t rsa -C "[email protected]"
     
     一路回车。。。
     
     .id_rsa  私钥  .id_rsa.pub 公钥 
     
     本地用私钥  远程配公钥
      
      
      
      
      
      

猜你喜欢

转载自blog.csdn.net/lm9521/article/details/89648297