git log止潘、git tag、git branch

Git-scm: Git tools - submodules

新建發(fā)行版 create a release

  • Do Git tags only apply to the current branch?: tag 和 branch 標簽和分支什么關(guān)系尼桶?
    tag 和 branch 沒任何關(guān)系,tag 只是一個指向一個 commit 的指針锯仪。git show <tag> 列出的就是這個 tag 指向的 commit 的信息泵督。
  • git describe [--tags] <commit> describes the current branch in terms of the commits since the most recent [possibly lightweight] tag in this branch's history. 列出指定 commit 和 最近 tag 的 基本關(guān)系。

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 diff

  • git diff --name-only <Start Commit ID>..<End Commit ID>: 顯示變更了哪些文件斥扛。這在合并分支時入问,方便查看修改文件列表。
    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 輸出界面上的每一行內(nèi)容的含義,詳實峻村。

git log

  • git log 用于查看每個提交的日志(Show commit logs)麸折。從 Viewing the Commit History 開啟了解 git log 的新視角。
  • 好的注釋可以讓人一下子就了解本次修改的目的粘昨,比如修改了哪個BUG垢啼,解決了什么問題,實現(xiàn)了什么功能张肾。
  • git log --reverse: 從第一個 commit 開始芭析,默認是看最新的 commit。

