1.拉取远程分支
1.1 git checkout
列出远程分支:
git branch -r
拉取并检出远程分支:
git checkout -b local-branch-name origin/remote-branch-name
1.2 git switch
新版本的Git,拉取并检出远程分支:
git switch -c local-branch-name origin/remote-branch-name
2.错误整理
2.1 修改未提交或存入暂存区
错误:
error: Your local changes to the following files would be overwritten by checkout: **** Please commit your changes or stash them before you switch branches. Aborting;
解决:提交、暂存、丢弃修改
提交修改:
git add . #或单个文件:test.go git commit -m "修改test.go"
暂存修改:
如果不希望提交这些修改,但也不想丢失,可以使用Git的stash功能将它们保存起来:
git stash # 现在可以安全地切换分支 git checkout <目标分支名> # 当你想要恢复这些修改时 git stash list # 查看暂存的列表 git stash pop # 恢复并删除最新的暂存 # 或者 git stash apply # 恢复暂存,但不删除
丢弃修改:
使用git checkout命令来重置文件到HEAD(即上一次提交的状态)。
单个文件:
git checkout -- test.go git checkout <目标分支名>
整个工作目录的修改:
git stash -u # 暂存包括未跟踪的文件(更安全的方式) # 或者 git reset --hard # 强制重置工作目录和暂存区到HEAD # 然后你可以切换分支 git checkout <目标分支名>