## git基礎開發流程php
> 第一步,新建分支git
首先,每次開發新功能,都應該新建一個單獨的分支
```shellshell
# 切換到主分支
git checkout masterfetch
# 獲取最新代碼
git pullthis
# 新建一個開發分支myfeature
git checkout -b myfeatureurl
```rest
> 第二步,提交commitorm
代碼修改後,就能夠進行commit操做了 開發
```shellrem
# git add 命令的all參數,表示保存全部變化(包括新建、修改和刪除)。
# 從Git 2.0開始,all是 git add 的默認參數,因此也能夠用 git add . 代替。
git add --all
# 用來查看發生變更的文件。
git status
#命令的verbose參數,會列出 diff 的結果。
git commit --verbose
```
> 第三步,撰寫提交信息
提交commit時,必須給出完整扼要的提交信息,下面是一個範本。
```php
Present-tense summary under 50 characters
* More information about commit (under 72 characters).
* More information about commit (under 72 characters).
```
第一行是不超過50個字的提要,而後空一行,羅列出改動緣由、主要變更、以及須要注意的問題。
若是隻是簡單提交,可使用參數 `git commit -m"fixbug"` 進行提交
> 第四步:與主幹同步
```shell
#獲取全部分支最新代碼
git fetch origin
# git merge用於把一個分支的修改合併到當前分支
git merge origin/master
```
分支的開發過程當中,要常常與主幹保持同步。
使用rebase 不會保留合併記錄,便於查看
> 第五步,推送到遠程倉庫
```shell
git push origin myfeature
```
---
## 番外篇
> 番外篇:合併屢次commit
分支開發完成後,極可能有一堆commit,可是合併到主幹的時候,每每但願只有一個(或最多兩三個)commit,這樣不只清晰,也容易管理。
```shell
# git rebase命令的i參數表示互動(interactive)
# git會打開一個互動界面,進行下一步操做
git rebase -i origin/master
```
```txt
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的下面是一大堆註釋,列出可使用的命令。
```txt
pick:正常選中
reword:選中,而且修改提交信息;
edit:選中,rebase時會暫停,容許你修改這個commit(參考這裏)
squash:選中,會將當前commit與上一個commit合併
fixup:與squash相同,但不會保存當前commit的提交信息
exec:執行其餘shell命令
```
面這6個命令當中,squash和fixup能夠用來合併commit。先把須要合併的commit前面的動詞,改爲squash(或者s)
```txt
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的提交信息。
```txt
# 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命令
```txt
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的提交信息,會被註釋掉。
```txt
# 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
```
git push命令要加上force參數,由於rebase之後,分支歷史改變了,跟遠程分支不必定兼容,有可能要強行推送
```
git push --force origin myfeature
```