方案 1:使用 git stash(推荐)
这是最常用的方法,可以临时保存修改:
# 1. 保存当前修改
git stash# 2. 拉取远程代码
git pull# 3. 恢复你的修改
git stash pop
如果你想给 stash 添加描述信息:
git stash save "描述你的修改内容"
查看所有 stash:
git stash list
方案 2:先提交再拉取
如果你的修改已经完成,可以先提交:
# 1. 提交当前修改
git add .
git commit -m "你的提交信息"# 2. 拉取并合并远程代码
git pull
方案 3:使用 git pull --rebase
提交后使用 rebase 方式拉取,保持更清晰的提交历史:
# 1. 提交当前修改
git add .
git commit -m "你的提交信息"# 2. 以 rebase 方式拉取
git pull --rebase