我想将本地存储库与远程存储库同步,以便本地存储库成为远程存储库的100%副本——这意味着如果这些存储库中的某些文件不同,我们将用远程存储库覆盖本地存储库,如果本地存储库中的文件在远程存储库中不存在,则本地文件将被删除。
除了重新克隆远程存储库之外,还有什么方法可以实现这一点吗?
类似的问题是一次性将本地git回购与远程同步,丢弃本地更改/提交。
我想将本地存储库与远程存储库同步,以便本地存储库成为远程存储库的100%副本——这意味着如果这些存储库中的某些文件不同,我们将用远程存储库覆盖本地存储库,如果本地存储库中的文件在远程存储库中不存在,则本地文件将被删除。
除了重新克隆远程存储库之外,还有什么方法可以实现这一点吗?
类似的问题是一次性将本地git回购与远程同步,丢弃本地更改/提交。
当前回答
你想做的是
git fetch --prune origin
git reset --hard origin/master
git clean -f -d
这使得您的本地回购完全像您的远程回购。
请记住用您想要同步的远程和分支来替换原点和主节点。
其他回答
如果你想在远程上创建一个新的分支来镜像和跟踪你的本地分支(反之亦然),永久的修复方法是:
git config --global push.default current
在我克隆git之后,我总是用这个命令配置我的本地git。尽管它可以在本地-远程分支“Git fatal: the current branch has no upstream branch”错误发生时随时应用。
您需要了解Git存储库不仅仅是目录和文件的树,而且还存储这些树的历史—其中可能包含分支和合并。
当从存储库中获取时,您将把所有或部分分支复制到存储库中。然后在你的存储库中作为“远程跟踪分支”,例如,命名为remotes/origin/master之类的分支。
从远程存储库获取新的提交不会改变本地工作副本的任何内容。
工作副本通常签出一个提交,称为HEAD。这个提交通常是某个本地分支的提示。
我认为您希望将您的本地分支(或者可能是所有本地分支?)更新到相应的远程分支,然后签出最新的分支。
为了避免与工作副本发生冲突(可能有本地更改),首先清理所有没有版本控制的内容(使用git clean)。然后签出与要更新的远程分支对应的本地分支,并使用git reset将其切换到获取的远程分支。(git pull会将远程分支的所有更新合并到本地分支中,如果你有本地提交,也可以创建合并提交。)
(但是这样你就会丢失所有的本地更改——包括工作副本和本地提交。确保你真的想要这个-否则最好使用一个新的分支,这将保存你的本地提交。并使用git stash保存尚未提交的更改。)
编辑: 如果您只有一个本地分支,并且正在跟踪一个远程分支,那么您所需要做的就是
git pull
从工作目录内部。
这将获取所有被跟踪的远程分支的当前版本,并将当前分支(和工作目录)更新到它所跟踪的远程分支的当前版本。
如果你正在谈论同步一个分叉回购,那么你可以按照这些步骤。
如何从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.
(此信息来自Git用户手册)
我也在学习,所以这可能不是问题的确切答案,但它可能会帮助一些人:
When a remote repository is initially cloned copies of all branches are stored in your local repository (view them with git branch -r) To update these copies and make them current (i.e. sync them with the remote branch) use git fetch. This will not effect any of you existing, custom created branches. To override your local branch checkout a fresh version of whatever branch you are working on (Assuming that you have already executed git add origin /path/to/repository) use git checkout origin/branch_name, this will override your locals changes on branch branch_name
以下步骤可以做到:
git reset --hard HEAD
git clean -f -x -d -n
然后没有-n
这将处理所有本地更改。现在提交。
git status
注意下面这句话:
Your branch is ahead of 'xxxx' by N commits.
记下数字“N” 现在:
git reset --hard HEAD~N
git pull
最后:
git status
应该没有显示要添加/提交的内容。 所有的清洁。
然而,一个新的克隆也可以做到这一点(但速度要慢得多)。
= = = = = =更新
随着时间的推移,我的git知识略有提高,我想出了另一种更简单的方法来做同样的事情。下面是如何做的(并加以解释)。在你的工作部门:
git fetch # This updates 'remote' portion of local repo.
git reset --hard origin/<your-working-branch>
# this will sync your local copy with remote content, discarding any committed
# or uncommitted changes.
虽然您的本地提交和更改将在此之后从视野中消失,但如果需要,可以恢复已提交的更改。