zoukankan      html  css  js  c++  java
  • git常用指令

    一、参考资料:
        Git初步安装与使用: http://blog.jobbole.com/78960/
        Git与Repo入门: http://www.cnblogs.com/angeldevil/p/3238470.html
       
    二、常用指令汇总:

    ssh-keygen -t rsa –C “446065469@qq.com”   //生成秘钥,需要id_rsa.pub内容添加到git服务器

    git config -global user.name "sheldon_li"               //配置当前用户名
    git config -global user.email "446065469@qq.com"    //配置当前用户邮箱

    git clone <url>    //从服务器下载源码,如:git clone https://github.com/dragonforgithub/git_cmd.git
     
    git status            //查看当前目录文件的状态,如:新建、修改、删除等
    git diff <file>     //查看文件的修改内容,可以配合更详细的参数制作patch文件
     
    git init           //创建本地仓库
    git add .          //暂存新建的文件,注:.代表当前整个目录及文件,也可以指定添加单一文件
    git commit -a   //跳过暂存区直接提交修改的文件(不加-a则需要先add新建的文件再commit)
    git commit -m "first commit"     //添加提交信息,注明此次修改的内容
    git commit --m "first commit"     //添加提交信息,注明此次修改的内容
    git commit --amend --author "sheldon <dragon1992long@163.com>" //--amend用于在同一个commit id上提交
    注:可以通过 -am 参数提交新建/修改的文件同时注明提交信息
     
    git log                       //查看提交记录(完整显示提交号、作者、时间及说明等)
    git log --pretty=oneline  //查看提交记录(仅一行显示提交号和说明)
    git log --graph --pretty=oneline --abbrev-commit  //查看提交记录(包含点线图:{*表示一个commit}、{|表示分支前进}、{/表示分叉}、{表示合入})
    git reflog          //查看历史操作,在丢失提交记录的情况下很有用
     
    git reset --hard HEAD~N   //回退到倒数第N+1个提交
    git reset --hard 6adbcd8   //回退到该提交号的版本
     
    git branch       //查看当前所有分支
    git branch  test        //创建一个名为test的分支
    git checkout -b test //创建一个名为test的分支并切换过去
    git branch -d test    //删除test分支
    git checkout <branch-name> | <commit id>   // 切换到指定的分支 | 切换到某一版提交 是同一个指令(git checkout)
    git merge test    //在master主干上合并test分支修改内容(如果有冲突需要再进一步处理差异)
    git branch --set-upstream test origin/test  //指定本地test分支与远程origin/test分支的链接
     
    git remote add origin https://github.com/dragonforgithub/git_cmd.git  //添加远程仓库(Git服务器建立对应的工程)
    git remote -v    //查看远程仓库信息
    git remote remove <name> //删除远程仓库
    git push origin master       //将本地仓库推送到服务器,后续提交的修改同样如此推送到服务器
    git pull origin master        //将服务最新版本同步到本地仓库
     
    git stash       //把当前的工作隐藏起来 等以后恢复现场后继续工作(比如临时想基于原始版本做一个其他修改尝试,可以暂时缓存当前修改的内容)
    git stash list       //查看所有被隐藏的内容
    git stash apply   //恢复被隐藏内容,但是不删除
    git stash drop    //删除隐藏内容
    git stash pop     //恢复的同时也删除隐藏内容
     
    多人协作工作模式一般是这样的:
    1.首先,可以试图用git push origin "branch-name"推送自己的修改;
    2.如果推送失败,则因为远程分支比你的本地更新早,需要先用git pull试图合并;
    3.如果合并有冲突,则需要解决冲突,并在本地提交。再用git push origin branch-name推送。

    三、使用git生成patch和打patch:

    (1) git format-patch
    git format-patch HEAD^            #生成最近的1次commit的patch
    git format-patch HEAD^^           #生成最近的2次commit的patch
    git format-patch HEAD^^^          #生成最近的3次commit的patch
    git format-patch HEAD^^^^          #生成最近的4次commit的patch
    git format-patch <r1>..<r2>                    #生成两个commit间的修改的patch(包含两个commit. <r1>和<r2>都是具体的commit号)
    git format-patch -1 <r1>                       #生成单个commit的patch
    git format-patch <r1>                           #生成某commit以来的修改patch(不包含该commit)
    git format-patch --root <r1>        #生成从根到r1提交的所有patch

    (2) git am
    git am 0001-limit-log-function.patch                                # 将名字为0001-limit-log-function.patch的patch打上
    git am --signoff 0001-limit-log-function.patch                  # 添加-s或者--signoff,还可以把自己的名字添加为signed off by 信息注明打patch的人是谁
    git am ~/patch-set/*.patch              # 将路径~/patch-set/*.patch 按照先后顺序打上
    git am --abort                                                                  # 当git am失败时,用以将已经在am过程中打上的patch废弃掉(比如有三个patch,打到第三个patch时有冲突,那么这条命令会把打上的前两个patch丢弃掉,返回没有打patch的状态)
    git am --resolved                                                             # 当git am失败,解决完冲突后,这条命令会接着打patch

     (3) git apply
    git apply --stat 0001-limit-log-function.patch         # 查看patch的情况
    git apply --check 0001-limit-log-function.patch        # 检查patch是否能够打上,如果没有任何输出,则说明无冲突
    (注:git apply是另外一种打patch的命令,其与git am的区别是,git apply并不会将commit message等打上去,而git am会直接将patch的所有信息打上去,author是生成patch的人)

    解决patch冲突方法 :
    方案一(稳):
    (1) 根据git am失败的信息,找到发生冲突的具体patch文件,然后用命令git apply --reject <patch_name>,强行打这个patch,发生冲突的部分会保存为.rej文件(例如发生冲突的文件是a.txt,则发生conflict的部分会保存为a.txt.rej)
    (2) 根据.rej文件,通过编辑该patch文件的方式解决冲突
    (3) 废弃上一条am命令已经打了的patch:git am --abort
    (4) 重新打patch:git am ~/patch-set/*.patch

    方案二(快):
    (1) 根据git am失败的信息,找到发生冲突的具体patch文件,然后用命令git apply --reject <patch_name>,强行打这个patch,发生冲突的部分会保存为.rej文件(例如发生冲突的文件是a.txt,则发生conflict的部分会保存为a.txt.rej)
    (2) 根据.rej文件,通过编辑发生冲突的code文件的方式解决冲突
    (3) 将该patch涉及到的所有文件(不仅仅是发生冲突的文件)通过命令git add <file_name>添加到工作区中
    (4) 告诉git冲突已经解决,继续打patch: git am --resolved (git am --resolved 和 git am --continue是一样的)

  • 相关阅读:
    [LeetCode] 617. Merge Two Binary Trees
    [LeetCode] 738. Monotone Increasing Digits
    289. Game of Life
    305. Number of Islands II
    288. Unique Word Abbreviation
    271. Encode and Decode Strings
    393. UTF-8 Validation
    317. Shortest Distance from All Buildings
    286. Walls and Gates
    296. Best Meeting Point
  • 原文地址:https://www.cnblogs.com/blogs-of-lxl/p/10613267.html
Copyright © 2011-2022 走看看