git workflow常用命令

  1. git init
  2. git status
  3. git add readme.txt
  4. git add --all         Adds all new or modified files
  5. git commit -m"message"git workflow常用命令_第1张图片
  6. git add '*.txt'    Add all txt file in the whole project
  7. git remote add originName https://github.com/try-git/try_git.git       :告诉git,新增一个类似于bookmark的信息,以便后面push分享
  8. git remote rm originName :删除originName这个remote
  9. git remote -v 列出所有已知的remote列表,remote代表了另外一个repo, 比如下图,mary和我们的repo之间分别建立了两个remote: mary,origin(clone时自动添加)git workflow常用命令_第2张图片
  10. git remote update(或者git fetch)执行该命令后,你的repo和remote repo通信,获取相关commit,放到你的orgin/master,origin/xxx的remote tracking branch上。随后通过git status -uno命令来检查你的local branch和对应的remote tracking branch是否有commit需要加进来。或者另外一种方法可以简单check一下是否localbranch需要更新: git remote show origin 这时,它会和remote 联络看一下是否up-to-date
    [cabox@ githubtest]$ git remote update                                                                                                                     
    Fetching origin                                                                                                                                                            
    Password:                                                                                                                                                                  
    remote: Counting objects: 17, done.                                                                                                                                        
    remote: Compressing objects: 100% (10/10), done.                                                                                                                           
    remote: Total 17 (delta 4), reused 16 (delta 3), pack-reused 0                                                                                                             
    Unpacking objects: 100% (17/17), done.                                                                                                                                     
    From https://github.com/cnweibo/githubtest                                                                                                                                 
       0a2f95b..b0c8d59  kidsitbranch -> origin/kidsitbranch                                                                                                                   
       0a2f95b..4784daf  master     -> origin/master                                                                                                                           
    [cabox@box-codeanywhere githubtest]$ git status -uno                                                                                                                       
    # On branch master                                                                                                                                                         
    # Your branch is behind 'origin/master' by 5 commits, and can be fast-forwarded.                                                                                           
    #                                                                                                                                                                          
    nothing to commit (use -u to show untracked files) 
  11. git revert 134535 将通过重新生成一个commit,撤销134535这个commit的所有改动,注意仅仅是撤销这一个commit,历史信息里面并不会删除这个commit,之后的commit是不受影响的哦
  12. git reset --hard, git clean -f : 注意git reset和git clean操作影响的都是working directory,并不会影响commited snapshots。而git reset却会永久性的undo changesgit workflow常用命令_第3张图片
  13. git reset --hard/git checkout anotherbranch  : reset --hard通过修改本分支所指向的commit来间接修改HEAD指针;而checkout只是修改HEAD,并不会修改分支的指向
  14. git branch -f branch-name new-tip-commit  :在你reset多次后,你将进入detached mode,你希望将你的branch回退到之前的某个commit,则可以使用这条命令,但是注意你还需要 git checkout branch-name
  15. git branch -a 列出所有的branch,这时可能包含以下两个
    * master                                                                                                                                                                   
      remotes/origin/master     这个branch就代表了其中的一个origin/master
  16. git branch -v :列出所有branch以及在该branch上的最近一个commit
      kidsitbranch 737efba [ahead 3] 3rd change                                                                                                                                
    * master       1030dba [ahead 4] go
  17. git push -u originName master  (-u tells Git to remember the parameters(设置master这个branch的default upstream为originName), so that next time we can simply run git push and Git will know what to do.)。push到remote的repository以便分享给他人(通过pull操作)。 注意:如果有人同步做了更改,那么这个push将会失败,必需首先git pull(暗含两个操作:一是git fetch,二是从origin/master merge到master)随后再git push。由于这种情况下会在Log中增加merge的历史,会污染这个log,所以更好的方法是用rebase来才做
  18. 一键push到多个remote的方法: 1.git remote add all [email protected]:akrabat/projectname.git
    2. git remote set-url --add all ssh://example.com/path/to/projectname.git 这样all 这个remote就有了两个url可以来push了。该命令通常可以用于不同staging/production/dev环境的一键更新。注意:当push到不同的repo时,所有历史信息都会保留,但是branch本身只有push过去了,在被push的central repo上才会有该branch!!也就是说本地的branch只有主动push了才能被其他人分享和使用
  19. git push   : 可以在Push remote时指示git将本地branch push到remote上的另外一个branch name, 或者直接 '':remotebranchtobedeleted删除远程repo中的branch
    git push origin :testbranch  --删除server上的testbranch
    git push origin master:production  --将本地的master分支push到origin上并且更名为production branch
    git push origin :refs/tags/1.7.0  --删除origin所标示的远程repo中的1.7.0这个tag
     
  20. git pull originName master             :git pull操作时,会将所有历史信息拉下来,但是比如在develop上面的改动虽然历史信息可以看到,但是并未pull到origin/develop上,要想使用,必须git checkout develop, git pull才能包含后面的改动
  21. rsync -arv --exclude-from ".gitignore" ./ git@server:/path/repo/xxxx.git       提供了一种同步repo的简单方法,不用pull/push操作
  22. git diff         show unstaged differences since last commit 比较working copy和staged copy的区别
  23. git diff HEAD README: 比较README文件的working copy和committed HEAD copy
  24. git diff commit1 commit2 README: 比较README文件的commit1和commit2的区别
  25. git add octofamily/octodog.txt
  26. git diff --staged         view staged differences: 比较已经staged(index)区和repo里的最新commit snapshot做比较.和git diff --cached类似git workflow常用命令_第4张图片
  27. git reset octofamily/octodog.txt  cancel the staged octodog.txt file
  28. git reset HEAD LICENSE     Unstage staged file for LICENSE file
  29. git checkout -- octocat.txt           从index区域获取octocat.txt覆盖工作区git workflow常用命令_第5张图片
  30. git branch clean_up           create one branch to work on, 注意:当在一个branch中修改了文件,但是你又没有stage it,然后checkout到另外的branch,那么这个修改将依然在working copy中,你可以将这个修改comit到新的branch中。注意branch本身存在的原因和意义:a)当向你的项目增加一个功能时需要创建branch隔离和将来集成;如果你不能给出一个特别意义的名字的时候,不要创建branch
  31. git checkout 514fbe7 :注意当前是在master分支时,checkout历史commit,则出现如下情况:命令本身返回一条消息说:we are in a detached HEAD state and that the HEAD is now at 514fbe7. HEAD本身标示了当前checkout的snapshot。这意味着红色圆圈代表了Git的HEAD。HEAD往往存在于tip of a development branch.但是当我们checkout branch的前一个commit时,HEAD移动到了当前branch的中间。我们不能再说我们仍然在当前branch(master)上因为当前branch(tip点)比HEAD包含了更多的snapshot。这由git branch输出来反映:we are currently on no branch!git workflow常用命令_第6张图片git workflow常用命令_第7张图片git workflow常用命令_第8张图片git workflow常用命令_第9张图片git workflow常用命令_第10张图片
  32. 通常情况下HEAD总是refer to a named branch(比如:master),同时master branch又refers to a specific commit(也就是master的tip那个commit)(tag也指向特定的commit),这样HEAD也就曲线指向了master分支的tip commit。在这种情况下(master分支状态下),如果提交一个commit,master这个分支就将被更新,指向到新的tip commit,HEAD通过master间接指向这个新的tip commit。但是如果我们checkout tip commit之前的任何commit,则这时HEAD指向这个中间commit,而master仍然指向master的tip commit,HEAD就和master detached了。
  33. 如果git checkout master,则会更新HEAD指针,HEAD等于master,master就等于tip commit,所以HEAD就等于master的tip commit。而一旦对中间的一个commit checkout,则只更新HEAD为中间的commit(detached state), 而在这种情况下的任何commit都不会改变master,而只会改变HEAD。这时如果再切换到master:git checkout master,则上面的所有新改动就将无任何引用了,过一定周期,git的垃圾收集机制就会起作用,定期清除上面detached状态下的提交。
  34. git branch -m  master test    将master branch重命名为test branch
  35. git config remote.testing.push refs/heads/test:refs/heads/master  修改相关ref ,效果等于: git remote add testing xxxxx;git push testing test:master
  36. git config有三个级别, System:适合于全部用户,配置文件为/etc/gitconfig;Global:适合于单个用户,配置文件为~/.gitconfig;Local:适合于单个repo,配置文件.git/config,具体地:
  37. git config --global user.name "kids learding"
  38. git config --global user.email "[email protected]"  执行上述两条命令后,在~/.gitconfig文件中,就有了“
    [user]                                                                                                                                                                                   
            name = kids learding                                                                                                                                                                  
            email = [email protected]
  39. git config user.name;列出现在使用的user.name;git config user.email同样列出email
  40. git config --global color.ui true :使能终端的输出color
  41. git config --global push.default simple   :这项配置使得git在push时只将current branch push到github上
  42. git config --global pull.rebase true  :自动在pull时执行rebase操作而不是merge(也就是说:执行git fetch, git rebase origin/master,而不是git merge origin/master),这样的好处是清除没有太大意义的空merge commit
  43. git config --global rerere.enabled true   Reuse recorded resolution(ReReRe) :record all fixes to merge conflicts;Reuse them automatically when the same conflict happen. Particullay useful when cherry picking to multiple branches or constantly rebasing.
  44. git config --global alias.s "status -s" =>git s=git staus -s 
  45. git config --global alias.lg "log --oneline --decorate --all --graph" =>git lg= git log --oneline --decorate --all --graph
  46. git config --list 列出所有的git配置项目
  47. .gitignore 文件列出所有不用被git track的文件或者文件夹
  48. 另外一种情况是如果文件已经被tracked了,你又不希望再次被修改,那么你可以通过执行git update-index --assume-unchanged filename命令,使得git在你的这个repo中暂时对这个文件失忆,对该文件的修改不做跟踪。这个功能非常适合于配置文件的场景。其匹配的其他命令是:
    git update-index --no-assume-unchanged filename  :取消该功能   ;
    git ls-files -v|grep '^h'       :列出所有被暂时取消track的文件集合 : 注意这种情况下,只对你自己的repo有效,也就是说只在你本地repo有效,其他人clone repo后仍然是track的!!
    cabox@box-codeanywhere:~/workspace/gitfattest$ git update-index --assume-unchanged read.txt                                                                                              
    cabox@box-codeanywhere:~/workspace/gitfattest$ git ls-files -v|grep '^h'                                                                                                                 
    h read.txt                                                                                                                                                                               
    cabox@box-codeanywhere:~/workspace/gitfattest$ vi read.txt                                                                                                                               
    cabox@box-codeanywhere:~/workspace/gitfattest$ git status                                                                                                                                
    On branch master                                                                                                                                                                         
    Your branch is up-to-date with 'origin/master'.                                                                                             
    nothing to commit, working directory clean                                                                                                  
    cabox@box-codeanywhere:~/workspace/gitfattest$ git update-index --no-assume-unchanged read.txt                                             
    cabox@box-codeanywhere:~/workspace/gitfattest$ git status                                                                                   
    On branch master                                                                                                                            
    Your branch is up-to-date with 'origin/master'.                                                                                             
    Changes not staged for commit:                                                                                                              
      (use "git add ..." to update what will be committed)                                                                                
      (use "git checkout -- ..." to discard changes in working directory)                                                                      modified:   read.txt                                                                                                                   
    no changes added to commit (use "git add" and/or "git commit -a") 

     

  49. 相对于只对每个repo local修改update-index后才能生效Untracked功能的,还有一种方法即是对原生repo修改,被clone后也会生效的方法是
    git rm --cached file :该命令将取消掉对file的track,这种情况下.gitignore中定义的file就将会被GIT彻底忽略。但是存在的问题是:后面被clone的文件中将彻底消失这个file文件!
  50. git checkout clean_up     switch to the clean_up branch  :注意,当执行checkout branch命令时,实际上更新的是.git/HEAD文件,也就是整个git全局的HEAD,并且指向被checkout的branch指针(也就是最新一个commit),该文件内容此时设置为refs/heads/clean_up,也就是说git的HEAD是由clean_up这个branch来决定的,而refs/heads/clean_up文件实际上就是包含一个40字符的最新commit,这时候,git HEAD, clean_up(branch的指针)是一致的。如果git checkout中间的某一个版本的话,由于.git/HEAD文件内容会被更新为被checkout指定的那个commit,而不是refs/heads/clean_up了,所以和clean_up或者master都不一致了,因此成为detach状态。 
  51. git stash  :当修改过一些文件后,但是还没有认真测试,这时比如有个hotfix需要解决,那么就可以执行这条命令,将未完成的工作暂存起来,随后git checkout hotfix在该hotfix branch上解决问题,完后就可以回到stash的状态重新继续工作,执行git checkout master, git stash apply则取回先前被暂存的变更集.如果不想用那个放在git堆栈里面的stash的话,可以使用命令:git stash clear 来清除掉。 http://www.cppblog.com/deercoder/archive/2011/11/13/160007.html 这篇文章解释的不错。注意:git stash apply命令默认从stash stack中取出最新的stash应用到当前branch上,但是并不会删除stash stack的内容
  52. git stash save --patch 上述命令可以交互式地选择你要stash哪个文件
  53. git stash save --keep-index :只将unstaged的变更放到stash stack中
  54. git stash=git stash save ;git stash apply=git stash apply stash@{0};git stash drop=git stash drop stash@{0};git stash pop=git stash apply+git stash drop
  55. 默认情况下git stash只会stash已经track的文件,你如果希望将新建的文件(即untracked文件)也被stash起来的话,需要使用git stash -u/git stash --include-untracked命令
  56. git stash list --stat : 列出stash里面内容的梗概信息:改了哪个文件以及具体更改的内容。。
  57. git stash show stash@{0} ; git stash show --patch
  58. git stash save "add some feature which has not been finished":在创建stash时可以增加一个简要的说明。。
  59. git stash branch newbrachforthestash stash@{0}
  60. git stash clear
  61. Hook into Git’s Internals: HOOK是一个当Git在某一个事件发生时执行的脚本。比如只要对central-repo.git的master branch上执行完成了push操作,我们就自动部署到生产环境中去。在central-repo.git目录,打开hooks目录将post-update.sample重命名为post-update,这个post-update就将被git在post完成后自动执行.

    #!/bin/sh
    
    # Output a friendly message
    echo "Publishing master branch!" >&2
    
    # Remove the old `my-website` directory (if necessary)
    rm -rf ../my-website
    
    # Create a new `my-website` directory
    mkdir ../my-website
    
    # Archive the `master` branch   //注意所有git archive 支持的placeholder都在git help log under the --pretty-format section可以看到
    git archive master --format=tar --output=../my-website.tar
    
    # Uncompress the archive into the `my-website` directory
    tar -xf ../my-website.tar -C ../my-website
    
    exit 0
  62. git rm '*.txt' 
  63. 如何更改一个在git版本控制下的文件名?首先将文件重命名A->B,随后,git rm A;(stop tracking A)git add B;(start to tracking B)
  64. git commit -m "Remove all the cats" :注意当commit时,git将同步更改.git/refs/heads/cureentBranch currentBranch为当前branch名称,比如master,develop等。也就是说branch本身永远指向该分支的最后一个commitHEAD本身永远指向你checkout后工作的那个版本,当他们不一致时,就是一种所谓detach状态。
  65. git checkout master            switching to master branch
  66. git merge clean_up   merge master with clean_up branch modifications。注意:merge本身只对checkout的分支有效。在这里有一个知识点:    3-way merge:在crazy这个branch上要merge master,由于crazy和master历史有diverge(分散),所以为了merge这两个branch,则需要创建一个额外的merge commit作为两个branch之间的桥梁。而这个新的commit有两个parent commit。下面的图中从crazy分支的tip处有两个箭头分别指向crazy和master。就像这样的说法:这个commit从crazy和master两个分支来的。在merge后,crazy分支就能访问crazy以及master的历史。3-way merge本身的命名就来自:Git looks at the three commits to generate the final state of the merge.git workflow常用命令_第11张图片
  67. git merge feature --squash :在上面的merge中,由于有所谓3-way merge,这个实际上是一个空的commit,会污染history,一个可行的方法是使用--squash选项,这样将简化历史信息

  68. git branch --merged  : 展示在你当前branch上已经merge过的所有branch,一般来说这个列表的内容说明已经被涵盖在当前branch上,这个列表中的branch如无必要就可以删除了,因为所有工作已经包含
    $ git branch --merged
    iss53  =>说明iss53的fix已经包含,可以删除该branch了
    * master
    $ git branch --no-merged
    testing =>说明testing branch未合入,需要适当时候合入。如果这时你想删除,GIT是不允许你这么做的。如果需要强行删除,可以执行:
    $git branch -D testing
  69. git branch --no-merged :展示在当前branch上还没有merge过来的branch。这个列表中的branch你要注意适当时候做好merge以便包含他们的工作
  70. 需要注意的是上面的git branch --merged/no-merged命令只能列出那些通过git merge命令而被merge的命令,而对于git merge --squash好像并不能准确列出来:git log --graph --left-right --cherry-pick --oneline master...featureX :该命令列出在featurX上有哪些commit还没有合入进来。
    zhenghuz@CV0005366N0 /d/devenv/Code/gitplayground/mary (master)
    $ git log --graph --left-right --cherry-pick --oneline master...fmary
    
    > e1ad8b2 mary added again to test unmerged //这条也是最新还未merge过的
    > e474cbb mary added again //这条是最新还未merge过的 < 56490e2 merge fmary again < 20eb4ac squash fjohn 9/10 line merged to master < bc413ae solve merge conflicts for master and fmary < fd3c3a8 john on fjohn add 7th and 8th line < 30952a7 Merge branch 'fmary'

     上述命令貌似显示的不是很准确,最好使用下面的一条命令可以一次性列出所有没有被merge到master或者develop分支的commits:

    $ git rev-list --all --not master --no-merges | xargs -L1 git name-rev | grep -E '[0-9a-f]{40}'
    54119bb150f22d720a781adca2305d85c9de9190 develop
    c8bc641b087a24bfce7c9b3b2adadc9d308955e6 remotes/origin/develop

     

  71. git branch -d clean_up           delete the clean_up branch after finished work on that branch(注意必须在另外一个branch时才能删除一个branch)
  72. git push                 push all the work we finished to github 
  73. git push origin --delete featureBranchFinished  = git push origin :featureBranchFinished  删除已经完成的feature branch
  74. git checkout hotfix迅速解决生产环境中报的一个重要问题git workflow常用命令_第12张图片
  75. git log --stat 包含统计信息的方式来展示log   
  76. git log --author=cnweibo  列出所有属于特定用户的所有commits
  77. git log --oneline --grep="被搜索的字符串"  查找所有包括“被搜索的字符串”内容的commits 
  78. git log --follow -p filename :查找一个filename这个文件的所有更改历史
    vagrant@homestead:~/Code/kidsit (develop)$ git log --follow bower.json
    commit b494ada331f7ee042e103ab098077d1178ce2ac0
    Author: cnweibo 163.com>
    Date:   Tue Jul 28 04:22:34 2015 +0000
    
        add angular-timer component
    
    commit 1b613522447dd16a70bc693dc558eeab511cd725
    Author: 1372921435 <1372921435@qq.com>
    Date:   Sat Jul 18 09:36:20 2015 -0400
    
        intial non lib version

     

  79. git log --oneline:梗概的方式展示log
  80. git log --graph 以图形的方式展示log
  81. git log --pretty="%h,%cn,%cr"友好的方式来展示log,这些formating有如下形式: %H-Commit Hash, %ad-Author Date, %h:Abbreviated Commit Hash,%ar-Author Date, Relative, %T-Tree Hash,%cn-Committer Name,%t: Abbreviated Tree Hash, %P-Parent Hashes;%p-Abbreviated parent Hashes;%an-author name;%ae-author email,%ce-committer email,%cd-committer date,%cr-commiter date, relative,%s-subject(commit message)
  82. git log --online --graph --all --decorate 以图形化方式展示所有branch的comit变更历史,非常好用。但是注意:如果你在做一个feature时,从master上创建了feature分支,代码完成后merge到master上,这时,如果你要查看图形化branch、merge历史则能清晰展示
    *   9eb6e93 (HEAD, master) Merge branch 'feature'
    |\
    | * 523e2ac (feature) tres
    | * 6d3cc0f dos
    | * 1bc0b2e uno
    * | d39734b three
    * | 779d37b two
    * | facbcbf one
    |/
    * 58848f4 Initial commit.

    但是如果你delete了那个feature分支,则从此不会再展示这个分支了。

    *   9eb6e93 (HEAD, master) Merge branch 'feature'
    |\
    | * 523e2ac tres
    | * 6d3cc0f dos
    | * 1bc0b2e uno
    * | d39734b three
    * | 779d37b two
    * | facbcbf one
    |/
    * 58848f4 Initial commit.

    原因是在GIT中branch实际上只是一个对最后的commit引用的指针而已,它会随着这个branch上的一个一个commit而不断移动。也就是说一旦这个指针被删除了,那么就将没有内存保存这个branch信息了。那么如何能够找到branch历史呢?有以下几个建议:要么不要删除分支,因为分支本身并没有什么坏处,除非分支太多,屏幕都无法看了;要么在删除branch之前将那个commit做一下Tag, Tag本身实际上就是一个不能移动的branch,你甚至可以将Tag命名为你将删除的branch名称;或者使用GIT系统默认给的merge commit comments,因为在merge时,系统会自动加上是将哪个branch上的commits merge过来的,这样你就可以查看到是从哪个branch上来的。

  83. git log -n 1 :展示最近的一个commit
  84. git log old..new    展示所有在new branch上的commit但是又没有包含在old branch上的commits.我们甚至可以使用这个filtering语法来指示Git只列出在当前分支上的最近的3条commits: git log HEAD~3..HEAD --stat
  85. git log -p  打印出commit对应的变更
    $ git log -p
    commit f67b73e86513d841dd68dd38f4de7327045f620a
    Author: cnweibo 163.com>
    Date:   Mon Mar 28 11:21:25 2016 +0800
    
        remove unused tab inactive style
    
    diff --git a/style.less b/style.less
    index 6dd816a..1c42159 100644
    --- a/style.less
    +++ b/style.less
    @@ -609,7 +609,6 @@ input,textarea,select,button{
             &.inactive{
               color: #666666;
               background-color: @component-background;
    -          border-bottom: ;
               box-shadow: 1px 1px 0 #d4d3d3;
             }
           }
    
    commit 3c88937a145f115002340369216145c5b6d4360b
    Author: cnweibo 163.com>
    Date:   Sun Mar 27 17:17:27 2016 +0800
    
        test for archive
    
    diff --git a/LAST_COMMIT b/LAST_COMMIT
    index eb0be20..33416cc 100644
    --- a/LAST_COMMIT
    +++ b/LAST_COMMIT
    @@ -1 +1 @@
    -$Format:Last commit: %h by %aN at %cd%n%+w(76,6,9)%B$
    \ No newline at end of file
    +$Format:Last commit: %d : %h by %aN at %cd%n%+w(76,6,9)%B$
    \ No newline at end of file
    
    commit 48f8866f2ff43482f10345aa8b42ffea75c6c81f
    Author: cnweibo 163.com>
    Date:   Sun Mar 27 16:57:04 2016 +0800
    
        exclude json file in the archive
    
    diff --git a/.gitattributes b/.gitattributes
    index 79c4bc0..a8e7392 100644
    --- a/.gitattributes
    +++ b/.gitattributes
    @@ -6,6 +6,6 @@ gulp.config.js export-ignore
     style.less export-ignore
     .idea export-ignore
     .git export-ignore
    -.json export-ignore
    +*.json export-ignore
    
     LAST_COMMIT export-subst
    \ No newline at end of file
    
    commit 472e2479de544ba38ecea0fc6a43835bc0e4923e
    Author: cnweibo 163.com>
    Date:   Sun Mar 27 16:54:42 2016 +0800
    
        add archive.sh script for deployment
    
    diff --git a/.gitattributes b/.gitattributes
    index 75fbf24..79c4bc0 100644
    --- a/.gitattributes
    +++ b/.gitattributes
    @@ -6,4 +6,6 @@ gulp.config.js export-ignore
     style.less export-ignore
     .idea export-ignore
     .git export-ignore
    +.json export-ignore
    +
     LAST_COMMIT export-subst
    \ No newline at end of file
    diff --git a/archive.sh b/archive.sh
    new file mode 100644
    index 0000000..d4e3bd6
    --- /dev/null
    +++ b/archive.sh

     

  86. git show 和git diff-tree来检查一个commit所修改过的文件集概要 
    $ git show --pretty="format:" --name-only bd61ad98
    
    index.html
    javascript/application.js
    javascript/ie6.js
    
    $ git diff-tree --no-commit-id --name-only -r bd61ad98
    index.html
    javascript/application.js
    javascript/ie6.js
  87. git show 359d234 fileToInvestigate.php : 查看359d234这个commit中的fileToInvestigate.php文件的修改
    vagrant@homestead:~/Code/kidsit$ git show bea6234 app/Http/Controllers/Admin/AdminUsersController.php
    commit bea6234678079f7d43f3291dedac55e7766cc0f1
    Author: cnweibo 163.com>
    Date:   Mon Jul 20 09:40:16 2015 +0000
    
        admin user update user
    
    diff --git a/app/Http/Controllers/Admin/AdminUsersController.php b/app/Http/Controllers/Admin/AdminUsersController.php
    index 4334ad9..2a948aa 100644
    --- a/app/Http/Controllers/Admin/AdminUsersController.php
    +++ b/app/Http/Controllers/Admin/AdminUsersController.php
    @@ -11,7 +11,9 @@ use Kidsit\Role;
     use Kidsit\Permission;
     use Illuminate\Support\Facades\Lang;
     use Illuminate\Support\Facades\View;
    -
    +use Illuminate\Support\Facades\Validator;
    +use Illuminate\Support\Facades\Input;
    +use Illuminate\Support\Facades\Redirect;
     use Datatables;
     class AdminUsersController extends Controller
     {
    @@ -180,7 +182,12 @@ class AdminUsersController extends Controller
         public function postEdit($user)
         {
             // Validate the inputs
    -        $validator = Validator::make(Input::all(), $user->getUpdateRules());
    +        $validator = Validator::make(Input::all(), array(
    +            'username' => 'required|alpha_dash',

     

  88. git blame :这个命令允许你查询是哪一个commit,哪一个作者修改了一个文件的某个特定行,这个命令对于分析是谁在哪个commit中引入了一行变更是非常有好处的。
    vagrant@homestead:~/Code/kidsit (develop)$ git blame -w bower.json
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  1) {
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  2)   "name": "kidsit-fe",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  3)   "description": "kidsit front end vendor components",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  4)   "version": "0.0.1",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  5)   "license": "MIT",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  6)   "private": true,
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  7)   "dependencies": {
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  8)     "angular": "1.3.x",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400  9)     "angular-route": "1.3.x",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 10)     "angular-loader": "1.3.x",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 11)     "angular-mocks": "~1.3.x",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 12)     "angular-animate": "~1.3.3",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 13)     "jquery": "~2.1.1",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 14)     "jquery-colorbox": "~1.5.14",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 15)     "angular-bootstrap": "~0.12.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 16)     "jquery-color": "~2.1.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 17)     "bootstrap": "~3.3.1",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 18)     "angular-toastr": "~0.5.1",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 19)     "angular-busy": "~4.1.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 20)     "ng-pageslide": "~0.1.8",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 21)     "PACE": "https://github.com/HubSpot/pace.git#~1.0.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 22)     "highcharts": "http://code.highcharts.com/highcharts.js",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 23)     "highcharts-ng": "~0.0.7",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 24)     "underscore": "~1.7.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 25)     "angular-underscore": "~0.5.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 26)     "bootstrap-hover-dropdown": "~2.0.11",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 27)     "angular-filter": "~0.5.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 28)     "angular-validate-directive": "~0.1.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 29)     "angular-messages": "1.3.3",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 30)     "ui-router": "~0.2.13",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 31)     "html5shiv": "^3.7.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 32)     "respondJs": "~1.4.2",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 33)     "jquery-placeholder": "~2.1.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 34)     "greensock": "~1.17.0",
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 35)     "code-prettify": "*",
    b494ada3 (cnweibo    2015-07-28 04:22:34 +0000 36)     "bootstrap-wysihtml5": "*",
    b494ada3 (cnweibo    2015-07-28 04:22:34 +0000 37)     "datatables": "~1.10.7",
    b494ada3 (cnweibo    2015-07-28 04:22:34 +0000 38)     "angular-timer": "~1.3.3"
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 39)   }
    ^1b61352 (1372921435 2015-07-18 09:36:20 -0400 40) }

     

  89. git tag -a v1.2 9fceb02 -m "Message here"  该命令将在一个commit上打上tag,比如在上面的例子中由于bracn被删除,所以为了标示523e2ac这个commit是从featurexxx分支上merge到master后被删除的,那么就可以执行一条命令:git tag -a featurexxx 523e2ac -m "创建永久'branch' featureXXX"
  90. git show v1.2  列出v1.2这个tag的详细信息
    (master)$ git show v1.2                                                                                                                                                              
    commit 7bddbdc2a1f8d9c23205707e74455d74684e3031                                                                                                                                          
    Merge: c822db1 3f63840                                                                                                                                                                   
    Author: Taylor Otwell                                                                                                                                            
    Date:   Tue Mar 24 16:06:56 2015 -0500                                                                                                                                                   
                                                                                                                                                                                             
        Merge pull request #3318 from TheShiftExchange/patch-1                                                                                                                               
                                                                                                                                                                                             
        Update services.php 
  91. git describe --all commitID : 通过给定一个commit,可以列出和这个commit最近的一个tag(该tag一定是这个commit的祖先)
    (master)$ git describe --all e01c173                                                                                                                                                     
    tags/v5.0.0-1-ge01c173
  92. git describe --tags HEAD 则列出离HEAD最近的那个tag信息
    $ git describe --tags HEAD
    v1.0-1-g3c88937   //注意这里的意思是离HEAD最近的是前1个commit打了V1.0的tag!!
    
    Administrator@USER-20151001BU MINGW64 ~/devenvironment/Code/fedevbp (master)
    $ git lg
    * 3c88937 (HEAD -> master) test for archive
    * 48f8866 (tag: v1.0) exclude json file in the archive

     

  93. git checkout -b version2 v2.0.0 从一个tag的版本上创建分支(注意:分支和tag都是一个pointer而已)
  94. git commit -a -m "modify readme" 将add to stage和commit change合二为一(add changes from all tracked files: DOES NOT ADD NEW FILE(untracked)!!)
  95. git reset --soft HEAD^将最后的一个commit rollback到staging area,再做修正后,比如增加新的文件,修改已有文件,之后再做commit动作
  96. git add todo.txt; git commit --amend -m "forget the todo.txt in last commit, so amend it here" 这个--amend参数在修正刚刚commit的内容,比如添加新的忘掉commit的内容,和前一个commit合在一起形成一次commit,优化历史信息是非常有帮助的!
  97. git reset --hard HEAD^;git reset --hard HEAD^^ 撤销掉最后一个/两个commit
  98. 当你checkout一个commit或者一个tag时,你将进入所谓的detached HEAD模式。如果这时你在这种模式下新commit代码,以后当你checkout一个branch时你将永远不会在git log中看到你在detached模式下作的commit,而要找回来你就需要使用git reflog机制
  99. git reset .vs. git checkout : git checkout主要是对working directory操作, git reset主要是对index区进行操作。git checkout和git reset都可以传入commit和file两种参数git workflow常用命令_第13张图片
  100. git reflog是一种记录HEAD的移动和各个branch的references的机制
    cabox@box-codeanywhere:~/workspace/gitfattest$ git reflog                                                                                                                                    
    91fb73d HEAD@{0}: clone: from https://github.com/cnweibo/gitfattest.git  

    git reflog也列出所有你已经remove掉的commit。注意:git reflog对于branch也有记录

    cabox@box-codeanywhere:~/workspace/kidsit$ git reflog develop                                                                                                                                
    6ea08d0 develop@{0}: branch: Created from refs/remotes/origin/develop 

     

  101. undo undo 如果在上面的git reset --hard HEAD^命令执行后,git将会把head向前移动一个commit,这时如果你希望反悔,即:你又希望取消刚才的reset hard操作,怎么办呢?方法是使用git reflog,该命令列出所有的历史commit,对于已经被“删除”的commit,只要没有被git自动垃圾回收机制所删除(一般在一个月以上才会被git自动清除),那么都可以用这个方法来恢复。
    $ git reflog
    9fbb460 HEAD@{0}: reset: moving to HEAD^1
    ecfc3dd HEAD@{1}: reset: moving to HEAD^1
    c62a928 HEAD@{2}: commit: add yinbiao/show
    ecfc3dd HEAD@{3}: commit (merge): strange files
    9fbb460 HEAD@{4}: commit: some strange file deleted
    d7ba3b7 HEAD@{5}: commit: migrations added
    6d89fa2 HEAD@{6}: commit: custom.js
    456d2e7 HEAD@{7}: commit: add custom.js in resource
    cd4f2be HEAD@{8}: pull: Fast-forward
    8b72bf8 HEAD@{9}: commit: update gulpfile
    38afa34 HEAD@{10}: pull: Merge made by the 'recursive' strategy.
    4a22d6f HEAD@{11}: commit: add some trace script
    044456b HEAD@{12}: pull: Fast-forward
    4744f4a HEAD@{13}: commit: let contact-us work with cn language
    c5e78bf HEAD@{14}: commit: add contact-us page for test
    4783e3d HEAD@{15}: checkout: moving from master to master
    4783e3d HEAD@{21}: checkout: moving from master to develop
    4783e3d HEAD@{22}: clone: from https://github.com/yyyy/xxxxx.git
    
    zhenghuz@CV0005366N0 /d/devenv/code/xxxx (master)
    $ git reset --hard c62a928
    Checking out files: 100% (2247/2247), done.
    HEAD is now at c62a928 add yinbiao/show
  102. git clone https://github.com/kidsit/gittest.git  gittest-demo :clone动作实际上包含三个工作:下载库到本地,创建'origin' remote并且指向clone url以便后续push,checkout initial branch(sets up your local master branch to track the remote master branch (or whatever the default branch is called(就是git remote show origin的结果中  HEAD branch: master 那里显示的内容,最终本质上是由repo中的refs/remotes/origin/HEAD文件所决定 )) on the server you cloned from.)  注意github中clone和fork是有区别的,clone产生一个origin remote, 而对于最原始的那个repo,你则只能通过git remote add upstream urloftheinitialrepo来设定引用
  103. git clone -o kidsitRepo https://github.com/kidsit/gittest.git, 则在git clone完成kidsit的gittest.git repo后,创建一个名为kidsitRepo的remote,而不再是默认的origin名称,这样有利于管理你的remote!
  104. git fetch origin, git checkout -b serverfix origin/serverfix  = git checkout --track origin/serverfix: 创建一个local tracking branch serverfix,该分支将和origin/serverfix绑定(建立一种直接的关系)。这时,如果你在tracking branch上,即:serverfix分支上,如果你直接运行git pull命令,则git自动从origin的serverfix分支同步commit,并且自动merge到你的local serverfix上

    Tracking branches are local branches that have a direct relationship to a remote
    branch. If you’re on a tracking branch and type git pull, Git automatically
    knows which server to fetch from and branch to merge into.

  105. git banch -vv -a 
    $ git branch -vv
    iss53 7e424c3 [origin/iss53: ahead 2] forgot the brackets
    master 1ae2a45 [origin/master] deploying index fix
    * serverfix f8674d9 [teamone/server-fix-good: ahead 3, behind 1] this should do it
    testing 5ea463a trying something new

    上述命令显示我们的local branch iss53实际上tracking了origin/iss53,并且本地有2个commit没有push,local master branch tracking了origin/master分支并且是up-to-date状态;serverfix分支tracking了teamone/server-fix-good分支,并且本地有了3个commit没有push,同时server上有1个commit我们没有fetch;testing是纯粹的local branch. 注意:本命令看到的是最近一次fetch后的结果,它本身并不会主动和remote联络更新信息。因此最好的使用方式是: git fetch --all; git branch -vv -a

  106. git branch cat;git checkout cat :另一个开发人员在clone库后需要做的就是创branch,并且checkout变换到新的branch上工作
  107. git checkout master; git merge cat:在上述branch工作结束时需要merge到主干上来
  108. git branch -d cat 工作完毕已经merge随后就删除该branch
  109. git checkout -b admin 创建一个admin的branch同时checkout开始在admin branch上工作
  110. fast-forward的概念:如果在创建branch后并且开始工作,在merge到master时,master并无改动,那么这种情况就是fast-forward
  111. remote branch:当你需要其他人在你的branch上工作时,任何可能存续超过1天时间时。git checkout -b shopping_cart;git push origin shopping_cart,这时link the local branch with the remote branch(tracking)。git add cart.rb;git commit -am "add basic shopping cart feature";git push;这将把本地的shopping_cart branch push到remote,其他人就能看到这个branch了
  112. git branch -r : list all remote branches(origin/master, origin/shopping_cart)
  113. git remote show origin    show the remote , 这条命令将展示:
    $ git remote show origin                                                                    
    * remote origin                                                                                                                
      Fetch URL: https://github.com/cnweibo/githubtest.git                                                                         
      Push  URL: https://github.com/cnweibo/githubtest.git                                                                         
      HEAD branch: master                                                                                                          
      Remote branches:                                                                                                             
        kidsitbranch tracked                                                                                                       
        master       tracked                                                                                                       
      Local branch configured for 'git pull':                                                                                      
        master merges with remote master                                                                                           
      Local ref configured for 'git push':                                                                                         
        master pushes to master (local out of date)   

    注意:在上面的输出信息中,其中   HEAD branch: master   指示你的remote origin它的默认分支是master,这样你一旦从那个repo clone过来时,git自动创建一个local master to track that remote repo master branch!!!

  114. git push origin :shopping_cart         Deletes remote branch, git branch -d shopping_cart Must delete local branch manually
  115. git remote prune origin      :clean up deleted remote branches
  116. git tag; list all the tags, git checkout v0.0.1; checkout code at commit
  117. git tag -a v0.0.3 -m"version 0.0.3" : add a new tag
  118. git push --tags (push new tags) :注意git push时并不会自动将tag上传,所以需要手工指定: git push origin [tagname].
  119. git fetch(将会从remote的repo master更新到local repo的origin/remote这个分支。当teammember创建了一个分支,并且push到remote repo中时,我们如果需要使用这个分支并且track它时,需要首先git fetch,随后:git branch --track branch-name origin/branch-name,以便将local的branch-name指向origin/branch-name并且track它。可以使用git remote show origin来查看是否有local branch track这个remote了。该命令和下面的命令可能等效。
    git branch --set-upstream local-branch-name origin/remote-branch-name、
    git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name] 上面几个命令都创建了能够track remote的branch。所谓track是指在local branch上git pull,git就知道从哪个remote上去取并且merge到哪个local branch上,这时你可以通过git remote show命令来显示详细信息);git rebase;这个动作有三个小步骤:(首先move all changes to master which are not in origin/master(别人已经push的改动)to a temprary area,随后run all origin/master commits on the master branch;最后执行暂存于临时区域的所有commit)
  120. 如果你希望在你的git log中看到一个线性的历史轨迹,则可以使用git pull --rebase,并且可以将此作为git的默认行为
    在.git/config中
    [branch "master"]
      remote = origin
      merge = refs/heads/master
      rebase = true
    或者全局性地:
    ~/.gitconfig
    [branch]  
      autosetuprebase = always

     

  121. 在你有本地变更,工作还没有做完从而还没有commit工作的情况下,你又要获取remote repo的变更作为基线,那么参照以下流程a)git stash;b)git pull --rebase(这个动作可以通过上面的配置成为标准动作,其本质是 rebase your local commits on top of the newly pulled-down commits).  c)git stash pop

     

  122. git workflow常用命令_第14张图片

  123. git checkout admin;git rebase master;git checkout master;git merge admin==>形成一种所谓fast-forward的场景。下图中,在rebase之后,feature branch就有了一个新的parent commit,而它和master所指向的commit是相同的。不同于joining the branches with a merge commit, rebase通过在master tip上来完成master集成到feature branch上的工作,这样的结果是一个线性的历史记录便于阅读。注意,为了在admin branch 进行merge到master时历史commit不要太过细节,我们可以使用git rebase -i来整合admin branch上的所有commit,使得这些commit可以作为一个整体一次性commit,这样历史信息就很单纯了。比如你可能看到的就是admin feature added into master,而不是admin feature的5个细节commitgit workflow常用命令_第15张图片git workflow常用命令_第16张图片

    rebase local changes
    you’ve made but haven’t shared yet before you push them in order to clean up
    your story, but never rebase anything you’ve pushed somewhere

  124. git rebase -i HEAD~3 :将HEAD的前3个commits重新整合。当你需要在相同的branch上修正一个提交之后的所有提交时(注意该命令将影响你指定的commit之后的所有commit!!),这个命令就派上用场了。rebase -i的原理就是将本branch上的指定commit之后的所有commit临时放到一个空间中,你可以选择pick,edit,squash,reword,fixup,随后将按照你的新的要求来replay这些commits。还有比如你希望将两个commits的历史做一下调整颠倒的话,也可以用,这时只需在popup出来的editor中颠倒更改两个commits的行文顺序,后面就会按此顺序replay commits了。注意:rebase -i命令展示的commits顺序(最旧到最新)和git log --oneline是相反的(最新到最旧)。还有一种需求场景是希望将一个历史的commit分解为几个commit
  125. git log --oneline  以一行一个commit的方式展示修改历史
  126. 假设以下场景:master和unicorns两个branch,unicorns从master第一个commit后创建的,随后master和unicorns各有两个本分支的commit,现在如何在unicorns上同步呢? git checkout unicorns;git rebase master(在unicorns分支上rebase master分支的内容);
  127. git rebase --onto master server client :这个有点复杂,

    Check out the client branch, figure out the patches from
    the common ancestor of the client and server branches, and then replay
    them onto master  git workflow常用命令_第17张图片git workflow常用命令_第18张图片                         git rebase master server :replay server work on top of master

  128. git-cherry-pick   : Apply the changes introduced by some existing commits
  129. git ls-remote --heads upstream   :仅列出upstream repo中所有的branch
    $ git ls-remote upstream
    4783e3d9a4d7e9c7ea117b4277b859674651dbb4        HEAD
    4783e3d9a4d7e9c7ea117b4277b859674651dbb4        refs/heads/develop
    4783e3d9a4d7e9c7ea117b4277b859674651dbb4        refs/heads/f_migration
    4783e3d9a4d7e9c7ea117b4277b859674651dbb4        refs/heads/master
    98aa6d8c2801c9639db39466f12804285eda6eec        refs/tags/v0.0.1
  130. git fetch --all; git branch -vv -a 这两个命令组合是检查repo之间同步情况的最有效方式
  131. git check-ignore -v .  查看本文件夹是由哪个gitignore rule而导致被忽略掉的

    vagrant@homestead:~/code/kidsit/resources/assets/bower_components$ git check-ignore -v .
    .gitignore:7:resources/assets/bower_components/ .

     

    vagrant@homestead:~/Code/kidsit$ git fetch --all
    Fetching origin
    From https://github.com/cnweibo/kidsit
     * [new branch]      develop    -> origin/develop
     * [new branch]      f_migration -> origin/f_migration
    
    $ git branch -vv -a
    * master                     4783e3d [origin/master] initial runnable version based on laravel5.1
      remotes/origin/HEAD        -> origin/master
      remotes/origin/develop     4783e3d initial runnable version based on laravel5.1
      remotes/origin/f_migration 4783e3d initial runnable version based on laravel5.1
      remotes/origin/master      4783e3d initial runnable version based on laravel5.1
  132. TO BE ADDED
  133. git ls-files  :列出index和working directory中的文件列表 
    vagrant@homestead:~/code/codeanywhereconsoleenv (master)$ git ls-files
    .bash_profile
    .bashrc
    .gitconfig
    git-aware-prompt/LICENSE
    git-aware-prompt/README.md
    git-aware-prompt/colors.sh
    git-aware-prompt/main.sh
    git-aware-prompt/preview.png
    git-aware-prompt/prompt.sh

     vagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git ls-files -m :只列出working directory和index不同的文件(也就是修改过的) -d:列出删除了的
      .gitconfig

     
    vagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git ls-files -s  : -s参数将输出blob的sha
    100644 cbd26e0815ae1f66f6658281e93f1e0be6eb579c 0       .bash_profile
    100644 71d2312c4ec072698a905cf94e8ee480b7bdb589 0       .bashrc
    100644 fab46aec849e85527d018878d878f2cd82ecd439 0       .gitconfig
    100644 3bbbc1ee92562e6a2eebfc6a366f4309d06c7d54 0       git-aware-prompt/LICENSE
    100644 f4f240652a9ec7d698554e0fa81465b885389453 0       git-aware-prompt/README.md
    100644 448ad10727d1a84dd6e2572c033b42f1e775f847 0       git-aware-prompt/colors.sh
    100644 620d7ed172b4dc64226fb60f6d542c63d7429cd5 0       git-aware-prompt/main.sh
    100644 2bafa65cb58464475bcc9861547ad679e1159a71 0       git-aware-prompt/preview.png
    100644 6a737b848df0a8bd52a1eb0a7cb4c67b71e4b258 0       git-aware-prompt/prompt.sh
    vagrant@homestead:~/code/codeanywhereconsoleenv (master)*$ git cat-file blob fab46aec849e85527d018878d878f2cd82ecd439

     

     

  134. git bisect :查找1.2和1.6之间开始引入bug的版本
  135. 查找一个文件在哪个commit中第一次git add的
    vagrant@homestead:~/Code/kidsit (develop)$ git log --diff-filter=A -- public/favicon.ico
    commit 1b613522447dd16a70bc693dc558eeab511cd725
    Author: 1372921435 <1372921435@qq.com>
    Date:   Sat Jul 18 09:36:20 2015 -0400
    
        intial non lib version

     

  136.  git log -- public/favicon.ico 查看单独文件的历史信息
    git vagrant@homestead:~/Code/kidsit (develop)$ git log -- public/favicon.ico
    commit 60ba5a064962db5fa05aa0bbe20038c4fef47677
    Author: cnweibo 163.com>
    Date:   Fri Jul 31 04:24:57 2015 +0000
    
        admin grade primary work
    
    commit 1b613522447dd16a70bc693dc558eeab511cd725
    Author: 1372921435 <1372921435@qq.com>
    Date:   Sat Jul 18 09:36:20 2015 -0400
    
        intial non lib version

     

  137. git show SHA:file  直接获取一个文件特定版本内容
    vagrant@homestead:~/Code/kidsit (develop)$ git show 60ba5a064962db5fa05aa0bbe20038c4fef47677:public/favicon.ico 
    #$# git-fat da39a3ee5e6b4b0d3255bfef95601890afd80709                    0

     

  138. 只checkout一个文件的特定版本,其他历史不变: 
    git checkout commitSHA file/to/restore
  139. 一次性列出所有conflict的文件
    vagrant@homestead:~/Code/kidsit (detached*)*$ git diff --name-only --diff-filter=U
    public/favicon.ico

     

  140. git filter-branch --tree-filter 'rm -f password.txt' -- --all(所有branch)/HEAD(当前branch) :删除所有branch上的所有commits中所包含password.txt文件,并且重新commit。重写历史!注意这条命令将在所有的branch上checkout所有的commit,并且执行后面'rm -f password.txt'的命令,并且再次commit,如果是很大的repo的话,这个是很耗时的。如果使用--index-filter的话,则不用checkout,而是直接在index/staging area操作,这样将会大大提高效率。这时上述命令就将调整为:git filter-branch --index-filter 'git rm --cached --ignore-unmatch password.txt' ; 
  141. git filter-branch -f --prune-empty -- --all :删除所有没有更改文件的commits
  142. git checkout --track origin/develop  :创建本地branch以便tracking origin repo的develop分支
