zoukankan      html  css  js  c++  java
  • 五分钟上手Git

    团队开发中。遵循一个合理、清晰的Git使用流程。是非常重要的。
    否则,每一个人都提交一堆杂乱无章的commit,项目非常快就会变得难以协调和维护。
    以下是ThoughtBot 的Git使用规范流程。我从中学到了非常多,推荐你也这样使用Git。

    图片描写叙述

    第一步:新建分支

    首先,每次开发新功能,都应该新建一个单独的分支(这方面能够參考《Git分支管理策略》)。

    # 获取主干最新代码
    $ git checkout master
    $ git pull
    
    # 新建一个开发分支myfeature
    $ git checkout -b myfeature

    第二步:提交分支commit

    分支改动后,就能够提交commit了。

    $ git add --all
    $ git status
    $ git commit --verbose

    git add 命令的all參数,表示保存全部变化(包括新建、改动和删除)。从Git 2.0開始,all是 git add 的默认參数,所以也能够用 git add . 取代。
    git status 命令。用来查看发生变动的文件。
    git commit 命令的verbose參数,会列出 diff 的结果。

    第三步:撰写提交信息

    提交commit时。必须给出完整扼要的提交信息。以下是一个范本。

    Present-tense summary under 50 characters
    
    * More information about commit (under 72 characters).
    * More information about commit (under 72 characters).
    
    http://project.management-system.com/ticket/123

    第一行是不超过50个字的提要。然后空一行,罗列出改动原因、主要变动、以及须要注意的问题。最后。提供相应的网址(比方Bug ticket)。

    第四步:与主干同步

    分支的开发过程中。要常常与主干保持同步。

    $ git fetch origin
    $ git rebase origin/master

    第五步:合并commit

    分支开发完毕后。非常可能有一堆commit,可是合并到主干的时候。往往希望仅仅有一个(或最多两三个)commit。这样不仅清晰,也easy管理。


    那么。如何才干将多个commit合并呢?这就要用到 git rebase 命令。

    $ git rebase -i origin/master

    git rebase命令的i參数表示互动(interactive),这时git会打开一个互动界面,进行下一步操作。
    以下採用Tute Costa的样例,来解释怎么合并commit。

    pick 07c5abd Introduce OpenPGP and teach basic usage
    pick de9b1eb Fix PostChecker::Post#urls
    pick 3e7ee36 Hey kids, stop all the highlighting
    pick fa20af3 git interactive rebase, squash, amend
    
    # Rebase 8db7e8b..fa20af3 onto 8db7e8b
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #  f, fixup = like "squash", but discard this commit's log message
    #  x, exec = run command (the rest of the line) using shell
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out

    上面的互动界面,先列出当前分支最新的4个commit(越以下越新)。每一个commit前面有一个操作命令。默认是pick,表示该行commit被选中。要进行rebase操作。


    4个commit的以下是一大堆凝视。列出能够使用的命令。

    • pick:正常选中
    • reword:选中。而且改动提交信息;
    • edit:选中,rebase时会暂停,同意你改动这个commit(參考这里
    • squash:选中。会将当前commit与上一个commit合并
    • fixup:与squash同样。但不会保存当前commit的提交信息
    • exec:运行其它shell命令

    上面这6个命令其中,squashfixup能够用来合并commit。先把须要合并的commit前面的动词。改成squash(或者s)。

    pick 07c5abd Introduce OpenPGP and teach basic usage
    s de9b1eb Fix PostChecker::Post#urls
    s 3e7ee36 Hey kids, stop all the highlighting
    pick fa20af3 git interactive rebase, squash, amend

    这样一改,运行后,当前分支仅仅会剩下两个commit。第二行和第三行的commit,都会合并到第一行的commit。提交信息会同一时候包括,这三个commit的提交信息。

    # This is a combination of 3 commits.
    # The first commit's message is:
    Introduce OpenPGP and teach basic usage
    
    # This is the 2nd commit message:
    Fix PostChecker::Post#urls
    
    # This is the 3rd commit message:
    Hey kids, stop all the highlighting

    假设将第三行的squash命令改成fixup命令。

    pick 07c5abd Introduce OpenPGP and teach basic usage
    s de9b1eb Fix PostChecker::Post#urls
    f 3e7ee36 Hey kids, stop all the highlighting
    pick fa20af3 git interactive rebase, squash, amend

    运行结果同样。还是会生成两个commit,第二行和第三行的commit。都合并到第一行的commit。可是,新的提交信息里面,第三行commit的提交信息,会被凝视掉。

    # This is a combination of 3 commits.
    # The first commit's message is:
    Introduce OpenPGP and teach basic usage
    
    # This is the 2nd commit message:
    Fix PostChecker::Post#urls
    
    # This is the 3rd commit message:
    # Hey kids, stop all the highlighting

    squashfixup命令。还能够当作命令行參数使用,自己主动合并commit。

    $ git commit --fixup  
    $ git rebase -i --autosquash 

    这个使用方法请參考这篇文章,这里就不解释了。

    第六步:推送到远程仓库

    合并commit后,就能够推送当前分支到远程仓库了。

    $ git push --force origin myfeature

    git push命令要加上force參数,由于rebase以后。分支历史改变了。跟远程分支不一定兼容,有可能要强行推送(參见这里)。

    第七步:发出Pull Request

    提交到远程仓库以后,就能够发出 Pull Request 到master分支。然后请求别人进行代码review。确认能够合并到master。

    欢迎关注我和我的公众号。定期提供各种干货哦!


  • 相关阅读:
    吉林1号整理
    easyUI的常见属性
    Windows7配置IIS时出现Default Web Site上怎么多了个问号或者右键浏览浏览器没有反应
    安装IIS后,应用程序池找不到相应的.net版本
    JS遍历对象里面有什么内容
    ASPX 页面可以从后台直接传递值到前端。
    datagridview相关
    sqlserver 同字段值拼接 列转行
    ASP.NET中Request.RawUrl、Request.Url的区别Path.GetFileName、Path.GetExtension、Path.GetDirectoryName
    SQLserver 行变列。
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6822475.html
Copyright © 2011-2022 走看看