Git 同步上游代码

- blog tutorial hugo

有时候 Fork 了别人的项目,自己在提交修改之后,上游的项目也有提交更新,这时候再想同步更新上游更新应该怎么办呢,以 xinebf 在 Github 上 Fork yursan9 的一个主题为例记录操作过程。

# 将 Fork 的项目 clone 到本地
git clone [email protected]:xinebf/manis-hugo-theme.git
# 查看远程仓库
cd manis-hugo-theme
git remote -v
# 添加远程仓库
git remote add yursan9 https://github.com/yursan9/manis-hugo-theme.git
git remote -v
# 把上游更新 fetch 到本地
git fetch yursan9
# 查看各个分支最后一个提交对象的信息
git branch -av
# 切换到 master 分支,合并上游分支
git checkout master
git merge yursan9/master
# 如果有冲突的话,用上游分支替换本地分支
git reset --hard yursan9/master
# 提交本地分支到远程
git add .
git commit -m "merge upstream"
git push origin master

这样操作之后就能 fetch 上游更新 ✌。