$ git checkout --track origin/develop
M       vendor/mockery/mockery/.styleci.yml
M       vendor/monolog/monolog/.php_cs
M       vendor/psy/psysh/.styleci.yml
M       vendor/swiftmailer/swiftmailer/tests/_samples/charsets/iso-2022-jp/one.txt
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'
vagrant@homestead:~/Code/kidsit$ git checkout -- . 

 

常见问题解决方法:

  • 出现一大片可能不是你希望看到的“modified”,比如:

        modified:   vendor/zizaco/entrust/src/Entrust/Traits/EntrustUserTrait.php
        modified:   vendor/zizaco/entrust/src/commands/MigrationCommand.php
        modified:   vendor/zizaco/entrust/src/config/config.php
        modified:   vendor/zizaco/entrust/src/views/generators/migration.blade.php

no changes added to commit (use "git add" and/or "git commit -a")
vagrant@homestead:~/code/kidsit$ git diff app/Console/Commands/Inspire.php
diff --git a/app/Console/Commands/Inspire.php b/app/Console/Commands/Inspire.php
old mode 100644
new mode 100755

这些奇怪的变动,可能是由Unix文件权限模式的变更,老的模式包含+x,新的模式不含这个x,

That looks like unix file permissions modes to me (755=rwxrw_rw_644=rw_r__r__) - the old mode included the +x (executable) flag, the new mode doesn't.

