文章目录
分支管理
在版本控制过程中,一般使用多个分支并行推进:
分支管理命令及操作
创建分支:
git branch branchName
查看各个分支最后一次提交:git branch -v
切换分支:git checkout branchName
创建并切换分支:git checkout -b mybranch
更新master主线上的东西到当前分支上:git rebase master
删除分支:git branch -d mybranch
强制删除分支:git branch -D mybranch
列出所有分支:git branch
查看哪些分支合并入当前分支:git branch –merged
查看哪些分支未合并入当前分支:git branch –no-merged
合并分支的基本操作:1.切换到需要修改的分支
git checkout 分支名
2.执行merge命令(下一节:
git merge
操作)git merge 有新内容的分支名
3.解决文件冲突
4.重新添加、提交,注意,提交操作时
git commit -m"commit flag"
,一定不能带具体文件名,否则将报错
下面来看一个具体的分支合并操作(注意:当产生冲突时,命令行中当前分支后将出现“|MERGING”):
git merge
操作
git merge
的基本用法为把一个分支或某个__commit__的修改合并到当前分支。可以使用git merge -h
或git merge --help
查看该命令详情,后者会直接转到一个git的帮助文档网页。
git merge [-n] [--stat] [--no-commit] [--squash] [--[no-]edit]
[--no-verify] [-s <strategy>] [-X <strategy-option>] [-S[<keyid>]]
[--[no-]allow-unrelated-histories]
[--[no-]rerere-autoupdate] [-m <msg>] [-F <file>] [<commit>…]
git merge (--continue | --abort | --quit)
- The second syntax (“git merge --abort”) can only be run after the merge has resulted in conflicts. git merge --abort will abort the merge process and try to reconstruct the pre-merge state. However, if there were uncommitted changes when the merge started (and especially if those changes were further modified after the merge was started), git merge --abort will in some cases be unable to reconstruct the original (pre-merge) changes.
- The third syntax (“git merge --continue”) can only be run after the merge has resulted in conflicts.
- Options:
-n do not show a diffstat at the end of the merge
–stat show a diffstat at the end of the merge
–summary (synonym to --stat)
–log[=] add (at most ) entries from shortlog to merge commit message
–squash create a single commit instead of doing a merge
–commit perform a commit if the merge succeeds (default)
-e, --edit edit message before committing
–cleanup how to strip spaces and #comments from message
–ff 默认值allow fast-forward
–no-ff 使用该参数后,会执行正常合并,在Master分支上生成一个新节点。为了保证版本演进的清晰,尽量采用这种做法。
–ff-only resolve the merge as a fast-forward when possible.When not possible, refuse to merge and exit with a non-zero status.
–rerere-autoupdate update the index with reused conflict resolution if possible
–verify-signatures verify that the named commit has a valid GPG signature
-s, --strategy merge strategy to use
-X, --strategy-option <option=value> option for selected merge strategy
-m, --message merge commit message (for a non-fast-forward merge)
-F, --fileread message from file
-v, --verbose be more verbose
-q, --quiet be more quiet
–abort abort the current in-progress merge
–quit --abort but leave index and working tree alone
–continue continue the current in-progress merge
–allow-unrelated-histories allow merging unrelated histories
–progress force progress reporting
-S, --gpg-sign[=] GPG sign commit
–overwrite-ignore update ignored files (default)
–signoff add Signed-off-by:
–no-verify bypass pre-merge-commit and commit-msg hooks
–squash选项的含义是:本地文件内容与不使用该选项的合并结果相同,但是不保留待合并分支上的历史信息,也不提交、不移动HEAD,因此需要一条额外的commit命令。其效果相当于将另一个分支上的多个commit合并成一个,放在当前分支上,原来的commit历史则没有拿过来。判断是否使用–squash选项最根本的标准是,待合并分支上的历史是否有意义。