Git GUI工具

lazygit

https://github.com/jesseduffield/lazygit

Git Fork

比 SourceTree 简洁,快速

基本使用

配置 Merge tool/External Diff Tool

配置路径:File→Preferences

ir3gz

Git fork 解决冲突

tfk9u

git fork 自带 merge tools 解决冲突

5s4cl

te2jg

疑问

配置了 --recurse-submodules git pull 按钮报错

执行命令查看 --recurse-submodules 是否配置了,true 表示配置了:

Git config --show-origin submodule. Recurse.

52s9h
配置成 true 后,fork 每次手动 git pull 都报错
aqqy4
解决:去掉 --recurse-submodules=true 配置,fork 的 git pull 命令不会自动加上该配置,fork 在 pull/checkout 会自动更新 submodules

Git config --global submodule. Recurse false

VS Code 解决 Git 冲突

vscode 解决 git pull --rebase 出现的冲突

rebase 变基后,本地的变成了 remote 了,远端的变成了 local,所以:

72x2b
冲突后如何选择?

mac 上的不太一样,merge 完成点击 Complete Merge 即可:
nv2vs

SourceTree

SourceTree 跳过 Atlassian 账号,免登陆,跳过初始设置

SourceTree 安装之后需要使用账号登陆以授权,并且是强制登陆。
登录过程非常漫长,并未在不 FQ 的情况下是不能成功的,下面记录一下跳过登录的方法:
装之后,转到用户本地文件夹下的 SourceTree 目录,找到 accounts.json 文件,没有则新建。C:\Users\Administrator\AppData\Local\Atlassian\SourceTree 输入以下内容保存即可:

[
  {
    "$id": "1",
    "$type": "SourceTree.Api.Host.Identity.Model.IdentityAccount, SourceTree.Api.Host.Identity",
    "Authenticate": true,
    "HostInstance": {
      "$id": "2",
      "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountInstance, SourceTree.Host.AtlassianAccount",
      "Host": {
        "$id": "3",
        "$type": "SourceTree.Host.Atlassianaccount.AtlassianAccountHost, SourceTree.Host.AtlassianAccount",
        "Id": "atlassian account"
      },
      "BaseUrl": "https://id.atlassian.com/"
    },
    "Credentials": {
      "$id": "4",
      "$type": "SourceTree.Model.BasicAuthCredentials, SourceTree.Api.Account",
      "Username": "",
      "Email": null
    },
    "IsDefault": false
  }
]

现在再打开 SourceTree,直接显示主窗口了。

SourceTree&Git 部分名词解释

SourceTree 多标签 (TAB) 支持

x40cd

SourceTree 之 pull

h3r6q

  1. Commit merged changes immediately
    将 merge 过来的代码直接 commit,不会 push
  2. Include messages from commits being merged in merge commit
    将 merge 的代码的 commit 添加到 commit messages 中
  3. Create a new commit even if fast-forward is possible
    会创建一条新的 commit message 如果符合 fast-forward
  4. Rebase instead of merge
    用 rebase 替代 merge,前提是你没有要 commit 的代码;分支线很好看,不会凌乱

SourceTree 之 merge

1. 首先切换到要合并的分支

2. 点击工具栏上的 merge

s5trp

8zrb9

xqhc2

SourceTree git rebase -i 变基

未 push 到远端

选中 9961a67d5d3e528df32a49f8800cd8b289436bf3(不包括)
k6os6
Rebase children of 9961a67d5 interactively
h5q72

  1. 上下箭头变更 commit 顺序
  2. squash with previous,只能选择第一个(在最后一个点击无效),后面的合并到该 commit,而 commit message 默认为:
提交错了啊 (+1 squashed commit)
Squashed commits:
[d087858] 连击动画

19iwr

  1. Edit message 可以更改 commit message
  2. Delete,删除该 commit
git rebase --abort // 放弃
git rebase --continue // 继续

已经 push 到远端

  1. 同未 push 到远端
  2. 需要加个 git push -f

SourceTree 回滚

1、Unstaged files 和 Staged files 需要回滚

