简体   繁体   English

一次性将本地 git repo 与远程同步,丢弃本地更改/提交

[英]Sync local git repo with remote in one shot discarding local changes/commits

I'm a not a git expert so this might look like a silly question.我不是 git 专家,所以这可能看起来像一个愚蠢的问题。

I have local and remote repositories, I want to sync my local with the remote repository.我有本地和远程存储库,我想将我的本地存储库与远程存储库同步。 I have many local changes, stashed changes, and few commits which are not yet pushed to remote.我有许多本地更改、隐藏的更改以及尚未推送到远程的少量提交。

One way can be to remove all local changes(using git clean ), revert commits and than fetch/pull from remote.一种方法是删除所有本地更改(使用git clean ),恢复提交,而不是从远程fetch/pull But I think there must be some single command which can do all this in one go.但我认为必须有一些单一的命令可以一次性完成所有这些。 I tried using git reset --hard HEAD and then git pull but that gave me:我尝试使用git reset --hard HEAD然后git pull但这给了我:

# Your branch and 'origin/master' have diverged,
# and have 1 and 9 different commits each, respectively.

Was looking at this question as well, but didn't help.也在看这个问题,但没有帮助。

As commented, a git reset --hard origin/master would reset your master to upstream.如评论所述, git reset --hard origin/master会将您的master服务器重置为上游。

But: your current master (with its local commit) would be "lost" (at least, no longer visible).但是:您当前的主人(及其本地提交)将“丢失”(至少,不再可见)。

So one step isn't the best approach.所以一步不是最好的方法。

I would recommend something like:我会推荐类似的东西:

git checkout master
git fetch origin
git branch tmp
git reset --hard origin/master
git checkout tmp
git rebase master
git checkout master
git merge tmp
git branch -d tmp

That way, you rebase your local commit(s) on top of the updated master这样,您可以在更新的 master 之上重新设置本地提交

x--x--x--x--x (master, origin/master)       x--x--x--x--x--y'--y' (tmp, master)
       \                                 =>
        y--y  (tmp)                           (after rebase+merge)

The permanent fix if you always want to create a new branch on the remote to mirror and track your local branch(or vice-versa) is:如果您总是想在遥控器上创建一个新分支来镜像和跟踪您的本地分支(反之亦然),那么永久修复是:

git config --global push.default current

I always configure my local git with this command after I do git clone.执行 git clone 后,我总是使用此命令配置本地 git。 Although it can be applied anytime when the local-remote branch "Git fatal: The current branch has no upstream branch" error occurs.虽然它可以在本地远程分支“Git 致命:当前分支没有上游分支”错误发生时随时应用。

Hope this helps.希望这可以帮助。 Much peace.多平安。 :) :)

This command will discard all local commits on a branch and reset it to mirror what is currently on the remote:此命令将丢弃分支上的所有本地提交并将其重置为镜像远程当前的内容:

git pull
git reset --hard origin/your-branch

The pull command is to make sure what you have is the most up to date of the branch pull 命令是为了确保你拥有的是最新的分支

git fetch -[options]

Checkout:查看:

git fetch --help

for the functionalities you can use对于您可以使用的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将一个本地存储库与远程git存储库同步 - Sync one local repo with the remote git repository 提交到没有本地Git仓库的远程Git仓库? - Make commits to a remote Git repo without a local one? 远程 Git 存储库与本地存储库不同步 - Remote Git repo is out of sync with local repo 当远程仓库发生变化时自动同步本地仓库 - Automatically sync a local repo when changes occur in remote repo 在回购同步结束时放弃提交? - Discarding commits at the end of the repo sync? 我有两个本地存储库,一个在学校一个在家里,另一个在 github 上的远程存储库,我如何从一个本地存储库提交以同步到另一个本地存储库 - i have two local repos, one at school one at home, and the remote repo on github, how do i get commits from one local repo to sync to the other local Git:将所有本地提交与远程仓库版本进行比较 - Git: Compare All Local Commits to Remote Repo Version 查看本地和远程 Git 存储库之间的确切分歧/提交 - See exact divergence/commits between local and remote Git repo 从远程Git存储库中删除本地后面的提交 - Delete commits from remote Git repo that are behind local Git-从本地和远程仓库中删除多个先前的提交 - Git - Removing multiple previous commits from both local and remote repo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM