将本地 Git 存储库与远程存储库同步
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6373277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Synchronizing a local Git repository with a remote one
提问by Axl
I want to synchronize my local repository with a remote one so that my local repository becomes a 100% copy of the remote one - meaning that if certain files differ in these repositories, we override the local ones with the remote ones, and if there are files in local repositories that do not exist in the remote, the local files get removed.
我想将我的本地存储库与远程存储库同步,以便我的本地存储库成为远程存储库的 100% 副本 - 这意味着如果这些存储库中的某些文件不同,我们将使用远程存储库覆盖本地存储库,如果有远程不存在的本地存储库中的文件,本地文件将被删除。
Is there any way to achieve that other than by doing a fresh clone of remote repository?
除了对远程存储库进行全新克隆之外,还有什么方法可以实现这一目标?
Similar question as Sync local git repo with remote in one shot discarding local changes/commits.
类似于Sync local git repo with remote in one shot 丢弃本地更改/提交的类似问题。
回答by jobwat
git fetch --prune
-p, --prune
After fetching, remove any remote-tracking branches which no longer exist on the remote. prune options
-p, --prune
获取后,删除远程上不再存在的任何远程跟踪分支。修剪选项
回答by FractalSpace
These steps will do it:
这些步骤将做到这一点:
git reset --hard HEAD
git clean -f -x -d -n
then without -n
然后没有 -n
This will take care of all local changes. Now the commits...
这将处理所有本地更改。现在提交...
git status
and note the line such as:
并注意以下行:
Your branch is ahead of 'xxxx' by N commits.
Take a note of number 'N' now:
现在记下数字“N”:
git reset --hard HEAD~N
git pull
and finally:
最后:
git status
should show nothing to add/commit. All clean.
不应该显示任何添加/提交。都干净。
However, a fresh clone can do the same (but is much slow).
然而,一个新的克隆可以做同样的事情(但速度很慢)。
===Updated===
===更新===
As my git knowledge slightly improved over the the time, I have come up with yet another simpler way to do the same. Here is how (#with explanation). While in your working branch:
随着时间的推移,我的 git 知识略有提高,我想出了另一种更简单的方法来做同样的事情。这是方法(#with 解释)。在您的工作分支中:
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.
Although your local commits and changes will disappear from sight after this, it is possible to recover committed changes, if necessary.
尽管在此之后您的本地提交和更改将消失,但如有必要,可以恢复已提交的更改。
回答by Pa?lo Ebermann
You need to understand that a Git repository is not just a tree of directories and files, but also stores a history of those trees - which might contain branches and merges.
您需要了解 Git 存储库不仅仅是目录和文件的树,还存储了这些树的历史记录 - 其中可能包含分支和合并。
When fetching from a repository, you will copy all or some of the branches there to your repository. These are then in your repository as "remote tracking branches", e.g. branches named like remotes/origin/master
or such.
从存储库中获取时,您会将那里的所有或部分分支复制到您的存储库。然后这些在您的存储库中作为“远程跟踪分支”,例如命名为 likeremotes/origin/master
等的分支。
Fetching new commits from the remote repository will not change anything about your local working copy.
从远程存储库获取新提交不会改变本地工作副本的任何内容。
Your working copy has normally a commit checked out, called HEAD
. This commit is usually the tip of one of your local branches.
您的工作副本通常会检出一个提交,称为HEAD
. 此提交通常是您本地分支之一的提示。
I think you want to update your local branch (or maybe all the local branches?) to the corresponding remote branch, and then check out the latest branch.
我认为您想将您的本地分支(或者可能是所有本地分支?)更新到相应的远程分支,然后查看最新的分支。
To avoid any conflicts with your working copy (which might have local changes), you first clean everything which is not versioned (using git clean
). Then you check out the local branch corresponding to the remote branch you want to update to, and use git reset
to switch it to the fetched remote branch. (git pull
will incorporate all updates of the remote branch in your local one, which might do the same, or create a merge commit if you have local commits.)
为避免与您的工作副本(可能有本地更改)发生任何冲突,您首先要清理所有未版本化的内容(使用git clean
)。然后你检出你想要更新到的远程分支对应的本地分支,并使用git reset
它来切换到获取的远程分支。(git pull
会将远程分支的所有更新合并到您的本地分支中,这可能会执行相同的操作,或者如果您有本地提交,则创建一个合并提交。)
(But then you will really lose any local changes - both in working copy and local commits. Make sure that you really want this - otherwise better use a new branch, this saves your local commits. And use git stash
to save changes which are not yet committed.)
(但是你真的会丢失任何本地更改 - 在工作副本和本地提交中。确保你真的想要这个 - 否则最好使用新分支,这会保存您的本地提交。并用于git stash
保存尚未提交的更改.)
Edit:If you have only one local branch and are tracking one remote branch, all you need to do is
编辑:如果您只有一个本地分支并且正在跟踪一个远程分支,那么您需要做的就是
git pull
from inside the working directory.
从工作目录内部。
This will fetch the current version of all tracked remote branches and update the current branch (and the working directory) to the current version of the remote branch it is tracking.
这将获取所有跟踪的远程分支的当前版本并将当前分支(和工作目录)更新为它正在跟踪的远程分支的当前版本。
回答by aandis
You want to do
你想做
git fetch --prune origin
git reset --hard origin/master
git clean -f -d
This makes your local repo exactly like your remote repo.
这使您的本地存储库与远程存储库完全相同。
Remember to replace origin and master with the remote and branch that you want to synchronize with.
请记住将 origin 和 master 替换为要与之同步的远程和分支。
回答by Mahendra Pratap
Reset and sync local repository with remote branch
重置本地存储库并将其与远程分支同步
The command: Remember to replace origin and master with the remote and branch that you want to synchronize with.
命令:记住将 origin 和 master 替换为要同步的远程和分支。
git fetch origin && git reset --hard origin/master && git clean -f -d
Or step-by-step:
或者一步一步:
git fetch origin
git reset --hard origin/master
git clean -f -d
Your local branch is now an exact copy (commits and all) of the remote branch.
您的本地分支现在是远程分支的精确副本(提交和全部)。
Command output:
命令输出:
Here is an example of running the command on a local clone of the Forge a git repository.
这是在 Forge a git 存储库的本地克隆上运行命令的示例。
sharkbook:forge lbaxter$ git fetch origin && git reset --hard origin/master && git clean -f -d
HEAD is now at 356cd85 FORGE-680
Removing forge-example-plugin/
Removing plugin-container-api/
Removing plugin-container/
Removing shell/.forge_settings
sharkbook:forge lbaxter$
回答by tjb
(This info is from The Git User's Manual)
(此信息来自The Git User's Manual)
I'm also learning, so this might not be exactly an answer to the question but it might help somebody:
我也在学习,所以这可能不完全是问题的答案,但它可能会帮助某人:
- 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
) usegit checkout origin/branch_name
, this will override your locals changes on branchbranch_name
- 当远程存储库最初被克隆时,所有分支的副本都存储在您的本地存储库中(查看它们
git branch -r
) - 要更新这些副本并使它们成为最新的(即与远程分支同步),请使用
git fetch
. 这不会影响任何现有的、自定义创建的分支。 - 要覆盖您的本地分支签出您正在处理的任何分支的新版本(假设您已经执行
git add origin /path/to/repository
)使用git checkout origin/branch_name
,这将覆盖您在分支上的本地更改branch_name
回答by Nishant Upadhyay
If you are talking about syncing a forked repo then you can follow these steps.
如果您正在谈论同步分叉存储库,那么您可以按照以下步骤操作。
How to sync a fork repository from 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 [email protected]: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 specificgit 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 分支
git branch
如果您不在 master 上,则结帐到 master
git checkout master
如果您具有正确的访问权限,则获取上游存储库
git fetch upstream
如果您遇到以下错误,请运行
git remote add upstream [email protected]: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.
现在运行以下命令。
git fetch upstream
现在,如果您在 master 上,则将上游/master 合并到 master 分支中
git merge upstream/master
就是这样!!
通过
git remote
命令交叉检查,更具体git remote -v
如果我也拥有上游存储库的提交权限,我可以创建一个本地上游分支并执行将在那里上游的工作。
回答by Richard Hansen
Sounds like you want a mirror of the remote repository:
听起来您想要远程存储库的镜像:
git clone --mirror url://to/remote.git local.git
That command creates a bare repository. If you don't want a bare repository, things get more complicated.
该命令创建一个裸存储库。如果您不想要一个裸存储库,事情会变得更加复杂。