• Git配置技巧及常用命令总结


    如果你想精通Git,直接到 Git官网 把这本ProGit掌握已足以Pro Git

    配置用户信息


    user和email,--global参数全局配置,当然你也可以不加此参数,不同的项目用不同的用户名和email

    git config --global user.name Super
    git config --global user.email 1342449****@163.com
    

    配置全局别名


    此配置在开发中相当重要,尤其是对于使用Terminal,习惯使用命令行的朋友,由于git不支持tab自动补全,每次想要看下工作目录 状态都要git status,相当耗时。除非你能确定你敲两个字母比六个字母用时少。

    git config alias.st "status"
    
    全局别名
    git config --global alias.st "status -s"
    git config --global alias.ci "commit -m"
    git config --global alias.aci "commit -a -m" (该配置很是方便,节省好多时间)
    直接将git add . 和 git commit -m 合并(在一本Pro Git书籍上看到过,网上我没有找到)
    git config --global alias.lg "log --color --pretty=format:'%Cred%h - %Cgreen%an %C(yellow)| %ad | %Creset%s' --graph" (自定义log)
    

    第一条:git status是开发中使用最多最频繁的,至于-s 是简洁输入(Give the output in the short-format)
    第二条:此条也使用频繁,但是我在开发中直接使用第三条跳过。
    第三条:配置git aci 因为这样直接跳过使用暂存区域,对于已经跟踪的文件,我不要再此次使用git add加入暂缓区,然后再git commit提交到本地数据库,为了方便省事,直接将两条命令合并为一条,使用git aci "提交说明" 即可。省不省事,用下自然知道。

    版本回退


    没有提交的情况下进行版本回退
    git reset --hard HEAD
    回退到上一个版本
    git reset --hard HEAD^
    回退到上上个版本
    git reset --hard HEAD^^
    回退到指定回退到某个版本
    git reset --hard 版本号(至少前5位)
    回退到前几个版本
    git reset --hard~1
    
    注意下面一行命令 reset和revert有本质区别
    git revert c011eb3c20ba6fb38cc94fe5a8dda366a3990c61
    

    分支管理


    多人开发或则多处管理分支时,可能会出现远程分支已经删除,但是在本地使用 
    'git branch -a'
    依旧可以看到
    
    此时可以使用 
    'git fetch -p'
    使fetch之后删除没有与远程分支对应的本地分支
    
    当然也可以通过查看远程分支
    'git remote show origin'
    
    YJTSuper:yjtim super$ git remote show origin 
    * remote origin
      Fetch URL: git@git.oschina.net:lingsui/yjtim.git
      Push  URL: git@git.oschina.net:lingsui/yjtim.git
      HEAD branch: master
      Remote branches:
        dev                      tracked
        im                       tracked
        master                   tracked
        proV2.3.0                tracked
        refs/remotes/origin/test stale (use 'git remote prune' to remove)
      Local branches configured for 'git pull':
        master merges with remote master
        show   merges with remote master
      Local refs configured for 'git push':
        dev       pushes to dev       (up to date)
        im        pushes to im        (fast-forwardable)
        master    pushes to master    (up to date)
        proV2.3.0 pushes to proV2.3.0 (up to date)
    
    我们可以看到分支origin/test 已经过期(stale)
    可以使用使用以下命令处理
    'Git remote prune origin'
    
    git branch dev 创建分支
    git merge dev  将dev分支合并到当前分支
    
    
    
    

    分支切换


    
    当我们正在当前分支编写代码,突然有某些比较烦人的家伙来中断你的思路,提出某地方需要修改而且比较紧急,
    更烦人的是当前的代码仅仅编写了一半,所以使用git commit提交的话,想不出好的提交说明,当然这时候提交也
    显得有所不适.这个时候,使用git stash就更加方便些
    
    暂时隐藏
    git stash
    
    之后就可以正常切换分支了,当前的修改内容只是保存并且隐藏起来
    
    回复到之前的工作状态
    git stash pop
    
    这样就可以愉快的继续编写当时被中断的代码了
    
    当然,还有好多命令,可以自己摸索
    
    git stash --help
    
    usage: git stash list [<options>]
       or: git stash show [<stash>]
       or: git stash drop [-q|--quiet] [<stash>]
       or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
       or: git stash branch <branchname> [<stash>]
       or: git stash [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
    		       [-u|--include-untracked] [-a|--all] [<message>]]
       or: git stash clear
    
    
    
    

    远程地址切换


    该方法保留所有的commit记录。如果不需要保留,直接新建项目copy代码好了 ,这样push的代码大小会小很多,因为所有的操作历史记录都没有了。
    // 查看远程地址
    git remote -v 
    // 更换远程地址, 新建一个项目不添加任何文件  在本地直接push即可
    git remote set-url origin https://git.oschina.net/HaiShengHuo/xxx.git
    
    
    

    查看某次提交修改的具体文件


    方法一

    git reflog 列出最新修改记录 也可以 git log --oneline
    git log 00788a04 --name-status 找到对应版本号 执行

    commit 00788a04f8d6dd834723c3479a0b5cdcfad8694a
    Author: ZhiChao <13424490552@163.com>
    Date:   Thu Mar 30 11:04:46 2017 +0800
    
        最后一条消息显示来源人
    
    M       yjtim/IM/Chat/Controller/ConversationListController.m
    
    commit a4b9129f1728dc10d5f6ab8c77c1b20ef7bb3d12
    Author: ZhiChao <13424490552@163.com>
    Date:   Wed Mar 29 17:49:44 2017 +0800
    
        隐藏被入群弹框
    
    M       yjtim/IM/Chat/Controller/ChatDemoHelper.m
    
    commit 1910008369d4ab264e7610997135a54d27fe22c7
    Author: ZhiChao <13424490552@163.com>
    Date:   Wed Mar 29 17:40:56 2017 +0800
    
        99+
    
    M       yjtim/IM/EaseUI/EMUIKit/Views/conversation/toolbar/EaseImageView.m
    
    
    

    git diff HEAD@{79} HEAD@{78} 具体某次修改的内容(具体某次内容和上次的内容进行比较)
    git diff a4b9129f 00788a04 当然也可以使用版本号

    这样假如之前修改过具体某些内容,以后还需要修改的话,找起来真的很方便.好比某些bug好久之后才发现,又要回头去修改,而修改总要找到对应的代码进行修改吧,这样几行命令就定位到具体文件和位置了,方便多了,节省很多时间.找代码有技巧,但是"找"终究还是很浪费时间.这也告诉我们,commit 提交命令很重要,需要认真写,不可为了省事而乱写

    
    diff --git a/yjtim/IM/Chat/Controller/ConversationListController.m b/yjtim/IM/Chat/Controller/ConversationListController.m
    index df0555e5..71822638 100644
    --- a/yjtim/IM/Chat/Controller/ConversationListController.m
    +++ b/yjtim/IM/Chat/Controller/ConversationListController.m
    @@ -347,7 +347,7 @@ - (NSAttributedString *)conversationListViewController:(EaseConversationListView
                 [attributedStr setAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1.0 green:.0 blue:.0 alpha:0.5]} range:NSMakeRange(0, NSLocalizedString(@"group.atMe", @"[Somebody @ me]").length)];
             }
             else {
    -            attributedStr = [[NSMutableAttributedString alloc] initWithString:latestMessageTitle];
    +            attributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@:%@",[[YJTAddressList shareAddressList].userNameDict objectForKey:lastMessage.from],latestMessageTitle]];
             }
         }
         
    
    方法二

    直接使用git log 6b81a31033 -p 或则 git log -p 6b81a31033 其中6b81a31033为版本哈希值

    commit 6b81a31033074ef279049e9cbf5944e3158d7510
    Author: ZhiChao <13424490552@163.com>
    Date:   Fri Apr 14 13:57:22 2017 +0800
    
        综合评价fixbug
    
    diff --git a/yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaDetailVC.m b/yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaDetailVC.m
    index 10ff4b31..cef73191 100644
    --- a/yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaDetailVC.m
    +++ b/yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaDetailVC.m
    @@ -28,7 +28,7 @@ @interface YJTEvaDetailVC ()<YJTEvaDateSelectVCDelegate>
     @property (nonatomic, strong) NSArray *catListArray;
     @property (nonatomic, strong) YJTEvaSchoolYearModel *evaSchoolYearModel;
     
    -
    +@property (nonatomic, copy) NSString *formId;
      @property (nonatomic, copy) NSString *className;
      @property (nonatomic, copy) NSString *classId;
      @property (nonatomic, copy) NSString *tempClassId;
      @@ -262,6 +262,7 @@ - (void)rightBtnClick {
         editVC.evaListModel = self.evaListModel;
         editVC.catListArray = self.catListArray;
    ...
    
    方法三

    git log --stat 6b81a31033或者git log 6b81a31033 --stat 查看简洁文件变化

    commit 6b81a31033074ef279049e9cbf5944e3158d7510
    Author: ZhiChao <13424490552@163.com>
    Date:   Fri Apr 14 13:57:22 2017 +0800
    
        综合评价fixbug
    
     yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaDetailVC.m | 5 +++--
     yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaEditVC.h   | 1 +
     yjtim/Sections/Apps/NewEvaluation/Controller/YJTEvaEditVC.m   | 2 +-
     3 files changed, 5 insertions(+), 3 deletions(-)
    

    忽略跟踪


    git checkout .   清空所有更改
    以下命令是我们在项目中已经添加了.gitignore  但是中途突然不想再跟踪某文件
    此时发现简单的在.gitignore文件中添加要忽略的文件是不起效的,因为该文件已
    经被track,我们还需要将其状态改为 未track(其实只需删除暂缓区文件然后将操作体检即可)
    git rm --cached Podfile.lock  将Podfile.lock从暂缓区删除,不再跟踪
    

    备份


    git tag -a WeChat1.0 -m "version 1.0" :给版本打上标签
    git tag : 查看所有的标签
    git push origin WeChat1.0 : 将WeChat1.0 push 到默认分支
    
  • 相关阅读:
    macOS Big Sur 关闭 SIP 正确方法
    绕过CDN寻找真实IP的8种方法
    mycat 链接mysql提示ERROR 3009 (HY000): java.lang.IllegalArgumentExceptio解决方式
    【SpringBoot】SpringBoot 内嵌容器及性能
    【Java】基于NIO的聊天室
    【Java】基于BIO的聊天室
    【ZooKeeper】ZooKeeper源码编译
    【SpringBoot】 使用外部Tomcat方法及启动原理
    【Java】深入理解SPI机制
    【SpringBoot】SpringBoot 自动配置原理
  • 原文地址:https://www.cnblogs.com/chao8888/p/6383334.html
Copyright © 2020-2023  润新知