This msysgit issue's replies suggests setting core.filemode to false in order to get rid of the issue:

 解决的方法就是:

git config core.filemode false
  •  git status出现一大片不是你希望看到的“改动”

可能的原因:当你在多个操作系统中协同使用git来工作的时候,由于不同OS对于回车键的处理有不同,所以有可能在你做git status时出现大片由于enter键区别导致的改动。解决方法就是:

git config --global core.autocrlf true
  •  偶尔出现明明是你新install的component(比如使用composer require),当你git status时,git却总是说Nothing to add

这个可能的原因是:要么该目录被.gitignore了,你可以使用git check-ignore -v directory来检查,也可能是由于第三方库配置了submodule的缘故。比如我在require一个bllim/datatables时,就出现这种怪异现象。

cabox@box-codeanywhere:~/workspace/temp/tmp/vendor$ git check-ignore bllim/datatables/README.md                                                                                                  
fatal: Pathspec 'bllim/datatables/README.md' is in submodule 'bllim/datatables' 

解决办法: 1,删除.git目录(我不需要submodule),2,git rm --cached directory 3. git add directory

这时,再执行git status, git就能发现vendors/bllim/datatables了!

  •  如何一次性删除所有untracked file??  

    vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)*$ git clean -d --dry-run
    Would remove app/Http/Controllers/Admin/AdminMathskillcatController.php
    Would remove public/htmlapp/math/
    Would remove public/htmlapp/system/
    Would remove public/preparebuild/assets/libs/angular-ui-event/
    Would remove public/preparebuild/assets/libs/angular-ui-indeterminate/
    Would remove public/preparebuild/assets/libs/angular-ui-mask/
    Would remove public/preparebuild/assets/libs/angular-ui-scroll/
    Would remove public/preparebuild/assets/libs/angular-ui-scrollpoint/
    Would remove public/preparebuild/assets/libs/angular-ui-uploader/
    Would remove public/preparebuild/assets/libs/angular-ui-utils/
    Would remove public/preparebuild/assets/libs/angular-ui-validate/
    Would remove public/preparebuild/assets/libs/angular-xeditable/
    Would remove resources/views/admin/mathskillcats/
    Would remove resources/views/admin/partials/csrf_token.blade.php
    Would remove resources/views/admin/partials/indicatorcontainer.blade.php

    git clean -d -f
    Removing public/htmlapp/math/
    Removing public/htmlapp/system/
    Removing public/preparebuild/assets/libs/angular-ui-event/
    Removing public/preparebuild/assets/libs/angular-ui-indeterminate/
    Removing public/preparebuild/assets/libs/angular-ui-mask/
    Removing public/preparebuild/assets/libs/angular-ui-scroll/
    Removing public/preparebuild/assets/libs/angular-ui-scrollpoint/
    Removing public/preparebuild/assets/libs/angular-ui-uploader/
    Removing public/preparebuild/assets/libs/angular-ui-utils/
    Removing public/preparebuild/assets/libs/angular-ui-validate/
    Removing public/preparebuild/assets/libs/angular-xeditable/
    Removing resources/views/admin/mathskillcats/

 

