zoukankan      html  css  js  c++  java
  • Git 命令收集

    记录一些比较特别的命令:

    1.清理恢复

    git clean -xdf
    

    清理所有未untracked的文件和文件夹

    git reset --hard
    

    恢复所有已tracked的文件和文件夹

    将这两个合并起来就是恢复到旧的干净的版本。

    删除所有未untracked的文件和文件夹,但是不删除在.gitignore中的文件或文件夹

    git clean -df
    

    删除之前不知道删除什么文件,可以加-n,比如:

    git clean -ndf
    

    2.回滚,reset与revert的区别

    git reset --hard 4971ebc
    

    git reset回滚到某个版本,只是移动指针,可以向前,也可以向后。


    git revert就不一样,它是创建一个新的commit,来覆盖旧的commit,指针只往后移动

    λ git revert head
    [master 67d4613] Revert "delete abc.txt"
     2 files changed, 8 insertions(+)
     create mode 100644 ABc.txt
     create mode 100644 abc.txt
    

    回滚到指定版本:

    λ git revert 5ccc4db
    [master 9149046] Revert "删除大小写的诡异"
     4 files changed, 32 insertions(+)
     create mode 100644 File1.txt
     create mode 100644 Readme.txt
    

    查看两次revert的日志记录:

    * 9149046 - (HEAD -> master) Revert "删除大小写的诡异" (41 seconds ago) | hongdada
    * 67d4613 - Revert "delete abc.txt" (7 minutes ago) | hongdada
    * 906417c - (origin/master) delete abc.txt (14 minutes ago) | hongdada
    * 5ccc4db - 删除大小写的诡异 (14 minutes ago) | hongdada
    

    所以一般在工作中,如果线上代码发生冲突,要用git revert来回滚,并签入到远程服务器,

    因为用git reset,在签入远程服务器时会发现全是冲突,因为远程服务器里面最近的commitid在本地不存在。

    如果实在有把握,可以git reset回到某个版本,并强制推送远程服务器

    git push -f
    

    3.merge,rebase,cherry-pick区别

    git merge --squash dev 
    git cm "..."
    

    merge --squash 是合并dev分支中的commit,再添加描述提交

    git rebase dev
    git rebase --continue
    git rebase --abort
    git rebae --skip
    

    在rebase的过程中,也许会出现冲突(conflict). 在这种情况,Git会停止rebase并会让你去解决冲突;

    在解决完冲突后,用"git-add"命令去更新这些内容的索引(index), 然后,你无需执行 git-commit,只要执行 git rebase --continue来继续

    或者根本不解决冲突,调用git rebase --skip 将引起冲突的commits丢弃掉

    这样git会继续应用(apply)余下的补丁。在任何时候,你可以用git rebase --abort参数来终止rebase的行动。

    git cherry-pick  commitid
    

    cherry-pick可以对整个项目中的某个commitid进行合并,不仅可以用在不同分支之间, 还可以用在同一个分支上,不同分支的用法如上所述. 同一分支用法也是一样的, 同一分支使用情形:

    比如说你在某一个向某个分支中添加了一个功能, 后来处于某种原因把它给删除了_,_然而后来某一天你又要添加上这个功能了, 这时候就可以使用cherry-pick把添加那个功能的commit

    4.删除不存在对应远程分支的本地分支

    D:Developmentlpapi.rongzi.comn (master)
    λ git b -a
      developer
      hongqi * master
      remotes/origin/HEAD -> origin/master
      remotes/origin/developer
      remotes/origin/hongqi
      remotes/origin/master
      remotes/origin/newdev
    
    D:Developmentlpapi.rongzi.comn (master)
    λ git remote prune origin newdev
    Pruning origin
    URL: http://10.40.3.31/rzr.net/lpapi.rongzi.com.git
     * [pruned] origin/newdev
    fatal: 'newdev' does not appear to be a git repository
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    远程已不存在分支newdev,但是本地还是存在,所以删除本地对应的远程分支

    具体的操作:

    D:DevelopmentRongzi.RZR.M (master)                                               
    λ git b -a                                                                         
      bugfix                                                                           
      hongqi                                                                           
    * master                                                                           
      remotes/origin/Develper                                                          
      remotes/origin/HEAD -> origin/master                                             
      remotes/origin/bugfix                                                            
      remotes/origin/develop                                                           
      remotes/origin/developer                                                         
      remotes/origin/feature                                                           
      remotes/origin/hongqi                                                            
      remotes/origin/master                                                            
                                                                                       
    D:DevelopmentRongzi.RZR.M (master)                                               
    λ git remote show origin                                                           
    * remote origin                                                                    
      Fetch URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                            
      Push  URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                            
      HEAD branch: master                                                              
      Remote branches:                                                                 
        Develper                    tracked                                            
        developer                   tracked                                            
        feature                     tracked                                            
        master                      tracked                                            
        refs/remotes/origin/bugfix  stale (use 'git remote prune' to remove)           
        refs/remotes/origin/develop stale (use 'git remote prune' to remove)           
        refs/remotes/origin/hongqi  stale (use 'git remote prune' to remove)           
      Local branches configured for 'git pull':                                        
        hongqi merges with remote hongqi                                               
        master merges with remote master                                               
      Local ref configured for 'git push':                                             
        master pushes to master (up to date)                                           
                                                                                       
    D:DevelopmentRongzi.RZR.M (master)                                               
    λ git remote prune origin                                                          
    Pruning origin                                                                     
    URL: http://10.40.3.31/rzr.net/Rongzi.RZR.M.git                                    
     * [pruned] origin/bugfix                                                          
     * [pruned] origin/develop                                                         
     * [pruned] origin/hongqi                                                          
                                                                                       
    D:DevelopmentRongzi.RZR.M (master)                                               
    λ git b -a                                                                         
      bugfix                                                                           
      hongqi                                                                           
    * master                                                                           
      remotes/origin/Develper                                                          
      remotes/origin/HEAD -> origin/master                                             
      remotes/origin/developer                                                         
      remotes/origin/feature                                                           
      remotes/origin/master
    

    5.git pull,git push 报错(本地分支没有对应的远程分支)

    D:DevelopmentRongzi.RZR.MI (develop)
    $ git pull
    There is no tracking information for the current branch.
    Please specify which branch you want to merge with.
    See git-pull(1) for details.
    
        git pull <remote> <branch> If you wish to set tracking information for this branch you can do so with:
    
        git branch --set-upstream-to=origin/<branch> develop
    
    D:DevelopmentRongzi.RZR.MI (develop)
    $ git branch -vv * develop                  bd19efd fengsheng
      feature/RZRAPP           427ea9c [origin/feature/RZRAPP] Merge branch 'develop'
      feature/ThirdCooperation ebbe5e8 [origin/feature/ThirdCooperation] 金额单位修改
      hongqi                   cbb9edd [origin/hongqi: gone] modify mojie channelId
      master                   39a624f [origin/master: behind 1] fix bug“
    
    

    使用git branch -vv 可以查看本地分支和远程分支的关联关系

    使用命令 git branch --set-upstream-to=origin/远程分支的名字 本地分支的名字

    D:DevelopmentRongzi.RZR.MI (develop)
    $ git branch --set-upstream-to=origin/develop develop
    Branch develop set up to track remote branch develop from origin.
    
    D:DevelopmentRongzi.RZR.MI (develop)
    $ git pull
    Already up-to-date.
    
    D:DevelopmentRongzi.RZR.MI (develop)
    $ git branch -vv * develop                  bd19efd [origin/develop] fengsheng
      feature/RZRAPP           427ea9c [origin/feature/RZRAPP] Merge branch 'develop'
      feature/ThirdCooperation ebbe5e8 [origin/feature/ThirdCooperation] 金额单位修改
      hongqi                   cbb9edd [origin/hongqi: gone] modify mojie channelId
      master                   39a624f [origin/master: behind 1] fix bug“
    

    也可以使用简化版本

    git branch --set-upstream-to=origin/develop develop
    
    (简化版-此命令的含义是,是指当前分支的upstream为origin远程仓库的develop分支)
    git b -u origin/develop
    

    6.对远程分支的操作:

    在远程生成本地分支对应的远程分支

    $ git push -u origin feat
    $ git push -u origin feat:feat
    
    $ git b -u origin/feat
    $ git b -u origin/feat feat 
    (注意这里为b,而且名称必须origin/远程名称)
    

    git b.... :当前分支的upstream为origin远程仓库的dev分支,(必须已经存在对应的远程分支)

    git push....:推送feat分支到远程origin仓库feat分支,并且建立本地分支feat的upstream为origin/feat(远程可不存在,如果存在,要看commit版本,可能会有冲突)

    删除:(冒号方式的删除,原理是,冒号左边应该为本地分支,右边为远程分支,现在推送了一个空的本地分支到远程分支)

    删除远程dev分支,分别是2种不同的命令
    $ git push origin :dev
    $ git push origin -d dev
    
    $ git push origin --delete dev
    

    取消upstream:

    取消当前分支的upstream
    $ git branch --unset-upstream
    取消其他分支的upstream
    $ git branch --unset-upstream [分支名]
    

    7.模糊匹配:

    查看某文件修改

    git diff *House*
    

    撤销某文件修改

    git co *House*
    

    8.error: src refspec hongqi does not match any.

    出现上述问题时,执行下面的命令进行推送,一般是因为本地分支名称与远程分支名称不一致导致。

    $ git push origin HEAD:hongqi
    

    9.取消某次合并

    git merge --abort #如果Git版本 >= 1.7.4
    git reset --merge #如果Git版本 >= 1.6.1
    

    **10.暂存命令stash使用 **

    git stash #将本地修改暂时存储起来
    git stash list #查看暂存的信息
    git stash pop  #应用最近一次暂存的内容
    git stash apply stash@{1} #应用指定版本的暂存内容
    git stash clear  #清空暂存栈
    

    11.git revert撤销merge

    正常方式revert merge的commitid:

    $ git revert 98ddcac
    error: commit 98ddcace161b532103597f5eaa4b9ac8b873ca1c is a merge but no -m option was given.
    fatal: revert failed
    

    log:

    *   g - Merge branch 'dev' (30 minutes ago) | hongda
    |
    | * e - (dev) 3333 (45 minutes ago) | hongda
    | * d - 1111 (45 minutes ago) | hongda
    * | f - 4444 (52 seconds ago) | hongda
    |/
    * c - (origin/master, origin/HEAD) 取消注释 (1 year, 3 months ago) | hongda
    * b - 调整后台调用的接口,修改分页 (1 year, 3 months ago) | hongda
    * a - 修改 (1 year, 3 months ago) | hongdada
    

    可以看到它merge是两个commitid,分别是它整个提交的commitid的起始

    a -> b -> c -> f -- g -> h (master)
                     /
                d -> e  (dev)
    

    上图中,g 是 merge commit,其他的都是常规 commit。g 的两个 parent 分别是 fe

    在你合并两个分支并试图撤销时,Git 并不知道你到底需要保留哪一个分支上所做的修改。从 Git 的角度来看,master 分支和 dev 在地位上是完全平等的,只是在 workflow 中,master 被人为约定成了「主分支」。

    于是 Git 需要你通过 mmainline 参数来指定「主线」。merge commit 的 parents 一定是在两个不同的线索上,因此可以通过 parent 来表示「主线」。m 参数的值可以是 1 或者 2,对应着 parent 在 merge commit 信息中的顺序。

    $ git show 98ddcac
    commit 98ddcace161b532103597f5eaa4b9ac8b873ca1c
    Merge: d9090a1 f032a1b
    Author: hongda <hongda159505@qq.com>
    Date:   Wed Dec 5 16:39:23 2018 +0800
    
        Merge branch 'dev'
    

    那么,$ git revert g -m 1 将会保留 master 分支上的修改,撤销 dev 分支上的修改。

    12.git pull冲突

    λ git pull
    warning: Pulling without specifying how to reconcile divergent branches is
    discouraged. You can squelch this message by running one of the following
    commands sometime before your next pull:
    
      git config pull.rebase false  # merge (the default strategy)
      git config pull.rebase true   # rebase
      git config pull.ff only       # fast-forward only
    
    You can replace "git config" with "git config --global" to set a default
    preference for all repositories. You can also pass --rebase, --no-rebase,
    or --ff-only on the command line to override the configured default per
    invocation.
    
    Already up to date.
    

    该问题就是,我本地有commit,但是未提交,origin也有新的commit,并且冲突

    如果这里使用rebase为true,就在pull的时候一步步合并代码,并生成新的commit再提交

    目前还是使用merge比较好,全局设置

    git config --global pull.rebase false
    

    查看:

    λ git config --global --list
    user.name=hongda
    user.email=hongda159505@qq.com
    alias.last=log -1 --stat
    。。。。
    pull.rebase=false
    

    13.git换行符问题

    git add .
    fatal: CRLF would be replaced by LF ...
    

    文本文件所使用的换行符,在不同的系统平台上是不一样的

    UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是 0x0D(CR),后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0A(CRLF) 作为换行符。

    git提供了一个 autocrlf 的配置项,用于在提交和检出时自动转换换行符,该配置有三个可选项:

    • true: 提交时转换为 LF,检出时转换为 CRLF
    • false: 提交检出均不转换
    • input: 提交时转换为LF,检出时不转换

    用如下命令即可完成配置:

    # 提交时转换为LF,检出时转换为CRLF
    git config --global core.autocrlf true
    
    # 提交时转换为LF,检出时不转换
    git config --global core.autocrlf input
    
    # 提交检出均不转换
    git config --global core.autocrlf false
    

    如果把 autocrlf 设置为 false 时,那另一个配置项 safecrlf 最好设置为 ture。该选项用于检查文件是否包含混合换行符,其有三个可选项:

    • true: 拒绝提交包含混合换行符的文件
    • false: 允许提交包含混合换行符的文件
    • warn: 提交包含混合换行符的文件时给出警告

    配置方法:

    # 拒绝提交包含混合换行符的文件
    git config --global core.safecrlf true
    
    # 允许提交包含混合换行符的文件
    git config --global core.safecrlf false
    
    # 提交包含混合换行符的文件时给出警告
    git config --global core.safecrlf warn
    

    目前采取的方法是,idea中修改换行符为LF,并git全局设置:

    $ git config --global core.autocrlf false
    $ git config --global core.safecrlf false
    

    这样设置最宽松。

    参考:

    10 个迅速提升你 Git 水平的提示

    git技巧:删除在本地有但在远程库中已经不存在的分支

    关于 Git 你所不知道的一些事

    Git查看、删除、重命名远程分支和tag

    Git远程操作详解

    Git 撤销合并

    Git 多平台换行符问题(LF or CRLF)

  • 相关阅读:
    Autofac官方文档翻译--二、解析服务--2隐式关系类型
    Verdi 不加载filelist,load design方法
    Power-Aware GateSim Debug
    simulation vs emulation
    关于SMI、MSI、SCI、INTx各种中断小结【转】
    zgrep用法
    sometimes we should use "disable fork" instead of "disable block_name"
    How to view assertions in the Verdi waveform viewer
    FIFO设计中的深度计算【zz】
    写写我的硕士三年【zz】
  • 原文地址:https://www.cnblogs.com/hongdada/p/9549506.html
Copyright © 2011-2022 走看看