git checkout main git pull origin main git checkout feature/my-new-feature git merge main
代码拉取冲突处理
1、保留并继续合并代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
localhost:bookstore liaoxb$ git pull Updating d369e85..67e8e8a error: Your local changes to the following files would be overwritten by merge: test.txt Please commit your changes or stash them before you merge. Aborting localhost:bookstore liaoxb$ localhost:bookstore liaoxb$ echo 'local change test' > test.txt localhost:bookstore liaoxb$ git pull Updating d369e85..67e8e8a error: Your local changes to the following files would be overwritten by merge: test.txt Please commit your changes or stash them before you merge. Aborting
git stash 暂存本地的代码修改 git pull 拉取远端最新代码 git stash pop 合并暂存的本地代码,解决已冲突的文件
1 2 3 4 5 6 7 8 9 10
localhost:bookstore liaoxb$ git stash Saved working directory and index state WIP on master: d369e85 update localhost:bookstore liaoxb$ git pull Updating d369e85..67e8e8a Fast-forward test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) localhost:bookstore liaoxb$ git stash pop Auto-merging test.txt CONFLICT (content): Merge conflict in test.txt
2、远端覆盖本地代码
git reset –hard 回滚到上个版本 git pull
1 2 3 4 5 6 7
localhost:bookstore liaoxb$ git reset --hard HEAD is now at d369e85 update localhost:bookstore liaoxb$ git pull Updating d369e85..67e8e8a Fast-forward test.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
# 创建新分支,同时切换到bugfix1 localhost:bookstore liaoxb$ git pull Already up to date. localhost:bookstore liaoxb$ git checkout -b bugfix1 Switched to a new branch 'bugfix1'
# 修改并提交代码 localhost:bookstore liaoxb$ echo'bugfix test' > test.txt localhost:bookstore liaoxb$ git add . localhost:bookstore liaoxb$ git commit -m "bugfix test" [bugfix1 2eafe98] bugfix test 1 file changed, 1 insertion(+), 3 deletions(-) # 新分支push到远端 localhost:bookstore liaoxb$ git push --set-upstream origin bugfix1 Counting objects: 3, done. Delta compression using up to 8 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 259 bytes | 259.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Powered by GITEE.COM [GNK-3.0] remote: Create a pull request for'bugfix1' on Gitee by visiting: remote: https://gitee.com/wisecloud/bookstore/pull/new/wisecloud:bugfix1...wisecloud:master To https://gitee.com/wisecloud/bookstore.git * [new branch] bugfix1 -> bugfix1 Branch 'bugfix1'set up to track remote branch 'bugfix1' from 'origin'.
# 切至合并分支 localhost:bookstore liaoxb$ git branch * bugfix1 master localhost:bookstore liaoxb$ git checkout master Switched to branch 'master' Your branch is up to date with 'origin/master'.