如何一次性清除所有的改动?包括unstaged(modified),untracked? 随后也就将git flow中创建的新feature delete掉?一种可行的方法是:

git stash save --keep-index 和git stash drop
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)*$ git stash save --keep-index 
Saved working directory and index state WIP on feature/mathskillcatmanage: 417f2ec printmath.css
HEAD is now at 417f2ec printmath.css
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git status
On branch feature/mathskillcatmanage
nothing to commit, working directory clean
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git stash list
stash@{0}: WIP on feature/mathskillcatmanage: 417f2ec printmath.css
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git stash drop
Dropped refs/stash@{0} (a5df6ceb236bd864cf182cca3685559a3406ce27)
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git status
On branch feature/mathskillcatmanage
nothing to commit, working directory clean
vagrant@homestead:~/Code/kidsit (feature/mathskillcatmanage)$ git flow feature delete mathskillcatmanage
Switched to branch 'develop'
Deleted branch feature/mathskillcatmanage (was 417f2ec).

Summary of actions:
- Feature branch 'feature/mathskillcatmanage' has been deleted.
- You are now on branch 'develop'
  •  如果你使用git fat来管理你的大文件时,当clone一个repo后第一件事儿你可能需要git fat init初始化git fat meta数据,随后你需要执行git fat pull操作,随后如果你执行git pull --rebase这个动作时,你就可能会因为变更了你的repo中的fat文件(即便是file mode之类的变更!),所以当执行git pull --rebase时出现unstaged files, can not commit,而同时奇怪的是当你git status时又无任何输出。这时将会是一团雾水。你可以执行git diff-files来找到git 认为的曾经被变更的文件集,给你一些解决问题的思路。之所以想到这个主意是因为发现git rebase实际上是/usr/libexec/git-core/git-rebase这个shell脚本,搜索"have unstaged changes"发现只有以下地方有打印:
    require_clean_work_tree () {
        git rev-parse --verify HEAD >/dev/null || exit 1
        git update-index -q --ignore-submodules --refresh
        err=0
    
        if ! git diff-files --quiet --ignore-submodules
        then
            echo >&2 "Cannot $1: You have unstaged changes."
            err=1
        fi
    
    [ ... ]

    所以从上面的代码就可以看出一定是git diff-files命令有文件输出故而才会打印have unstaged changes

    cabox@box-codeanywhere:~/workspace/kidsit$ git pull --rebase                                                                                                                                           
    Cannot pull with rebase: You have unstaged changes.                                                                                                                                                    
    Please commit or stash them.  

    cabox@box-codeanywhere:~/workspace/ktest/kidsit$ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    nothing to commit, working directory clean

    abox@box-codeanywhere:~/workspace/kidsit$ git diff-files    
    :100644 100644 565a0e6b45a0fde1ccd1f39ee2bc6b0ba81ac0b0 0000000000000000000000000000000000000000 M      storage/uploaded/yinbiaomp3/1409927256_10.mp3                                                  
    :100644 100644 a08e9d3b68da37f2e233d0d103e2a3a90405ab3e 0000000000000000000000000000000000000000 M      storage/uploaded/yinbiaomp3/1409927276_10.mp3                                                  
    :100644 100644 baad2e2763847dded441f59776b7e9a544f0cea8 0000000000000000000000000000000000000000 M      storage/uploaded/yinbiaomp3/1409927297_5.mp3                                                   
    :100644 100644 a694caba87b3b4d4a57f9070a9d983e368ad8a14 0000000000000000000000000000000000000000 M      storage/uploaded/yinbiaomp3/1409927315_8.mp3   
    
    cabox@box-codeanywhere:~/workspace/kidsit$ git diff  storage/uploaded/yinbiaomp3/1409927276_10.mp3                                                                                                     
    warning: LF will be replaced by CRLF in storage/uploaded/yinbiaomp3/1409927276_10.mp3.                                                                                                                 
    The file will have its original line endings in your working directory.                                                                                                                                
    diff --git a/storage/uploaded/yinbiaomp3/1409927276_10.mp3 b/storage/uploaded/yinbiaomp3/1409927276_10.mp3                                                                                             
    index a08e9d3..373f7b6 100644                                                                                                                                                                          
    --- a/storage/uploaded/yinbiaomp3/1409927276_10.mp3                                                                                                                                                    
    +++ b/storage/uploaded/yinbiaomp3/1409927276_10.mp3                                                                                                                                                    
    @@ -1 +1 @@                                                                                                                                                                                            
    -#$# git-fat c3ea5a9a54d0809b60de406d4951905b9c6625bc                 9040                                                                                                                             
    +#$# git-fat d20e15ce5c6b8ecd2fd2f0d2fd2a498dddfee7d8                   75 
  • 如何实现当git push origin master时,自动触发deployment过程?有什么好的策略吗?一般来说,github是一个非常好用的central repo hosting service。在github中保存着项目的所有历史信息,master branch作为稳定可靠的可以deployment的分支。在depolyment server上一般也会从github上clone一个repo,当我们在自己的开发主机上完成编码和测试工作后,打一个tag,git push origin master将会把所有工作上传到github中分享,这时我们如何自动触发production server来部署呢?一种方法是登陆到production server上,随后git pull,git archive, copy到我们的部署目录中去;另外一种可行的方法是:将部署服务器上被clone的repo作为我们开发主机repo的一个remote,我们push到github上面的同时,push到deployment server上去,同时在deployment server上利用git的server侧hook机制,触发git archive,并且部署到相关目录中。具体方法是:git push gituser@productionserverIP:/directory/to/deployed/repo/ master:production 注意这个命令中master是开发主机的branch,production是depoyment这个机器的branch。一般地,在开发流程中,我们还应该再创建一个remote repo作为staging的环境,这样我们在开发过程中,一旦develop上的开发工作有了阶段性的成果,会拉出一个pre-release的分支,打上标签,push到staging主机上去,供QA测试
  • 如何将已经tracked文件从git tracking中删除(并不删除文件)?
    # Do this on all machines
    echo "FILE_NAME" >> .gitignore
    git rm --cached FILE_NAME
    git add -u
    git commit -m "removing files from version control"
    # Sync with your git server, pull to sync and push to register your local change
    git pull
    git push

     

  • 如何搭建自己的git server?以下资源可供参考:

https://git-scm.com/book/it/v2/Git-on-the-Server-Setting-Up-the-Server

http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000

https://github.com/progit/progit/blob/master/zh/04-git-server/01-chapter4.markdown

http://www.centoscn.com/CentosServer/ftp/2014/0414/2789.html

http://freeloda.blog.51cto.com/2033581/1410562

https://www.digitalocean.com/community/tutorials/how-to-set-up-a-private-git-server-on-a-vps

https://www.linux.com/learn/tutorials/824358-how-to-run-your-own-git-server

http://www.linux-magazine.com/Online/Features/Install-Your-Own-Git-Server

http://blog.csdn.net/csfreebird/article/details/7204345

http://wiki.ubuntu.org.cn/Git%E6%9C%8D%E5%8A%A1%E5%99%A8Gitosis%E5%AE%89%E8%A3%85%E8%AE%BE%E7%BD%AE

  •  为了确保master分支的稳定,必须对master分支做好控制(甚至develop分支也要控制),那如何控制向master分支的push操作呢?如果使用Github,最好的策略是: 除了原始master repo分配给deployment manager外,先fork出一个staging repo分配给system integration manager,每一个开发人员都从这个staging repository(从master repo fork来的)(注意:如果有大的feature team,则可以再增加一级:feature team leader fork一个repo作为整个team协同工作的repo)来fork出自己的repo,每一个人都在本地开发后push到自己fork出来的自己有写权限的repo,开发人员在自己的repo中开发测试完毕后,pull request提交到staging repo,这时system integration manager召集专家和相关team member来做review(如果可以fast forward的,则无需review),随后确认接受后就放到staging repo的develop分支,正式宣布接受开发人员完成的一个feature变更。一旦有feature落地,则应该触发QA过程,QA过程中发现的问题提交bug给team leader。具体怎么知道应该给哪些team leader呢?QA人员应该使用git bisxx功能,给出问题是一直存在的还是后来引入的,如果是新代码引入的,那么是哪个feature落地带来的呢?依据这些信息就能找到应该找谁来进一步处理。如上面所说,如果feature较大,需要至少2个人人以上来开发,那么就需要指定feature team leader,同时将staging repo的collabarator为每个team leader开放权限?team leader可以往staging repo的dev下有Push权限。QA完成后,可以打tag,由集成测试经理执行merge到master的动作,并且向master repo发pull request。最后由信息集成官确认ok,部署代码,负责运维。
  • 如果在上面描述的工作流中,我们不使用github,而是自建git server,则可以在服务端通过hook来实现这个权限的控制。
  • 如何既能在feature分支上保留详细的历史信息又能在master分支上只保持一个一个feature粒度的commit?

在feature开发中,我们鼓励及时commit,这个时候变更管理的粒度是很小的。但是对于一个产品级别的管理者来说,他们可能只关心一个个feature级别的变更,因此当需要将feature落地到master/develop分支上时,我们可以通过下面的策略来执行(先在featureX分支上和master merge并且测试完成,结果Ok后,再在master分支上做git merge --squash完成代码落地):

git checkout featureX
git merge master //首先在featureX分支上将featureX分支和master分支做merge并且做好测试工作,之后再在master分支上执行git merge --squash featureX只保留一个历史
git checkout master git merge
--squash featureX//通过这条命令将featureX的所有commit都合成一条放到master分支

对于小的feature,最好使用git pull --rebase, 大的feature,最好使用git merge --no-ff这样保留一个commit节点能够描绘出merge这个重要动作。

或者说: 本地开发 相同的分支总是 git pull --rebase(比如master,develop分支总是这个策略), 在本地合并分支时总是 git checkout master, 对于小的feature,则git merge --squash featureX  :主要作用是featureX的历史细节只在local repo中保留,中央库中只保留feature级别的commit;对于大的feature我们则使用git checkout master, git merge featureX  --no-ff 这样的好处是既能保留大feature的历史信息,又能使official库能大体看到版本变迁历史的大粒度视图;还有也可以对featureX的commit使用git rebase -i 来重写历史梳理干净

git merge --squash featureX 

Hosted GIT repository: Github, BitBucket;

Selft Managed: Gitosis, Gitorious

  •  git clone --recursive git://github.com/nvie/gitflow.git  :使用--recursive参数会自动将repo中的submodules一起clone下来
  •  如何免密码push远程repo? a)创建ssh私钥和公钥,将公钥放到github/codingnet的配置页面上去;b)将remote repo的connection中的url修改为ssh方式的, 比如[email protected]:xxxx/yyyy.git 

