我想将本地存储库与远程存储库同步,以便本地存储库成为远程存储库的100%副本——这意味着如果这些存储库中的某些文件不同,我们将用远程存储库覆盖本地存储库,如果本地存储库中的文件在远程存储库中不存在,则本地文件将被删除。
除了重新克隆远程存储库之外,还有什么方法可以实现这一点吗?
类似的问题是一次性将本地git回购与远程同步,丢弃本地更改/提交。
我想将本地存储库与远程存储库同步,以便本地存储库成为远程存储库的100%副本——这意味着如果这些存储库中的某些文件不同,我们将用远程存储库覆盖本地存储库,如果本地存储库中的文件在远程存储库中不存在,则本地文件将被删除。
除了重新克隆远程存储库之外,还有什么方法可以实现这一点吗?
类似的问题是一次性将本地git回购与远程同步,丢弃本地更改/提交。
当前回答
如果你想在远程上创建一个新的分支来镜像和跟踪你的本地分支(反之亦然),永久的修复方法是:
git config --global push.default current
在我克隆git之后,我总是用这个命令配置我的本地git。尽管它可以在本地-远程分支“Git fatal: the current branch has no upstream branch”错误发生时随时应用。
其他回答
这个脚本将检查哪些本地远程不存在于您的远程并将它们从本地删除
git fetch --prune; git branch -vv | egrep -v "(\[origin\/[a-zA-Z0-9/_-]+\])" | awk "{print \$1}" | xargs git branch -D
你可以使用git钩子。只需创建一个钩子,在更新后将更改推到另一个回购。
当然,您可能会遇到合并冲突,因此您必须弄清楚如何处理它们。
你想做的是
git fetch --prune origin
git reset --hard origin/master
git clean -f -d
这使得您的本地回购完全像您的远程回购。
请记住用您想要同步的远程和分支来替换原点和主节点。
听起来好像你想要一个远程存储库的镜像:
git clone --mirror url://to/remote.git local.git
该命令创建一个裸存储库。如果您不想要一个裸露的存储库,事情就会变得更加复杂。
如果你正在谈论同步一个分叉回购,那么你可以按照这些步骤。
如何从git同步一个fork存储库
check your current git branch git branch checkout to master if you are not on master git checkout master Fetch the upstream repository if you have correct access rights git fetch upstream If you are getting below error then run git remote add upstream git@github.com:upstream_clone_repo_url/xyz.git fatal: 'upstream/master' does not appear to be a git repository fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. Now run the below command. git fetch upstream Now if you are on master then merge the upstream/master into master branch git merge upstream/master That's it!! Crosscheck via git remote command, more specific git remote -v If I also have commit rights to the upstream repo, I can create a local upstream branch and do work that will go upstream there.