git基础开发流程

## 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

```

相关文章
相关标签/搜索