git

分支操作

  • 连接远程仓库 - git remote add origin <url>
  • 拉取远程分支 - git fetch origin <remote branch>:<local branch>
  • 删除远程分支 - git push origin --delete <branch name>
  • 删除本地分支 - git branch -D <local branch>
  • 从当前分支建立新分支 - git checkout -b <branch name>
  • 合并远程分支 - git merge origin/<remote branch>
  • 推送本地分支到远程(远程不存在这样的分支)- git push --set-upstream origin <local branch>
  • 重命名本地分支 - git branch -m new-name
  • 查看本地分支与远程分支的对应关系 - git branch -vv
  • 同步本地分支和远程分支:
    • 如果本地和远程 2 个分支同名,git pull 一般来说可以解决问题;
    • 否则,git fetch 拉取的分支不会自动绑定参数的 origin 远程分支,这种情况下可以考虑使用 git pull origin <remote branch name>

rebase

rebase 可用于修改最近的 n 个 commit 。

> git rebase -i HEAD~3

  1 pick 2e75f95 commit1
  2 pick 8a88106 commit2
  3 pick 16c439c commit3
  4 
  5 # Rebase 26bdb62..16c439c onto 26bdb62 (3 commands)
  6 #
  7 # Commands:
  8 # p, pick <commit> = use commit
  9 # r, reword <commit> = use commit, but edit the commit message
 10 # e, edit <commit> = use commit, but stop for amending
 11 # s, squash <commit> = use commit, but meld into previous commit
 12 # f, fixup <commit> = like "squash", but discard this commit's log message
 13 # x, exec <command> = run command (the rest of the line) using shell
 14 # b, break = stop here (continue rebase later with 'git rebase --continue')
 15 # d, drop <commit> = remove commit
 16 # l, label <label> = label current HEAD with a name
 17 # t, reset <label> = reset HEAD to a label
 18 # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]

如果是选择 squash,那么将这个 commit 与前一个 commit 合并。如果是选择 edit,那么回到这个 commit 的提交前的状态(即执行 git commit -m "msg" 之前,git add 之后的状态),允许在这个 commit 上进行修改。这些修改同时会在这个 commit 之后的 commit 生效。

在上面的例子中,如果 edit commit1,那么 rebase 完成之后,commit2, commit3 同时也会有这个修改。


cherry-pick

master,我们可以通过 git merge branch-dev 合并分支 branch-dev所有 master 缺失的 commit 。如果我们只是想选择某些 commit (而不是所有),那么应该使用 git cherry-pick

git cherry-pick <commit-hash-id>

diff 操作

  • 显示最近一次 commit 的修改内容 - git show
    • 显示某次 commit 的修改内容 - git show <commit id> .
    • --stat 显示修改的文件列表。
  • git diff <branch1> <branch2> <path> 可对比 2 个分支的差异
    • --stat 显示修改的文件列表。
    • <path> 对比 2 个分支的同一个文件(或者目录)。
    • --binary 参数可以将二进制文件以 base64 编码的方式也生成文本形式的 diff 结果。

Patch Apply

生成 Patch 可以使用:

  • git format-patch <start commit id> <end commit id> - 生成 2 个 commit 之间的 patch 改动文件(本质上是文本)。
  • git diff 输出的内容,重定向到 *.patch 文件。
  • diff -uprN <folder1> <folder2> - 对比 2 个目录的内容差异(仅用于文本文件),可将输出重定向到 .patch 文件。

合并 Patch - git apply <patch file name>:

  • --check - 测试是否可以正常 Merge,可使用 $? 获取该命令的执行状态
  • --reject - 合并没有 conflict 的部分,对于有 conflict 的文件,生成一个 .rej 文件。
posted @ 2019-03-28 21:06  sinkinben  阅读(253)  评论(4编辑  收藏  举报