git提交代码的方式
自己总结的两种git提交代码的方式,一种提交方式会产生两条commit记录,另一种提交则只会产生一条commit记录。
一、先commit再pull
- 将本地代码提交:
git commit -a -m "备注1"
; - 通过
git pull
拉取代码,如果存在冲突提示(例如:CONFLICT (content): Merge conflict in test.js
)继续往下看步骤3,否则直接通过git push
推送代码; - 通过命令
git mergetool
合并代码(按照提示操作); - 冲突解决完之后通过命令
git commit -a -m "备注2"
; - 最后通过
git push
推送代码。
此种代码提交方式会产生两条commit提交记录。
二、先stash再pull再stash pop再commit(推荐)
- 将本地修改的代码通过命令
git stash
暂存起来; - 通过
git pull
命令拉取最新代码; - 通过命令
git stash pop stash:{0}
命令将暂存起来的代码取出来,如果存在冲突提示(例如:CONFLICT (content): Merge conflict in test.js
)继续往下看步骤4,否则直接通过git commit -a -m "备注1"
和git push
提交代码; - 通过命令
git mergetool
合并代码(按照提示操作); - 冲突解决完之后通过命令
git commit -a -m "备注2"
; - 最后通过
git push
推送代码。
此种代码提交方式只会产生一条commit提交记录。推荐此方式!
当本地代码是干净的(即没有做任何修改)可以直接通过git pull
更新代码;
当本地代码有做修改,建议使用git fetch
和git merge
来更新代码;