Unstaged files
未 git add 文件状态
Staged files
git add 后文件状态
此时的文件还是处于本地分支, 远端还没有, 直接选中要回滚的文件右键 Discard 丢弃掉
w29zz

2、已经 commit,未 push

这个可以先 git pull --rebase 下来,此时你的 commit 在最上面;然后再选中你提交的上一个 commit,然后 reset,选择 hard 模式。
4dim5

3、已经 push 的

要注意选择 commit id

  1. 选中要回滚的 commit,
  2. 右键,Reverse commit,这种方式不好有冲突,会留下一个 Revert 的 commit 记录

然后生成一条未 push 的 commit,并生成一条 commit msg,类似 Revert "fix: Splash页,debug环境20秒跳转"

  1. 也可以用 git reset,修改后,git push -f 强推(这种方式可能容易出冲突,但是没有了 log 了)

SourceTree 丢弃本地未 commit 的修改记录和新增的文件

1、没有新增的文件

jcrhk

如果要删除新增的文件,上诉操作后,选中要删除的文件右键 remove

2、有新增的文件

Repository→Checkout Clean
04fac

可以 Discard 修改的文件,貌似也删除不了新增的文件

SourceTree 配置 merge tools:Beyond Compare

配置 Beyond Compare 辅助解决冲突

hxsaj

a2m1t

qjlcp

BC 工具对比解决 git 冲突时 Mine/Theirs 区分

diosm

  1. 如果没有加 --rebease,就是 merge,左边 xxx_LOCAL 就是你提交的,右边的 xxx_REMOTE 就是他人提交的
  2. 如果加了 --rebase,左边的 xxx_LOCAL 就是他人提交的,右边的 xxx_REMOTE 就是你提交的
  3. 蓝色代表的 LOCAL 的,灰色代表中间的 BASE,粉色代表的是 REMOTE 的;默认 BC 会为什么自动合并代码
    1. 当出现冲突时,冲突的地方会有红色的感叹号,我们可以选择用 LOCAL 的还是 REMOTE;或者先 left 再 right;或者先 right 再 left
    2. 点击上方工具栏的 Next ConfilctPrev Conflict 可快速定位到后/前一个冲突的代码块

用 BC 解决冲突时不生成.orig

但是会生成 README.md.orig 文件

git config --global mergetool.keepBackup false
# git mergetool backup
*.orig

SourceTree Mine/Theirs 解决冲突时的坑

n45el

  1. SourceTree 在解冲突时,如果使用 Resolve using 'Theirs',不是指冲突部分使用他人版本,而是将整个文件 Resolve using 'Theirs'没有冲突的那部分也会被丢弃。

https://segmentfault.com/a/1190000019990611

  1. git merge/git pull 时的冲突
    这个时候,Resolve using 'Mime' 表示的是自己本地的修改;Resolve using 'Theirs' 表示的是远端的修改
  2. git rebase 方式的冲突
    Resolve using 'Mime' 表示的是远端的修改;Resolve using 'Theirs' 表示的是本地的修改,这个要注意
  3. applying a stash
    这个没有试

Mine/Theirs 的解释

官方解释
https://community.atlassian.com/t5/Git-questions/Resolve-using-quot-theirs-quot-vs-quot-mine-quot-what-is-the/qaq-p/393422

When you have conflict between your file and the file that you're merging, you have two choices:
"Mine" - Your file will be used to solve the conflicts
"Theirs" - The Target file will be used to solve the conflicts.

啊男公司 git 布道师解释:

官方问题反馈:
https://jira.atlassian.com/browse/SRCTREE-1579?_ga=2.74721882.952710698.1566959108-1816678471.1566959108

注意

免费版本不支持 Three-Way 冲突对比,只有 Local 和 Remote 对比

SourceTree 配置 merge tools:Vscode

  1. Vscode: Install Command tools
  2. 配置 diff tool: code --diff --wait "$LOCAL" "$REMOTE"
  3. 配置 merge tool: code -n --wait "$MERGED"

ovmcr

SourceTree 配置多个 ssh key

Tools→Options:

5a7z6