使用 repo sync 命令来同步远端服务器的 Android 代码,如果本地修改了代码但还没有 commit,会提示无法 sync:
error: android/frameworks/base/: contains uncommitted changes
此时,可以使用 git reset 命令丢弃本地修改,然后再执行 repo sync 来同步代码。
如果想要不丢失本地修改,强制同步远端服务器代码,可以加上 -d 选项,repo sync -d 命令会将 HEAD 强制指向 repo manifest 版本,而忽略本地的改动。
查看 repo help sync 的帮助信息,对 -d 选项的说明如下:
-d, --detach
detach projects back to manifest revision
注意:加上 -d 选项只表示忽略本地改动,可以强制同步远端服务器的代码,但是本地修改的文件还是保持改动不变,不会强制覆盖掉本地修改。而且同步之后,本地的分支指向会发生变化,不再指向原来的分支。具体举例如下。
- 下面是执行 repo sync -d 之前的分支信息:
$ git branch * curent_branch_xxx
- 下面是执行 repo sync -d 之后的分支信息:
$ git branch * (detached from 715faf5) curent_branch_xxx
即,从远端服务器同步的代码,是同步到跟踪远端服务器的分支,还没有从 git 仓库把代码 checkout 到本地,而当前本地修改的代码处在未命名分支下,是不同的分支,互不干扰,才能在不丢弃本地修改的情况下,强制同步远端服务器代码。
- 执行 git status 命令,可以看到本地还是有修改过且还没有 commit 的文件,同步远端服务器代码后,并不会强制覆盖本地文件的修改:
$ git status HEAD detached at 715faf5 Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git checkout -- ..." to discard changes in working directory) modified: vendor/chioverride/default/g_pipelines.h modified: vendor/topology/g_usecase.xml
即,如果想要丢弃本地修改、让本地代码跟同步后的 git 仓库代码一致,repo sync -d 命令达不到这个效果。
另外,repo sync 有一个 --force-sync 选项,具体说明如下:
--force-sync
overwrite an existing git directory if it needs to point to a different object directory. WARNING: this may cause loss of data
从说明来看,像是可以强制同步,且可能丢失本地改动。但是实际测试发现,这个选项并不能强制覆盖本地的改动。如果本地文件发生改动,加上这个选项也是会 sync 报错:
$ repo sync --force-sync . Fetching project tools/ error: tools/: contains uncommitted changes
同时提供 -d 和 --force-sync 两个选项,还是不能强制覆盖本地修改。
目前没有找到 repo sync 命令可以强制覆盖本地修改的选项。