当git checkout到不同的branch时,相应目录看到的东西是不一样的。这个是文件系统的功能,比较神奇啊

git checkout -- fileToDropChange   如果有文件被修改但是又还没有staging,上述命令则彻底丢弃local的变更;git checkout -- . 丢弃当前目录中所有的local change(从index区域获取fileToDropChange的内容覆盖工作区)  

http://eagain.net/articles/git-for-computer-scientists/  :Git internal

http://www.vogella.com/tutorials/Git/article.html#gitremotebranch_overview  Git commands

http://blog.csdn.net/ithomer/article/details/7529841 

如何找回偶然被删除的commit?

1. get reflog列出所有操作历史;

2. 找到对应的commit,创建一个branch : git checkout -b urgentbranch xxxcommit

3. git co dev/git merge urgentbranch

以上三步就能恢复你想要的历史commit了!

如何强制pull远端的branch?

有时候一个branch发生冲突,我们已经解决并且push了,这时可能希望强制pull成远端分支的内容,解决办法:

1. git fetch --all //获取所有远端内容

2. git co yourbranch

3. git reset --hard origin/yourbranch

上述三步解决问题!

如何列出两个branch之间的commits区别?

git show-branch stock-information staging/master master :列出两个分支之间的详细区别列表

! [stock-information] WIP: Link to data series
 ! [staging/master] Add a description to Stock
  ! [master] Display Stocks
