zoukankan      html  css  js  c++  java
  • git总结一、工作中常用基础命令

    首先来了解两个概念:

    工作区:比如你的项目目录
    暂存区:git和其他版本控制系统的不同之处就是有这个暂存区的概念。
    .git不是工作区,而是git 版本库,在版本库中存放着很多东西,比如暂存区(stage),git为我们创建的第一个分支master,以及指向master的一个指针head
                        
    一、提交系列命令:
    git add .     // 将文件修改添加到暂存区;
    git commit -m ""    // 将暂存区的所有内容提交到当前分支
     
    二、查看信息命令:
    1、git status    //查看目前状态,这个命令很常用,运行这个命令git能够给我们提供很多信息,比如以下的提示信息很常见
    $ git status
    On branch 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:   readme.txt
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        LICENSE
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked。

    2、

    git log       可以查看所有提交过的版本信息
    commit qaws1a4b1e251f07e4e8658fd2b1d6d0918ca44f (HEAD -> test_qy, origin/test_all, test_all)
    Author: qy <quyang@123.com.cn>
    Date:   Thu Apr 25 13:49:10 2019 +0800
    
        member page change
    
    commit qwsedeed7b208331ead8cc469fd568f0d4c2da05
    Author: qy <quyang@123.com.cn>
    Date:   Thu Apr 25 11:44:50 2019 +0800
    
       member strategy page style change
    
    commit 417fce1c1b1171f5ed9f5a5a6cb31e44ec41da77
    Author: zhangsan <zhangsan@123.com.cn>
    Date:   Thu Apr 25 11:26:49 2019 +0800
    
        modify some file
    
    commit 76ytd819f40a633d4ec3fc8f9d7662c6f234c0fb
    Merge: eb784ae 000efaf
    Author: lisi <lisi@123.com>
    Date:   Thu Apr 25 11:24:13 2019 +0800
    
        update them
    
    commit tru24ae4162639ff01346cc12c1d578bddb5a33e
    Author: lisi <lisi@123.com>
    Date:   Thu Apr 25 11:23:00 2019 +0800
    
        update continue
    git reflog      查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)
    fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{0}: checkout: moving from test_local to test_qy
    ae3c43b (origin/test_local, test_local) HEAD@{1}: cherry-pick: member page change
    01311f0 HEAD@{2}: checkout: moving from test_all to test_local
    fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{3}: merge test_qy: Fast-forward
    3f625ee HEAD@{4}: checkout: moving from test_qy to test_all
    fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{5}: checkout: moving from test_all_0001 to dbg_qy_0001
    3f625ee HEAD@{6}: checkout: moving from dbg_qy_0001 to dbg_all_0001
    fabe1a4 (HEAD -> test_qy, origin/test_all, test_all) HEAD@{7}: commit: member page change
    3f625ee HEAD@{8}: checkout: moving from test_local to test_qy
    01311f0 HEAD@{9}: cherry-pick: member strategy page style change
    3a60b15 HEAD@{10}: pull: Fast-forward
    84eee19 HEAD@{11}: checkout: moving from test_all to test_local

    三、回退系列命令:

    1、丢弃工作区的修改(此时还没有执行git add .)

    git checkout -- "文件路径"  删除指定文件的修改,执行后工作区是干净的

    2、丢弃暂存区的修改 (此时已经执行了git add .但是还没有执行git commit -m "")

    要用的是git reset命令,步骤如下:

    git log    查看commit历史信息,拿到最新一次提交的commit-id

    git reset --hard/--soft/--mixed  "xxx"(这里指拿到的commit-id)  

    git reset命令有三个可选的参数:

    git reset --hard af4d64d4d5d51709a4a0af7db01e5f2a392f9a19

    此命令执行后,工作区中的所有未提交的修改都会丢失,无论是否是你想要保留的部分

    -- hard这个命令是很危险的,它是git里面少数几个会丢失信息的命令,虽然也可以通过git reflog来挽救,但最好还是不要使用此参数

    git reset --soft af4d64d4d5d51709a4a0af7db01e5f2a392f9a19

    此命令执行后,查看文件的状态并没有做任何改变,只是代码所作用的效果不显示了

    git reset af4d64d4d5d51709a4a0af7db01e5f2a392f9a19   //默认参数为mixed

    Unstaged changes after reset:

    M index.html

    此命令执行后,将暂存区的修改撤回到了工作区,即git add .之前的状态

    git status //查看文件状态,修改已经被撤回到暂存区

    On branch dbg_qy_0001

    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:   index.html

    no changes added to commit (use "git add" and/or "git commit -a")

    3、将提交到分支的修改撤回到工作区(即已经执行了git commit,但还没有git push)

    git log //拿到想要撤销的修改前一次的提交id

    git reset af4d64d4d5d51709a4a0af7db01e5f2a392f9a19

    git status  //查看状态看到提交到分支上的修改以已经撤回到工作区

    On branch test_qy

    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:   src/index.vue

     

    no changes added to commit (use "git add" and/or "git commit -a")

    4、将push到远程的个人分支的修改撤回(已经执行了git push但是还未merge到公共的repository) 

    这种情况同样也可以使用git reset命令来回退,但是当你回退到某一个版本再次push的时候,由于本地的版本落后于远程的版本,此时需要使用git push -f强制推送。

    注意这种情况是只有你一个人使用的远程分支

    5、要回退的代码已被merge(合入)到公共的repository

    git log   //查看提交历史信息,拿到要撤回的那次提交的commit-id

    git revert   "xxx"(commit-id) 

    若产生冲突,手动解决冲突,然后提交;

    git revert 与 git reset的区别:

    git reset执行后,要回退的那次提交之后的所有提交都被撤回到工作区,如果其他提交是团队其他开发人员做的,那么就会影响到其他人的代码,这样做是不合理的。

    git revert 撤销 某次操作,此次操作之前和之后的commit和history都会保留,并且把这次撤销
    作为一次最新的提交
        * git revert HEAD                  撤销前一次 commit
        * git revert HEAD^               撤销前前一次 commit
        * git revert commit (比如:fa042ce57ebbe5bb9c8db709f719cec2c58ee7ff)撤销指定的版本,撤销也会作为一次提交进行保存。
    git revert是提交一个新的版本,将需要revert的版本的内容再反向修改回去,

    四、删除命令:

    rm "文件"

    rm test.txt

    当误删了的时候
    Git checkout -- test.txt
     
     
    五、修改提交信息命令:
    使用 git commit --amend 命令(修改最近一次提交的注释信息)
    进入一个git编辑模式:
    按d键两遍,删除第一行
    换行
    键盘按“i”键,进入编辑模式
    输入commit名称:list edit
    按esc键
    键盘输入“:wq”(写入编辑信息并且退出)
     
    六、关于分支命令:
    1、git checkout -b dev //创建并切换到dev分支
    2、git merge dev //将dev分支合并到当前分支
    3、git branch -D dev //删除dev分支
     
    4、git rebase master   //将master分支在当前分支做一次衍合操作
     
    git rebase 合并分支的方法:
    (1)git checkout dev   
    (2)git rebase master
    (3)git checkout master
    (4)git merge dev
     
    关于git merge 和git rebase 想要详细了解的请看以下两篇文章:
    git rebase 和 git merge的具体工作原理:https://www.cnblogs.com/youyang-2018/p/10790422.html
     
     
     

     

     

     

     
     
  • 相关阅读:
    利用idea的code inspect功能进行代码静态分析
    AntiSamy测试
    常用git命令
    node 常用命令
    java中byte[] 和16进制字符串互转
    使用idea的条件断点快速定位注解的处理类
    使用数组初始化list
    django连接Mysql数据库
    20分钟快速用django+mysql+pycharm搭建一个自己的web网站学习笔记
    随便谈谈------关于django学习笔记
  • 原文地址:https://www.cnblogs.com/youyang-2018/p/10769314.html
Copyright © 2011-2022 走看看