[GIT] 將特定branch同步成特定commit

最常用到的狀況大概就是在新開的 branch 上做了很久,但是自以為是在 master 上,時間久了要 merge 卻是個眼花撩亂,只好直接拿這 branch 蓋掉 master。


Image 3
最糟糕的方式就是把所有 branch 都 fetch 下來且 track,把 branch的所有檔案先複製一份,切回 master 之後再一口氣覆蓋回來,然後commit & push

$ git branch -r | grep -v '\->' | while read remote; do git branch --track "${remote#origin/}" "$remote"; done
$ git fetch --all
$ git pull --all
$ git checkout branch_1
$ cp * ../bak/
$ git checkout master
$ cp ../bak/* ./
$ git add .
$ git commit -m "blahblah"
$ git push

但比較好的方式是用 git branch -f 強制

#先透過 git log 找到正確的 commit 編號
$ git log --graph --oneline
$ git branch -f master 9541155
$ git push -v

或者可以參考https://stackoverflow.com/questions/30105210/git-overwrite-master-with-branch