---
+   [stock-information] WIP: Link to data series
+   [stock-information~1] Create DataSeries for Stocks.
++  [staging/master] Add a description to Stock
++  [staging/master~1] Import external Stock information
+++ [master] Display Stocks

git log --oneline --no-merges feature/xx..dev 列出现在的从feature/xx分支到dev分支之间的未被merged的commits列表(一般的fast forward merge commit不会列出来!),也就是说在dev上但是却未能出现在feature/xx分支上的所有commit

git workflow常用命令_第19张图片

解读:

http://openwares.net/linux/git_show_branch_output.html

输出分为上下两部分,使用短划线”-“分隔。两个分支使用两个短划线”–“,三个分支使用三个短划线”—“,依次类推。
上半部分为层次缩进的分支列表,下半部分为commit列表。
上半部分的分支列表中,使用*标识当前分支,其他分支使用!标识。分支前的标识符*或者!一直垂直贯通到下半部分,这一垂直列的符号都是属于这个分支的。
下半部分的commit列表中,前导的符号有*和+号。*表示这一列上的分支(当前分支)有此commit。而+表示这一列上的分支(非当前分支)有此commit。

标识符的颜色只是用于容易区分列,一列的颜色是一致的,看起来更清楚。

windows下symlink的支持

很多时候存在这样的场景:linux、windows混合环境在git项目开发中存在。在linux下存在symlink的概念,windows中从win7开始也有了symlink的支持,我们在使用nmp install时会产生很多的node_modules,而这个node_modules中就会有很多以symlink存在。这时,如果我们在linux下面做了git add和git commit,那么在repo中就存在这样的symlinks,而一旦在windows中git clone/git checkout就会发现存在问题!原因就是git bash可能不支持symbol link的操作~。解决方案:

1. 配置git的core.symlinks为true

core.symlinks=true

2.启动git bash时一定要以administrator权限启动.

that is it~!

git workflow常用命令_第20张图片

 

git如何untrack已经tracked的文件?

git rm --cached myFile
git rm -r --cached myDirectory

如何列出两个commit之间的所有commit变更?

git log --oneline ee8b8e5e6 ^feature/xiaozhao_xiqing_review

 

转载于:https://www.cnblogs.com/kidsitcn/p/4450466.html

你可能感兴趣的:(git workflow常用命令)