Skip to content

Instantly share code, notes, and snippets.

@vitojeng
Last active October 17, 2017 07:50
Show Gist options
  • Save vitojeng/dcfe2d8f8b7517a916f4 to your computer and use it in GitHub Desktop.
Save vitojeng/dcfe2d8f8b7517a916f4 to your computer and use it in GitHub Desktop.
Git Memo

https://ihower.tw/blog/archives/5436

function git_branch {
    ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
    echo "("${ref#refs/heads/}") ";
}

function git_since_last_commit {
    now=`date +%s`;
    last_commit=$(git log --pretty=format:%at -1 2> /dev/null) || return;
    seconds_since_last_commit=$((now-last_commit));
    minutes_since_last_commit=$((seconds_since_last_commit/60));
    hours_since_last_commit=$((minutes_since_last_commit/60));
    minutes_since_last_commit=$((minutes_since_last_commit%60));
    
    echo "${hours_since_last_commit}h${minutes_since_last_commit}m ";
}

PS1="[\[\033[1;32m\]\w\[\033[0m\]] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0;33m\]\$(git_since_last_commit)\[\033[0m\]$ " 

比對指定的檔案

git diff myFile
git difftool myFile

difftool 是使用外部程式比對, 因此要使用 difftool, 須先完成設定

比對指定的資料夾

git difftool myDir/ [--dir-diff]

若不加上 --dir-diff, 則會循序地將資料夾內的檔案一一地做 file diff

比對整個分支(branch)

# 比對本地端的二個 branch
git difftool myBranch1..myBranch2 [--dir-diff]

# 與 remote branch 比對, 比對己經 commit, 但尚未 push 的部份
git difftool origin/master..HEAD [--dir-diff]
  • 若不加上 --dir-diff, 則會循序地將 branch 內的檔案一一地做 file diff
  • 沒有 stage 的不會進行比對

相關設定

Beyond Compare 4

在 ~/.gitconfig 內加入下列參數

[diff]
    tool = bc
[difftool "bc"]
    cmd = /usr/local/bin/bcomp -ro \"$LOCAL\" \"$REMOTE\"
	trustExitCode = true
[difftool]
    prompt = false

Beyond Compare 的設定

  • 在使用 difftool 開啟 BeyondCompare 後, 會發現許多檔案屬於 symbolic link, 會造成相同的檔案對不上, 因此需要設定 BeyondCompare
    • New window(Home) -> Sessions -> New(tree) -> Folder Compare(tree) -> Handling(tab):
      Follow symbolic links 打勾

參考資訊

查看尚未 push 到 remote 的 commits(Outgoing changes)

git log origin/master..HEAD
git log --oneline origin/master..HEAD
git log --stat origin/master..HEAD
git log --oneline @{u}..

加入 -- stat 會列出差異的檔案
Stackoverflow: Viewing Unpushed Git Commits

查看 remote repository 有增加的 commits(Incoming changes)

git log HEAD..origin/master
git log --oneline HEAD..origin/master
git log --stat HEAD..origin/master

# Incoming changes
git log --oneline ..@{u}

篩選 commits

### 篩選 message
git log --grep="t174" --oneline

### 篩選 author
git log --author="John"

### 篩選某日期後的 commits
git log --after="2014-7-1"
get log --after="yesterday"

### 篩選日期區間
git log --after="2014-7-1" --before="2014-7-4"

### 列出有某些檔案的 commits
git log --oneline -- file1 file2

Advanced Git Log

Rename branch

git branch -m old_branch new_branch         # Rename branch locally    
git push origin --delete old_branch         # Delete the old branch
git push --set-upstream origin new_branch   # Push the new branch, set local branch to track the new remote

Stackoverflow

Change commit messages

git commit --amend -m "New commit message"

Change commit message Github: Changing a commit message

Undo

Undo stage

# 指定要 unstage 的檔案
git reset HEAD -- path/to/file

Undo merge

Stackoverflow

git reset --merge ORIG_HEAD

Undo commit

Stackoverflow

$ git commit -m "Something terribly misguided"              (1)
$ git reset --soft HEAD~                                    (2)
<< edit files as necessary >>                               (3)
$ git add ...                                               (4)
$ git commit -c ORIG_HEAD                                   (5)

更改 author 的資訊(email & name)

參考這裡: https://help.github.com/articles/changing-author-info/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment