DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Git Submodule的同步问题


获取更新

git submodule foreach --recursive 'git fetch --all'

列出remote比local新的submodule和所在分支

git submodule foreach --recursive 'git branch --all -vv'|grep behind -B1

查看最新的提交在哪个分支上

git submodule foreach --recursive 'git for-each-ref --sort=-committerdate --count=1'

查询local branch与remote branch之间的关系

这一步的目的是为了避免后面做git pull --all时无法确定merge目标branch.

git submodule foreach --recursive 'git branch --all -vv'

如果一个repository早已设置好remote和local之间的对应关系(见Note2), 则可以跳过这一步。

显示origin中每个branch的情况:

git submodule foreach --recursive 'git remote show origin'

同步到本地分支上

git submodule foreach --recursive 'git pull --all'

如果上面的命令由于Git无法确定merge目标失败,先执行:

git submodule foreach --recursive 'git checkout master'

再执行git pull命令。

回退

任何时候如果想放弃本地所做的修改,用git submodule update返回到初始状态。

切换到工作分支

git submodule foreach --recursive 'git checkout $(git for-each-ref --count=1 --sort=-committerdate| grep -oE "[^/]+$")'

其中git for-each-ref拿到最新的一次commit及其所在分支, 用grep命令得到分支名称,在checkout出此分支。

参考:

http://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit

http://stackoverflow.com/questions/3162385/how-to-split-a-string-in-shell-and-get-the-last-field

Note

Note1

如果希望查询某个时间点后的提交:

git --no-pager submodule foreach --recursive 'git log --since="2016-03-05" --pretty=oneline --all --decorate'

这里的--no-pager避免git log进入"pager"(即类似于vim或者less的全屏风格)模式。

Note2

Local branch与remote branch的关系实际上是定义在.git/config文件中的, 对于Meteor package, 这个文件是 "/.git/modules/packages//config",例如:

[branch "master"]
  remote = origin
  merge = refs/heads/master


Published

Mar 17, 2016

Last Updated

Mar 17, 2016

Category

Tech

Tags

  • git 36
  • submodule 3
  • sync 6

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor