Skip to main content

Tedshd's Dev note

Git -Tip & Note

Table of Contents

# 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”