git command 筆記(一)

git command 筆記(一)


----------------------------------------------------------------------
Git學習教程(5)Git tag {{{1
git push origin :refs/tags/v0.7.5

----------------------------------------------------------------------
Git學習教程(六)Git 日誌 {{{1

查看每一次提交的補丁內容
    git log -p
    --顯示每一次提交與其父節點提交內容之間的快照差異

    git log dir/
    :!git show commit-id

--    !command             Execute the shell command with $SHELL.

查看統計數字,添加或刪除了多少行
    git log --stat

調整顯示格式:
    git log --pretty=format
    --將提交歷史顯示成你想要的格式:oneline, short, medium, full, fuler, email, raw

分支拓撲圖:
    git log --pretty=oneline --graph
    --顯示提交歷史及圖像化分支拓撲

    git log --graph
    git log --graph --decorate --pretty=oneline --abbrev-commit --all


利用提交查詢過濾器,查詢符合某些條件的提交信息:
日期區間:
    git log --before="2 weeks ago" --after="2016-05-20" --pretty=oneline
    --查看5月20號之後,2個星期之前的提交內容

貢獻者過濾器:
    --查看某個作者發起的提交:--author --commiter
    git log --author=liu,shuang --since="14 days ago" --pretty=oneline
    --查找作者在過去兩週內的所有提交條目

可以指定完整人名或email地址來搜索
    git log [email protected]

查找提交信息:
    git log --grep='C90' --pretty=oneline
    --搜索提交信息中有C90的所有提交內容

查看指定文件的提交歷史:
    git log --pretty=oneline -- .vimrc
查看目錄或文件的提交歷史
    git log --pretty=oneline -- t/lib-httpd/ notes.c

查看未合併的提交歷史記錄
    git log --grep='c90' --pretty=oneline --no-merges

-N 查看滿足條件的最近N條歷史記錄

查看兩條提交信息之間的提交歷史:
    git log --pretty=oneline 710f0f..8a5cbc
    --commit號710f0f(前6位)到commit號8a5cbc之間的提交歷史
查找一個分支上的提交時:
    目前在'master'分支上,想查看'experiment'分支上還沒有合併的提交記錄:
    git log master..experiment --pretty=oneline
    --告訴你,如果現在合併的話,所有列出的這些提交都會被合併
    可以不寫某一端的分支名,git會判斷目前在哪個分支上。
    git log ..experiment --pretty=oneline

    如果在experiment分支上,也想看到相同的信息,即還沒有合併到master的提交:
    git log master.. --pretty=oneline

----------------------------------------------------------------------
Git學習教程(七)Git差異比對 {{{1

git diff    查看變更還未載入(還未git add)的文件差異
git diff --stage/--cached   查看載入並未提交的變更差異
git diff HEAD  顯示最後一次提交之後的所有變更。(包括變更的和未變更的)
git diff 'tag' -- README.md   查看tag標籤後,README.md文件所發生的修改
git diff v1.0 v2.0  兩次提交的差異比對
git diff v1.0 v2.0  顯示兩個版本之間差異的統計數字
git diff v1.0 v2.0 -- file.c   顯示file.c在兩個版本之間的比對結果
git diff master...dev  創建分支dev之後,在這條分支上的差異對比 ,用dev與
                    master 所交的分岐點和現在dev分支上最後一個快照比對


You can quickly review the changes made to a file using the diff command:
git diff <commit hash> <filename>

----------------------------------------------------------------------
查看某次改動的具體修改:
    git show git提交版本號 文件名

    git show e17e782 --color=always | less -r

================================================================================
git branch

顯示本地分支及分支頭commit號、commit信息
git branch -v

顯示本地、遠程分支及分支頭commit號、commit信息
git branch -va

對應的顯示本地、遠程分支及分支頭commit號、commit信息
git branch -vv

獲取,映射遠程分支到本地:
(1)將遠程分支信息獲取到本地:
    git fetch

(2)將遠程分支映射到本地命名爲local-branchname的分支:
    git checkout -b local-branchname origin/remote_branchname

================================================================================
git revert {{{1
git revert commit-id

================================================================================
How to create and apply a patch with Git {{{1
git format-patch生成的Git專用Patch

If you fix a bug or create a new feature – do it in a separate branch!
FYI: I’m assuming you made a few commits in the fix_empty_poster branch and
did not yet merge it back in to the master branch.

--------------------------------------------------------------------------------
Creating the patch. {{{2
This will create a new file fix_empty_poster.patch with all changes from the
current (fix_empty_poster) against master.

    git format-patch master --stdout > fix_empty_poster.patch

>>針對每次提交生成一個patch
    git format-patch -M master
    -M選項表示這個patch要和那個分支比對
用git am來應用
    git am 0001-Fix1.patch
    git commit -a -m "PATCH apply"
--------------------------------------------------------------------------------
Applying the patch {{{2

(1) take a look at what changes are in the patch.
not apply the patch, but only shows you the stats about what it’ll do.
After peeking into the patch file with your favorite editor,
you can see what the actual changes are.

    git apply --stat fix_empty_poster.patch

(2) test the patch
If you don’t get any errors, the patch can be applied cleanly.

    git apply --check fix_empty_poster.patch

(3) apply the patch, I’ll use git am instead of git apply.
    (git am allows you to sign off an applied patch.)
    Patches were applied cleanly and your master branch has been updated.

    git am --signoff < fix_empty_poster.patch

================================================================================
git diff生成的標準patch {{{1

(1) 把git diff輸出變爲一個Patch
    git diff master > patch

(2) 使用git apply應用patch
    建立一個專門處理新交來的patch的分支:
    git checkout -b PATCH
    git apply patch
    git commit -a -m "Patch Apply"


================================================================================
git diff生成的標準patch {{{1
chmod a+x  .git/hooks/commit-msg

generate "Change-Id" to make it can be updated in one patch:
"Uploaded patch set 1."
"Uploaded patch set 2."
================================================================================
錯誤執行git commit --amend 之後想回退
git reflog
git reset commid

用 git reset HEAD <file>... 的方式取消暫存(取消git add)

git d: use vimdiff {{{1
in terminal:
git config --global diff.tool vimdiff
git config --global difftool.prompt false
git config --global alias.d difftool
// git d //open files to diff

git d{{{1
gitt d AmbaConfig
gitt d --cached  AmbaConfig

gui:
//git config --global diff.tool meld


Dealing with line endings{{{1
git diff 忽略換行符差異:

git config --global core.whitespace cr-at-eol

更好的方法是每個項目都有一個.gitattributes文件
note:
Windows用CR LF來定義換行,Linux用LF。
CR全稱是Carriage Return ,或者表示爲\r, 意思是回車。
LF全稱是Line Feed,它纔是真正意義上的換行表示符。
如果用git diff的時候看到^M字符,就說明兩個文件在換行符上有所差別。

Refreshing a repository after changing line endings

After you've set the core.autocrlf option and committed a .gitattributes file, you may find that Git wants to commit files that you have not modified. At this point, Git is eager to change the line endings of every file for you.

The best way to automatically configure your repository's line endings is to first backup your files with Git, delete every file in your repository (except the .git directory), and then restore the files all at once.

    Save your current files in Git, so that none of your work is lost.

    git add . -u
    git commit -m "Saving files before refreshing line endings"

    Remove the index and force Git to rescan the working directory.

    rm .git/index

    Rewrite the Git index to pick up all the new line endings.

    git reset

    Show the rewritten, normalized files.

    git status

    Add all your changed files back, and prepare them for a commit. This is your chance to inspect which files, if any, were unchanged.

    git add -u
    # It is perfectly safe to see a lot of messages here that read
    # "warning: CRLF will be replaced by LF in file."

    Rewrite the .gitattributes file.

    git add .gitattributes

    Commit the changes to your repository.

    git commit -m "Normalize all the line endings"

忽略文件權限或者擁有者改變導致的git狀態變化 {{{1
1) 在當前git倉庫下執行:
    git config core.filemode false
    git config --list
2) 對全局git庫生效
    git config --global core.filemode false
or:
    也可以在命令行下對文件進行編輯:
    vi ~/.gitconfig
    filemode = false

----
已經clone下來的項目,在使用全局設置後無用, 需要對當前項目做單獨設置?
need to check?
    git config core.filemode false
or:
    vi ~/xxx_prj/.git/config
    filemode = false

3)刪除配置
    git config --unset --global core.filemode false

git diff的時候忽略換行符的差異:{{{1
1)
    git config --global core.whitespace cr-at-eol
2)
更好的方法是每個項目都有一個.gitattributes文件,裏面配好了換行符的設置,參考
https://help.github.com/articles/dealing-with-line-endings

撤消 git add 操作{{{1
    use the following cmd to unstage
    git reset HEAD <file>...

Reset or revert a specific file to a specific revision using Git {{{1
(1)    Assuming the hash of the commit you want is c5f567:

    git checkout c5f567 -- file1/to/restore file2/to/restore

    The git checkout man page gives more information.
    If you want to revert to the commit before c5f567, append ~1 (works with any number):

        git checkout c5f567~1 -- file1/to/restore file2/to/restore

git合併分支上指定的commit {{{1
假設分支結構如下:
dd2e86 - 946992 - 9143a9 - a6fd86 - 5a6057 [master]
                  \
                   76cada-62ecb3-b886a0[feature]
(1) cherry pick 合併單個 commit:
    git checkout master
    git cherry-pick 62ecb3
    62ecb3 已經應用在 master 上了(作爲一個新的commit)。

(2) cherry pick 連續多個commit
合併多個要用 rebase。
假設想要把 76cada 和 62ecb3 合併到 master 上:
    git checkout -b newbranch 62ecb3
    git rebase --onto master 76cada^
    76cada^ 表示從 76cada 的 commit 開始合併(作爲新的commit)。
    這樣就完成了 76cada 到 62ecb3 合併到 master。

刪除遠程分支{{{1
    git push origin --delete Chapater6       
可以刪除遠程分支Chapater6

Git 修改commit message{{{1

1、 查看最近n次的提交
    git log --oneline -5
    查看最近5次commit的簡要信息,輸出信息爲:簡短commitID commit_message,

    git log -5,輸出相對詳細信息,完整commitID,加上--oneline查看簡短commitID

2、git rebase -i <簡短commitID>
    修改從上往下第n個commit_message,簡短commitID爲第n+1個.

    彈出窗口,顯示最近n次的提交信息.

3、要修改的提交前的pick改爲reword; 修改多個,將對應的多個pick改爲reword

4、保存退出; 5、在彈出窗口中,修改commit_message;
   修改多個pick爲reword,會多次彈出修改界面

5、查看修改結果
   git log --oneline -5
   or
   git log -5,

6、最後強制push上去git push --force


快捷操作:
---------
1,修改最近一次的commit 信息
git commit --amend

2,比如要修改的commit是倒數第三條,使用下述命令:
git rebase -i HEAD~3
    退出保存
3,執行 git rebase --continue
4,執行 git push -f 推送到服務端。

git 對比兩個分支 具體某個文件的差異{{{1
比較本地分支:
git diff branch1 branch2 --stat   //顯示出所有有差異的文件列表
git diff branch1 branch2 具體文件路徑   //顯示指定文件的詳細差異
git diff branch1 branch2                   //顯示出所有有差異的文件的詳細差異

git diff origin/master origin/Demo
遠程分支前面要加上remote名稱/
git diff local_branch origin/Demo


Git show-branch顯示提交信息{{{1
git show-branch
    1、輸出分爲上下兩部分,使用若干個短劃線”-“分隔。兩個分支使用兩個短劃線”–“,三個分支使用三個短劃線”—“,依次類推。
    2、上半部分爲層次縮進的分支列表,下半部分爲commit列表。
    3、上半部分的分支列表中,使用*標識當前分支,其他分支使用!標識(不同的分支!標識顏色不一樣)。分支前的標識符*或者!一直垂直貫通到下半部分,這一垂直列的符號都是屬於這個分支的。
    4、下半部分的commit列表中,前導的符號有*和+號。*表示這一列上的分支(當前分支)有此commit。而+表示這一列上的分支(非當前分支)有此commit。
    5、標識符的顏色只是用於容易區分列,一個分支一個顏色。

    使用git show-branch,查看某個使用 git branch branchName 或 git
    checkout -b branchName 開的分支的第一次提交。否則,直接使用 git
    log 是沒辦法找到該分支的第一次提交的情況的。

    

    另外一個查看分支什麼時候開的,或某個分支第一次提交的方法就是,在開分支的時候使用如下命令開分支:
    1 $ git checkout --orphan branchName
使用 --orphan 參數開分支時,要注意,新分支的文件都相當於新添加,且已add過的,
因此,在文件修改之前,需要先commit一次,否則第一次提交沒辦法和默認開分支時的文件進行對比。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章