git入门2

本文详细介绍了Git的分支管理,包括创建、查看、切换、删除分支以及解决冲突的方法。重点讲解了如何合并分支,包括基本操作和解决冲突的步骤。还深入探讨了`gitmerge`命令的不同选项及其用途,如--squash选项,强调了在合并时考虑分支历史信息的重要性。
摘要由CSDN通过智能技术生成

分支管理

在版本控制过程中,一般使用多个分支并行推进:
分支管理示例.png

分支管理命令及操作

创建分支:git branch branchName
查看各个分支最后一次提交: git branch -v
切换分支:git checkout branchName
创建查看及切换分支.png
创建并切换分支: 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.解决文件冲突
文件冲突.png

4.重新添加、提交,注意,提交操作时git commit -m"commit flag",一定不能带具体文件名,否则将报错

下面来看一个具体的分支合并操作(注意:当产生冲突时,命令行中当前分支后将出现“|MERGING”):
分支合并.png

git merge操作

git merge的基本用法为把一个分支或某个__commit__的修改合并到当前分支。可以使用git merge -hgit 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, --file read 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选项最根本的标准是,待合并分支上的历史是否有意义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值