git log、git tag、git branch

Git-scm: Git tools - submodules

  • Git-scm: Git Submodules man page
  • Git Submodules basic explanation 适用场景和使用步骤,透彻。
  • SitePoint - Understanding and Working with Submodules in Git 引入三方库的例子,挺好。
  • Atlassian: Git Submodule workflow tips
    Git submodules are a powerful way to leverage git as an external dependency management tool. 适合作为一个外部依赖管理的工具。
  • Git Submodules Pros and Cons 入门,关于 blog 网站的内容(content )、主题(theme )、开发指南(developer guides)的例子很好。
  • 以 mbedtls 依赖为例 讲了 Submodules。
  • naging Git projects with submodules and subtrees:submodules 和 subtrees 的区别。
  • The Git submodule: misunderstood beast or remorseless slavering monster?:这篇文章对使用 submodules 以复用 component 的好处说得非常到位。
  • Source Control Tools:Android 开发用到的 Repo、Gerrit 等。 AOSP: Android Open Source Project.

新建发行版 create a release

  • Do Git tags only apply to the current branch?: tag 和 branch 标签和分支什么关系?
    tag 和 branch 没任何关系,tag 只是一个指向一个 commit 的指针。git show 列出的就是这个 tag 指向的 commit 的信息。
  • git describe [--tags] describes the current branch in terms of the commits since the most recent [possibly lightweight] tag in this branch's history. 列出指定 commit 和 最近 tag 的 基本关系。

git remote 远程地址变化

  • git remote set-url origin <新地址> 或者 remove + add。

git merge --patch 从分支上合并一个文件

  • 主支上的一个文件,在分支上变更了(比如解决了一个bug),怎么合并过来?
# 签出 master,然后把 amazing_feature_branch 分支的 index.html 并过来。
git checkout master
git checkout --patch amazing_feature_branch index.html
  • Git - How to Merge a Single File from a Branch?

git diff

  • git diff --name-only ..: 显示变更了哪些文件。这在合并分支时,方便查看修改文件列表。
    git diff -b -w: --ignore-space-change --ignore-all-space. 加上这两个选项,可以极大的减少那些不重要的变化。
  • What is Git Diff and How Do We Read the Output?
    How to read the output from git diff?
    详尽地解释了 git diff 输出界面上的每一行内容的含义,详实。

git log

  • git log 用于查看每个提交的日志(Show commit logs)。从 Viewing the Commit History 开启了解 git log 的新视角。
  • 好的注释可以让人一下子就了解本次修改的目的,比如修改了哪个BUG,解决了什么问题,实现了什么功能。
  • git log --reverse: 从第一个 commit 开始,默认是看最新的 commit。

至于具体修改了哪些文件,通过 git log --name-status 即可查知,没有必要在 commit 的注释中体现。

  • git log --name-status 发现:

    M 表示 Modify
    R093 表示 Rename 相似度 93%
    D 表示删除
    A 表示新加的文件

  • git log --name-status --oneline
    查看提交的注释和文件列表;内置图形工具 git gui 和 gitk 当然更方便。

  • git log --name-status 查看指定文件的commit记录(即查看指定文件的变更记录)。当前目录下的指定文件,或者带上路径。
    git diff 12a6fa3..7984547 sv_minicurl/src/minicurl.c: 查看指定文件的修改内容。
    git diff 12a6fa3 sv_minicurl/src/minicurl.c: 查看指定文件从指定commit到目前的修改内容。

  • git checkout [commit ID] -- path/to/file: 将指定文件复原到指定commit的那个版本,然后直接 git commit 即可。

  • git rev-parse HEAD 取得当前HEAD的commit值。

  • git log -n 5
    只查看前5条。

  • git log --graph
    查看所有分支情况,简单、清晰图形标识分支;

  • git log --pretty=format:"%an %ad %s"
    自定义输出格式:author name, author date, subject。

  • git log --all --oneline **/
    git log --all --name-status **/**
    查找哪些提交中包含了指定文件;
    --name-status 列出具体的文件目录,**/ 表示任意路径;

  • git log --diff-filter=D --summary | grep delete
    Get a list of the deleted files and copy the full path of the deleted file.
    列出已删除文件列表;

  • git log --stat:加 --reverse 则从最早的开始显示,加 则只显示范围内的变更统计。
    列出变更的简略的统计数据:abbreviated stats for each commit.

git 查看修改历史或某个文件的修改历史?

  1. git log -p [a_commit]: --patch。只提供一个 commit,则表示列出这个commit及之前的所有改动。如果想显示从这个commit到最新改动则 a_commit..
    查看 diff。类似 git diff [commit]
  2. git log -p ..: 显示改动的内容;
    -p 参数 Generating patch text with -p。其中:.. 指明了范围 ;不指明的话则显示全部(最初到当下commit,即HEAD)。
  3. git log --follow --pretty=oneline <文件名>
    使用 git log 命令可列出指定文件的所有改动历史。由于一个文件的改动是有限次的,所以这里列出每次commit。查看文件历史,可以了解文件变迁。
  4. git log -p --follow -- 可以查看该文件整个历史每次的内容变更,包括改名以前的。如果不带 --follow,则不会看到改名前的变更历史。加上 --stat,则可以看到每次变更的加减行数。
    git blame 可以追溯该文件每行是谁写的。
  5. git show
  6. git show 可显示具体某个 object 的一次 commit、tag 的修改~
  7. git branch -a --contains ;查找某个commit所在的各个分支,这些分支都包含这个commit。
  8. git log master.. --oneline | tail -1:查看某个分支 branch_name 从 master 分支出来后,提交的第一个commit。查看分支偏离主干情况。

图形化展示当前分支拓扑:Visualizing branch topology in Git

  • 查看各个分支的起源、分支关系等。这个回答 非常好,在配置文件 .gitconfig 建别名 alias lg和hist,展示相当清晰,且使用命令方便。
   [Alias]
     lg = log --graph --pretty=format:'%Cred%h%Creset %ad %s %C(yellow)%d%Creset %C(bold blue)<%an>%Creset' --date=short
     hist = log --graph --full-history --all --pretty=format:'%Cred%h%Creset %ad %s %C(yellow)%d%Creset %C(bold blue)<%an>%Creset' --date=short
  • 查看两个 commit 之间提交的次数:git log 37efe84..2c7fe25 --pretty=oneline | wc -l

git submodule

  • Using Git Submodules Effectively:可以看看。

    Git submodules are useful when you want to share code that you also need change along with the consumer of that code.
    使用 submodules 有额外的复杂性,需要管理好复杂性。共享代码和工程代码可同时修改的情形,适合使用 submodules。

git tag

  • Get the commit hash for a tag: 加 -m 的 tag 叫 annotated tag,他自己有独立的内容存储区及SHA1。她实际指向的那个 commit(underlaying commit)要通过 git rev-parse tag^{} 才能看到背后指向的那个实体 commit。git rev-parse tag 看到的是自己的。unannotated tag 两个是一样的,只是她背后那个 commit 的一个别名。
  • git tag -m [] 打 tag。-m 表示 tag 备注,必须加上,多个表示多个段落。
    对本地当前分支的当前 commit 打标签,如:git tag -m '修复日期显示问题' v2.0.609。
  • git tag -n --sort=-taggerdate | head -3 : 最近签入的 3个 tag(按 taggerdate 倒序)。git tag sorted in chronological order of the date of the commit pointed to。
  • git ls-remote --tags origin 查看远程标签列表;
  • git log -1 显示 tag 或者 commit 的信息,其中是数字 1。
    git rev-list -n 1 :列出 tag 指向的 commit;
    git log -1 --format=%cs 显示 tag 或者 commit 的日期 YYYY-MM-DD。
    git tag -l |xargs -n1 git log -1:列出当下每个 tag 对应的提交信息。
  • git show 显示 commit 的信息,文件改动等。
  • git tag -n 查看标签注释;-n 等同于 -n1,可以显示多行注释,比如 -n3。
  • git tag -d 删除标签;
  • git tag -l 查看本地标签列表 list;
  • git log --pretty=oneline 查看该 tag(或 commit) 及之前的 每个 commit。
  • git log --pretty=oneline tagVersion1...tagVersion2 查看 tagVersion2 之前直到 tagVersion1 的所有 commit。
  • git push origin tag v2.0.609
    本地建好标签后,将标签推送到远程。
    git push tag
    tag means the same as refs/tags/:refs/tags/. 冒号前面的是来源 src,后面的是目标 dst。
git push  
git push   
git push   refs/tags/

Pushing an empty allows you to delete the ref from the remote repository.

  • git push origin :old_tag 删除远程标签
  • git push origin :old_branch 删除远程分支
  • git push :refs/tags/ 删除远程分支的标签;
  • git fetch --prune 和远程同步,清理本地存储的远程分支信息
    -p,--prune,After fetching, remove any remote-tracking references that no longer exist on the remote.
    如果有人清理了远程 repo 标签,你 git pull 后并不会同步清理本地记录的那些标签名;请使用该命令清理那些在远程并不存在的refs;
  • 远程仓库回滚到上一次
    $ git reflog 查看日志。
    $ git reset 回滚到。(这个commit 之后所做的修改都保留在工作区,即默认 --soft。回滚到上次就是 git reset HEAD^),撤销在本地commit的内容。
    $ git push origin HEAD --force 推送到远程。慎用。
  • What is the difference between an annotated tag and a lightweight tag?
Tag objects (created with -a, -s, or -u) are called "annotated" tags; they contain a creation date, the tagger name and e-mail, a tagging message, and an optional GnuPG signature. Whereas a "lightweight" tag is simply a name for an object (usually a commit object).
Annotated tags are meant for release while lightweight tags are meant for private or temporary object labels. For this reason, some git commands for naming objects (like git describe) will ignore lightweight tags by default.

参考:Git 分支与整合策略。

  • git fetch;
  • git fetch --prune --prune-tags(自版本 2.17 支持)

Before fetching, remove any local tags that no longer exist on the remote if --prune is enabled. This option should be used more carefully.


git fetch --prune --prune-tags

你可能感兴趣的:(git log、git tag、git branch)