What's the equivalent of git clone, using git

2019-08-29 10:19发布

As currently I cannot clone repositories from a Bitbucket server, I've found I can still do a series of git init, git remote add etc. to get the repository on my computer. I would like to make sure I exactly produce the equivalent of a "clone". Is the following correct?

git init
git remote add origin -m master https://www.myserver.com/bitbucket/scm/proj/repo.git
# need to do git fetch twice, otherwise for some reason git branch -r 
# returns "warning: ignoring broken ref refs/remotes/origin/HEAD"
git fetch
git fetch
git branch -r
git checkout --track origin/master
git checkout --track origin/develop
git checkout --track origin/feature/myfeature
# (etc.. for all the branches I need to work with)

I'm also not sure about the flag -m in git remote is needed or potentially harmful.

Some debug info as suggested by @jthill :

Repository 1

git ls-remote --symref origin HEAD
ref: refs/heads/master  HEAD
842163b275ade3ec317543ed3a645f537d719766        HEAD

Repository 2

git ls-remote --symref origin HEAD
ref: refs/heads/master  HEAD
1a1044eef2d46a292305dfc10cf076a4cf1e9933        HEAD

1条回答
叛逆
2楼-- · 2019-08-29 11:09

That's it. The clone is the init, remote add and fetch part, after that you can e.g. git checkout master even before creating the local ref and git will set it up and auto-track the remote because that sequence is so common. The -m just bypasses what that bitbucket repo has as its own main branch and tells the local git what you want as yours, if master is what you want, it's what you want.

So the -m "should", near as I can figure, avoid whatever misconfiguration is going on with the origin's HEAD, and the second fetch "shouldn't" be necessary. If you'll include the results of git ls-remote --symref origin HEAD it'll probably be possible to identify what's causing the trouble, but I don't see much point, you've got a workaround.

查看更多
登录 后发表回答