category: git

Git -Tip & Note

on 2014-07-01

Git -Tip & Note

git pull 出現 CONFLICT 要回復到 pull 之前

git reset --hard <commit id>

強制 push 取代 remote

git push -f

取消 merage (須知道 merage 前的 commit id)

git reset --hard <commit id>

拉 remote 的新 branch 到 local(local 不存在該 branch)

git checkout --track -b <local new branch> <remote branch>

不小心把 remote & local 的 branch 刪掉

先用 git reflog

git reflog

# show
# df4bb25 HEAD@{0}: checkout: moving from master to dev
# df4bb25 HEAD@{1}: checkout: moving from webtv_dev to master
# 9e920fb HEAD@{2}: commit: [Dev] Modify keycode to keyboard.
# 16b9a83 HEAD@{3}: checkout: moving from webtv to webtv_dev
# 16b9a83 HEAD@{4}: checkout: moving from master to webtv
# df4bb25 HEAD@{5}: checkout: moving from webtv to master
# 16b9a83 HEAD@{6}: commit: Responsive video list.
# 5eee322 HEAD@{7}: commit: Modify ico_enter to SVG.
# ...

找到要回復的 commit id, 再把它重建起來

git checkout -b <new branch name> <git commit id>

Git 版本控制系統(2) 開 branch 分支和操作遠端 repo. 30 天精通 Git 版本控管 (16):善用版本日誌 git reflog 追蹤變更軌跡

delete remote branch

git push origin --delete <branchName>

git log file

git log -p <file name>

view remote log

git log origin/master

push commit id

Need follow commit log order

git push <remotename> <commit SHA>:<remotebranchname>
# example
git push origin 712acff81033eddc90bb2b45e1e4cd031fefc50f:master

push local branch to remote

git push -u origin <branch-name>

修改 commit

修改最後一次 commit

在 push 前處理

git commit --amend

修改之前的 commit(使用 rebase)

git rebase -i HEAD~7
pick d32cf12 Add a README file
pick d120793 Update year and copyright holders
pick 6a78547 Add agenda section in README
pick 1b263b5 br2 add file.
pick ee5f588 br2 Add again.
pick 8c19bf6 [test] Add readme add22.
pick 71bf708 Modify br2.txt2.

# Rebase 505b0cf..71bf708 onto 505b0cf (7 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

利用 r 處理要改的 commit

pick d32cf12 Add a README file
pick d120793 Update year and copyright holders
pick 6a78547 Add agenda section in README
r 1b263b5 br2 add file.
pick ee5f588 br2 Add again.
pick 8c19bf6 [test] Add readme add22.
pick 71bf708 Modify br2.txt2.

# Rebase 505b0cf..71bf708 onto 505b0cf (7 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

這操作會修改所有之後的 commit 因為相當於重新 commit, 建議在 push 前處理

Refer - Git 工具 - 重寫歷史

清理不存在的 remote branch

有時 remote 協作的 branch 砍了

但是 git branch -r 還在就需要清理一下

git remote prune origin

check branch is create from

git reflog --date=local <branch>

移除 commit

有時要捨棄最後一次 commit

那就可以使用

git reset <commit id>^

就會切到該 commit 的修改狀態

reset 有參數可以控制是否要直接連同該 commit 的變動丟掉還是回到修改狀態

這時在自己決定即可

如果已經 push 的話就很麻煩

必須維持在最後一筆且 remote 得是和 local 的 commit 一樣

git push origin +<branch name>

強制推上去

如果 remote 有別的 commit 更新就需要調整 commit 再來處理以避免強制推送時蓋掉最新的 commit

ref - 【狀況題】剛才的 Commit 後悔了,想要拆掉重做… ref - How to delete a commit in git, local and remote ref - git - Git Force推送语法,“ - f”与“+ branch”

Read more

Git - command

on 2013-09-20

Git - command

git status
# 查看修改哪些檔案

git diff
# 比對修改了的地方與上一個 commit 有何不同
git diff <file>
# 比對修改的檔案與上一個 commit 有何不同
git diff <old commit id>..<new commit id>
# 比對不同 commit id 的檔案差異

git coheckout -- <file>
# 把檔案回復到修改前的樣子

git add <file>
# 加入要 commit 的檔案

git rm <file>
# 移除檔案(有 commit 過的檔案要移除, rm <file> 之後要執行的指令)

git reset HEAD <file>
# 把 git add 之後的檔案回復到 git add 前

git commit -m '註解'
# or
git commit
# commit 檔案進入版本控管系統
git commit --no-verify
# 不驗證 commit

git log
# git commit log 紀錄
git log -p
# 詳細的log紀錄(修改哪些部分)

git blame <file>
# 查看修改的紀錄

git clone <repository>
# 拉 repository 下來到 local 端

git branch <branch name>
# 建立一個 local 端的 branch

git checkout <branch>
# 切換到該 branch

git branch
# 查看本地端的 branch

git branch -r
# 查看 remote 端的 branch

git stash
# 檔案 不commit 跳 branch (暫存)
git stash pop
# 回復檔案不 commit 狀態(讀回)

git branch -d <local branch>
# 刪除本地端的 branch

git branch -r -d <remote branch>
# 刪除 remote 端的 branch

git checkout --track -b <local branch> origin/<remote branch>
# 將遠端的 branch 並在 local 建立 branch

Refer Refer

Read more