至于具體修改了哪些文件吞瞪,通過 git log --name-status 即可查知馁启,沒有必要在 commit 的注釋中體現(xiàn)。

  • git log --name-status 發(fā)現(xiàn):

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

  • git log --name-status --oneline
    查看提交的注釋和文件列表芍秆;內(nèi)置圖形工具 git guigitk 當然更方便惯疙。

  • git log --name-status <filename> 查看指定文件的commit記錄(即查看指定文件的變更記錄)。當前目錄下的指定文件妖啥,或者帶上路徑霉颠。
    git diff 12a6fa3..7984547 sv_minicurl/src/minicurl.c: 查看指定文件的修改內(nèi)容。
    git diff 12a6fa3 sv_minicurl/src/minicurl.c: 查看指定文件從指定commit到目前的修改內(nèi)容荆虱。

  • 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 **/<filename>
    git log --all --name-status **/*<partoffilename>*
    查找哪些提交中包含了指定文件;
    --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 則從最早的開始顯示寝优,加 <revision-range> 則只顯示范圍內(nèi)的變更統(tǒng)計。
    列出變更的簡略的統(tǒng)計數(shù)據(jù):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 <Start Commit ID>..<End Commit ID>: 顯示改動的內(nèi)容铅协;
    -p 參數(shù) Generating patch text with -p捷沸。其中:<Start Commit ID>..<End Commit ID> 指明了范圍 <revision-range>;不指明的話則顯示全部(最初到當下commit狐史,即HEAD)痒给。
  3. git log --follow --pretty=oneline <文件名>
    使用 git log 命令可列出指定文件的所有改動歷史。由于一個文件的改動是有限次的骏全,所以這里列出每次commit苍柏。查看文件歷史,可以了解文件變遷姜贡。
  4. git log -p --follow -- <file> 可以查看該文件整個歷史每次的內(nèi)容變更试吁,包括改名以前的。如果不帶 --follow楼咳,則不會看到改名前的變更歷史熄捍。加上 --stat,則可以看到每次變更的加減行數(shù)母怜。
    git blame <file> 可以追溯該文件每行是誰寫的余耽。
  5. git show <commit-id|tag|branch>
  6. git show 可顯示具體某個 object 的一次 commit、tag 的修改~
  7. git branch -a --contains <commit>糙申;查找某個commit所在的各個分支宾添,這些分支都包含這個commit船惨。
  8. git log master..<branch_name> --oneline | tail -1:查看某個分支 branch_name 從 master 分支出來后,提交的第一個commit缕陕,即查看該分支的起源粱锐。查看分支偏離主干情況。

圖形化展示當前分支拓撲:Visualizing branch topology in Git

  • 查看各個分支的起源扛邑、分支關(guān)系等怜浅。這個回答 非常好,在配置文件 .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

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功炮,他自己有獨立的內(nèi)容存儲區(qū)及SHA1溅潜。她實際指向的那個 commit(underlaying commit)要通過 git rev-parse tag^{} 才能看到背后指向的那個實體 commit。git rev-parse tag 看到的是自己的薪伏。unannotated tag 兩個是一樣的滚澜,只是她背后那個 commit 的一個別名。
  • git tag -m <msg> <tag-name> [<commit>] 打 tag嫁怀。-m <msg> 表示 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> 顯示 tag 或者 commit 的信息召噩,其中是數(shù)字 1母赵。
    git rev-list -n 1 <tag-name>:列出 tag 指向的 commit;
    git log -1 --format=%cs <tag|commit> 顯示 tag 或者 commit 的日期 YYYY-MM-DD具滴。
    git tag -l |xargs -n1 git log -1:列出當下每個 tag 對應(yīng)的提交信息凹嘲。
  • git show <tag|commit> 顯示 commit 的信息,文件改動等构韵。
  • git tag -n <tag> 查看標簽注釋周蹭;-n 等同于 -n1趋艘,可以顯示多行注釋,比如 -n3凶朗。
  • git tag -d <tag> 刪除標簽瓷胧;
  • git tag -l 查看本地標簽列表 list;
  • git log --pretty=oneline <tagname or commit> 查看該 tag(或 commit) 及之前的 每個 commit棚愤。
  • git log --pretty=oneline tagVersion1...tagVersion2 查看 tagVersion2 之前直到 tagVersion1 的所有 commit搓萧。
  • git push origin tag v2.0.609
    本地建好標簽后,將標簽推送到遠程宛畦。
    git push <remote> tag <tag>
    tag <tag> means the same as refs/tags/<tag>:refs/tags/<tag>. 冒號前面的是來源 src瘸洛,后面的是目標 dst。
git push <remote> <tag>
git push <remote> <branch> <tag>
git push <remote> <branch> refs/tags/<tag>

Pushing an empty <src> allows you to delete the <dst> ref from the remote repository.

  • git push origin :old_tag 刪除遠程標簽
  • git push origin :old_branch 刪除遠程分支
  • git push <remote> <branch> :refs/tags/<tag> 刪除遠程分支的標簽次和;
  • 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> 回滾到<commit>燃少。(這個commit 之后所做的修改都保留在工作區(qū)束亏,即默認 --soft≌缶撸回滾到上次就是 git reset HEAD^)碍遍,撤銷在本地commit的內(nèi)容。
    $ 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末东跪,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鹰溜,更是在濱河造成了極大的恐慌虽填,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,204評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件曹动,死亡現(xiàn)場離奇詭異斋日,居然都是意外死亡,警方通過查閱死者的電腦和手機墓陈,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評論 3 395
  • 文/潘曉璐 我一進店門恶守,熙熙樓的掌柜王于貴愁眉苦臉地迎上來第献,“玉大人,你說我怎么就攤上這事兔港∮购粒” “怎么了?”我有些...
    開封第一講書人閱讀 164,548評論 0 354
  • 文/不壞的土叔 我叫張陵押框,是天一觀的道長岔绸。 經(jīng)常有香客問我,道長橡伞,這世上最難降的妖魔是什么盒揉? 我笑而不...
    開封第一講書人閱讀 58,657評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮兑徘,結(jié)果婚禮上刚盈,老公的妹妹穿的比我還像新娘。我一直安慰自己挂脑,他們只是感情好藕漱,可當我...
    茶點故事閱讀 67,689評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著崭闲,像睡著了一般肋联。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上刁俭,一...
    開封第一講書人閱讀 51,554評論 1 305
  • 那天橄仍,我揣著相機與錄音,去河邊找鬼牍戚。 笑死侮繁,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的如孝。 我是一名探鬼主播宪哩,決...
    沈念sama閱讀 40,302評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼第晰!你這毒婦竟也來了锁孟?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,216評論 0 276
  • 序言:老撾萬榮一對情侶失蹤茁瘦,失蹤者是張志新(化名)和其女友劉穎罗岖,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腹躁,經(jīng)...
    沈念sama閱讀 45,661評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡桑包,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,851評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了纺非。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哑了。...
    茶點故事閱讀 39,977評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡赘方,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出弱左,到底是詐尸還是另有隱情窄陡,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評論 5 347
  • 正文 年R本政府宣布拆火,位于F島的核電站跳夭,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏们镜。R本人自食惡果不足惜币叹,卻給世界環(huán)境...
    茶點故事閱讀 41,306評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望模狭。 院中可真熱鬧颈抚,春花似錦、人聲如沸嚼鹉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,898評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽锚赤。三九已至匹舞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間线脚,已是汗流浹背赐稽。 一陣腳步聲響...
    開封第一講書人閱讀 33,019評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留酒贬,地道東北人。 一個月前我還...
    沈念sama閱讀 48,138評論 3 370
  • 正文 我出身青樓翠霍,卻偏偏與公主長得像锭吨,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子寒匙,可洞房花燭夜當晚...
    茶點故事閱讀 44,927評論 2 355

推薦閱讀更多精彩內(nèi)容